有没有办法禁用右侧水平滚动,但允许左侧水平滚动?
let touches = []
window.addEventListener('touchmove', event => {
event.preventDefault()
}, false)
window.addEventListener('touchstart', event => {
touches[0] = event.touches[0].pageX
}, false)
window.addEventListener('touchend', event => {
touches[1] = event.changedTouches[0].pageX
}, false)
看来您必须在 touchmove
事件上preventDefault()
。
最佳答案
touchmove 返回 touche 列表
- touchmove事件对象有触摸列表
- 获得第一次接触,并在所有情况下防止违约。
- 移动时比较新的 x 值与旧的 x 值
- 如果 nx - ox > 0 则手动移动。
示例: http://codepen.io/anon/pen/LWQJvx
var old_x = null;
document.addEventListener('touchstart', function(e) {
old_x = e.targetTouches[0].clientX;
}, false);
document.addEventListener('touchcancel', function(e) {
old_x = e.targetTouches[0].clientX;
}, false);
document.addEventListener('touchmove', function(e) {
var touch = e.touches[0];
if (touch.clientX - old_x < 0 ) {
document.body.scrollLeft += old_x - touch.clientX;
}
old_x = touch.clientX;
e.preventDefault();
return false;
}, { passive: false });
关于javascript - 有没有办法禁用右侧水平滚动,但允许左侧水平滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42866799/