這篇文章主要介紹了javascript實(shí)現(xiàn)的元素拖動(dòng),將相應(yīng)的元素對(duì)象的引用傳到函數(shù)中。
//宿主為瀏覽器
//將相應(yīng)的元素對(duì)象的引用傳到函數(shù)中
function candrag(drager) {
drager.onmousedown = function (down) {
var offx = drager.offsetLeft
var offy = drager.offsetTop;
var offxl = down.clientX - offx;
var offyl = down.clientY - offy;
window.condition = 0;//為window添加了condition屬性,用于解決和click之間的矛盾
document.onmousemove = function (move) {
drager.style.left = move.clientX - offxl + "px";
drager.style.top = move.clientY - offyl + "px";
drager.style.cursor = "move";
condition = Math.abs(move.clientX - down.clientX) + Math.abs(move.clientY - down.clientY);
}
}
drager.onmouseup = function () {
document.onmousemove = null;
draggerr.style.cursor = "auto";
}
}
/*對(duì)于和click之間的矛盾解決,需要判斷condition
*例如:
candrag(dragger);
d01.onclick = function () {
if (!condition) {
d01.style.backgroundColor = "red";
}
}
*其中,d01為dragger的子元素
*/
更多信息請(qǐng)查看IT技術(shù)專欄