Last active
December 26, 2015 03:19
-
-
Save zzuhan/7085422 to your computer and use it in GitHub Desktop.
code_structure simple module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function drag(o){ | |
var params = { | |
startLeft: 0, | |
startTop: 0, | |
_x: | |
_y: | |
isDrag: false | |
}; | |
params.startLeft = o.currentLeft; | |
params.startTop = o.currentTop; | |
// 记录start值 | |
o.onmousedown = function (e) { | |
e = e || window.event; | |
params.isDrag = true; | |
// 为什么不在这里记录初始值 因为要抓的物体, | |
// 本身是静态的,生成实例时,记录下就ok了 | |
// 还有就是在鼠标停止时更新下当前的位置,这就减少了 | |
// 每次鼠标按下要执行的东西 | |
// tip:从实物的状态出发,看它哪些状况会变, | |
// params.startLeft = o.currentLeft; | |
params._x = e.clientX; params._y = e.clientY; | |
} | |
o.onmouseup = function () { | |
params.isDrag = false; | |
// 更新start值 | |
params.startLeft = o.currentLeft; | |
params.startTop = o.currentTop; | |
} | |
o.onmousemove = function (e) { | |
e = e || window.event; | |
var diffX = e.clientX - params._x ; | |
o.css('left', params.startLeft+diffX); | |
o.css('top', params.startTop+diffY); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment