javascript 在 iOS Safari 中禁用过度滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12639888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:44:49  来源:igfitidea点击:

Disable overscroll in iOS Safari

javascriptjqueryiphonetouchmobile-safari

提问by keepyourweb

How can I prevent overscroll in Safari iOS? I would use the touch gesture for navigate on a site but I can't.

如何防止 Safari iOS 中的过度滚动?我会使用触摸手势在网站上导航,但我不能。

I tried this:

我试过这个:

$(window).on('touchstart', function(event) {

    event.preventDefault();

});

But in this way I disabled all gesture, infact I can't zooming with pinch-in and pinch-out.

但是通过这种方式,我禁用了所有手势,事实上我无法通过捏合和捏合进行缩放。

Any solutions? Thanks.

任何解决方案?谢谢。

回答by Tyler Dodge

This way will allow scrollable elements while still preventing the overscroll in the browser itself.

这种方式将允许可滚动元素,同时仍然防止浏览器本身的过度滚动。

//uses document because document will be topmost level in bubbling
$(document).on('touchmove',function(e){
  e.preventDefault();
});
//uses body because jquery on events are called off of the element they are
//added to, so bubbling would not work if we used document instead.
$('body').on('touchstart','.scrollable',function(e) {
  if (e.currentTarget.scrollTop === 0) {
    e.currentTarget.scrollTop = 1;
  } else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
    e.currentTarget.scrollTop -= 1;
  }
});
//prevents preventDefault from being called on document if it sees a scrollable div
$('body').on('touchmove','.scrollable',function(e) {
  e.stopPropagation();
});

回答by Todd

This Shouldbe the easiest way to accomplish this:

应该是实现此目的的最简单方法:

$(function() {
    document.addEventListener("touchmove", function(e){ e.preventDefault(); }, false);
});

Hope this helps.

希望这可以帮助。

Best.

最好的。