我有一个在台式计算机上运行良好的 mootools 拖动窗口,我找到了有关如何使 mootools 拖动支持触摸事件的答案。
当我在移动设备(IOS)上拖动时,Class.refactor
应用程序也会触发滚动事件。所以可拖动的窗口移动,屏幕同时滚动。
问题:当拖动事件“移动”时,有没有办法禁用/暂停滚动?或者当鼠标/触摸在拖动 div 区域内时?
小提琴示例在这里
还有我的代码(使用 mootools 1.3.2)
答案:(
document.getElement('.dragme').ontouchmove = function() {event.preventDefault();}
在下面的答案中解释)
html:
<div class="container">
<div class="dragme">drag me</div>
</div>
JS:
Class.refactor(Drag.Move,
{
attach: function(){
this.handles.addEvent('touchstart', this.bound.start);
return this.previous.apply(this, arguments);
},
detach: function(){
this.handles.removeEvent('touchstart', this.bound.start);
return this.previous.apply(this, arguments);
},
start: function(event){
document.body.addEvents({
touchmove: this.bound.check,
touchend: this.bound.cancel
});
this.previous.apply(this, arguments);
},
check: function(event){
if (this.options.preventDefault) event.preventDefault();
var distance = Math.round(Math.sqrt(Math.pow(event.page.x - this.mouse.start.x, 2) + Math.pow(event.page.y - this.mouse.start.y, 2)));
if (distance > this.options.snap){
this.cancel();
this.document.addEvents({
mousemove: this.bound.drag,
mouseup: this.bound.stop
});
document.body.addEvents({
touchmove: this.bound.drag,
touchend: this.bound.stop
});
this.fireEvent('start', [this.element, event]).fireEvent('snap', this.element);
}
},
cancel: function(event){
document.body.removeEvents({
touchmove: this.bound.check,
touchend: this.bound.cancel
});
return this.previous.apply(this, arguments);
},
stop: function(event){
document.body.removeEvents({
touchmove: this.bound.drag,
touchend: this.bound.stop
});
return this.previous.apply(this, arguments);
}
});
new Drag.Move(document.getElement('div.container'), {
handle: document.getElement('.dragme'),
modifiers: {
x: 'margin-left',
y: 'margin-top'
}
});