Jquery拖拽元素方法


//初始化div拖拽 參數分別為:元素,拖拽方向(L和R代表靠左或右拖拽,為空或null表示隨意拖拽),minLeft,maxLeft表示距兩邊距離,mtop距頂端距離,mBottom距離底邊距離
function initDivMove(deeplinkdiv,direction,minLeft,maxLeft,mTop,oBottom) {
var moves = ["touchstart", "touchmove", "touchend"]
var isdown = false;
var flag = "touch";
var maxH = document.body.clientHeight;
var oL;
var oR;
var oT;
//移動開始
deeplinkdiv.addEventListener(moves[0], function (e) {
isdown = true;
var ev = e || window.event;
var touch = flag == "touch" ? ev.targetTouches[0] : ev;
if (direction == "L") {
oL = 0;
} else if (direction == "R") {
oR = 0;
} else {
oL = touch.clientX - deeplinkdiv.offsetLeft;
}
oT = touch.clientY - deeplinkdiv.offsetTop;
deeplinkdiv.addEventListener(moves[1], preventDefault, false);

});
//移動中
deeplinkdiv.addEventListener(moves[1], function (e) {
if (!isdown) { return; }
var ev = e || window.event;
var touch = flag == "touch" ? ev.targetTouches[0] : ev;
var oLeft = oL;
var oRight = oR;
var oTop = touch.clientY - oT;
if (oTop < mTop) {
oTop = mTop;
} else if (oTop >= maxH - oBottom) {
oTop = maxH - oBottom;
}
if (direction == "L") {
deeplinkdiv.style.left = oLeft + 'px';
} else if (direction == "R") {
deeplinkdiv.style.Right = oRight + 'px';
} else {

oLeft = touch.clientX - oLeft;
if (oLeft < minLeft) { oLeft = minLeft }
if (oLeft > maxLeft) { oLeft = maxLeft }
deeplinkdiv.style.left = oLeft + 'px';
}

deeplinkdiv.style.top = oTop + 'px';

});
//觸摸結束
deeplinkdiv.addEventListener(moves[2], function () {
isdown = false;
document.removeEventListener(moves[1], preventDefault);
});

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM