一、面向?qū)ο蟮乃悸放c代碼步驟:
···
function Drg(box4){ (Drg是一個(gè)構(gòu)造函數(shù),box4是一個(gè)參數(shù))
this.ele=document.getElementById(box4)
var self = this; (保留這個(gè)this.ele的值)
this.ele.onmousedown=function(e){ (鼠標(biāo)點(diǎn)擊的函數(shù)事件)
self.start(e);
}
this.start=function(e){
e.preventDefault()
var t = e.clientX-this.ele.offsetLeft;
var y = e.clientY-this.ele.offsetTop;
document.onmousemove=function(e){ (點(diǎn)擊的時(shí)候要調(diào)用move的這個(gè)功能)
self.move(e,t,y)
}
}
this.move=function(e,t,y){
this.ele.style.left = e.clientX-t + "px"
this.ele.style.top = e.clientY-y + "px"
// console.log(e.clientX,e.clientY)
}
this.stop=function(){ 當(dāng)鼠標(biāo)松開(kāi)的時(shí)候,要將移動(dòng)事件清除)
document.onmousemove=null
}
this.ele.onmouseup=function(){
self.stop()
}
}
總結(jié):經(jīng)過(guò)面向過(guò)程與面向?qū)ο蟮膶W(xué)習(xí),我覺(jué)得面向過(guò)程好理解一些,面向?qū)ο笾械膖his
很容易混淆,面向過(guò)程與面向?qū)ο蟮拇a差不多,思路不一樣,有時(shí)候聽(tīng)課聽(tīng)著就不知道this到底在哪里,到底指什么,有時(shí)候單學(xué)知識(shí)點(diǎn)能懂,但是運(yùn)用起來(lái)就亂了