gpt4 book ai didi

javascript - jQuery UI 可拖动可防止在移动设备上滚动

转载 作者:数据小太阳 更新时间:2023-10-29 05:18:49 26 4
gpt4 key购买 nike

我有垂直列出的全屏宽度的可拖动元素。

我正在使用一个名为 (jquery.ui.touch-punch) 的插件来启用 jQuery 可在移动设备上拖动。但问题是可拖动元素会阻止用户滚动页面。

$('#novieList .element .content').draggable({
axis: 'x',
revert: function() {
return $(this).position().left < 30;
},
containment: [ 0, 0, 75, 0 ],
scope: 'element',
scroll: false,
delay: 300,
drag: function(event, ui) {
return true;
},
start: function(event, ui) {
// Prevent to drag the element after open it
var left = $(this).position().left;
return left == 0;
},
stop: function(event, ui) {
var left = $(this).position().left;
if (left != 0) {
$(this).offset({left: 75});
}
return true;
}
});

enter image description here

最佳答案

我认为在 jquery.ui.touch-punch.js 中注释掉 event.preventDefault() 不再有效。我尝试了相同的解决方案,发现 jQuery UI draggable 本身阻止了垂直滚动的默认行为——即使元素设置为仅沿 x 轴拖动。

对我有用的解决方案是测量光标垂直位置的任何变化,并使用 window.scrollBy 手动将窗口滚动相同的量:

var firstY = null;      
var lastY = null;
var currentY = null;
var vertScroll = false;
var initAdjustment = 0;

// record the initial position of the cursor on start of the touch
jqDraggableItem.on("touchstart", function(event) {
lastY = currentY = firstY = event.originalEvent.touches[0].pageY;
});

// fires whenever the cursor moves
jqDraggableItem.on("touchmove", function(event) {
currentY = event.originalEvent.touches[0].pageY;
var adjustment = lastY-currentY;

// Mimic native vertical scrolling where scrolling only starts after the
// cursor has moved up or down from its original position by ~30 pixels.
if (vertScroll == false && Math.abs(currentY-firstY) > 30) {
vertScroll = true;
initAdjustment = currentY-firstY;
}

// only apply the adjustment if the user has met the threshold for vertical scrolling
if (vertScroll == true) {
window.scrollBy(0,adjustment + initAdjustment);
lastY = currentY + adjustment;
}

});

// when the user lifts their finger, they will again need to meet the
// threshold before vertical scrolling starts.
jqDraggableItem.on("touchend", function(event) {
vertScroll = false;
});

这将非常模拟触摸设备上的 native 滚动。

关于javascript - jQuery UI 可拖动可防止在移动设备上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com