1. 移动端拖拽
JC.tools.drag = function(obj, parentNode) {
var pWidth, pHeight;
if (arguments.length == 1) {
parentNode = window.self;
pWidth = parentNode.innerWidth;
pHeight = parentNode.innerHeight;
} else {
pWidth = parentNode.clientWidth;
pHeight = parentNode.clientHeight;
}
obj.addEventListener('touchstart', function(event) {
if (event.touches.length === 1) {
event.preventDefault();
}
var touch = event.targetTouches[0];
var disX = touch.clientX - obj.offsetLeft,
disY = touch.clientY - obj.offsetTop;
var oWidth = obj.offsetWidth,
oHeight = obj.offsetHeight;
function moving(event) {
var touch = event.targetTouches[0];
obj.style.left = touch.clientX - disX + 'px';
obj.style.top = touch.clientY - disY + 'px';
if (obj.offsetLeft <= 0) {
obj.style.left = 0;
}
if (obj.offsetLeft >= pWidth - oWidth) {
obj.style.left = pWidth - oWidth + 'px';
}
if (obj.offsetTop <= 0) {
obj.style.top = 0;
}
if (obj.offsetTop >= pHeight - oHeight) {
obj.style.top = pHeight - oHeight + 'px';
}
}
obj.addEventListener('touchmove', moving);
obj.addEventListener('touchend', function() {
obj.removeEventListener('touchmove', moving);
obj.removeEventListener('touchend', moving);
});
});
};