iOS Safari – 如何禁用过度滚动但允许可滚动的 div 正常滚动? [英] iOS Safari – How to disable overscroll but allow scrollable divs to scroll normally?

查看:41
本文介绍了iOS Safari – 如何禁用过度滚动但允许可滚动的 div 正常滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于 iPad 的网络应用程序,需要防止过度滚动,使其看起来不像网页.我目前正在使用它来冻结视口并禁用过度滚动:

I'm working on an iPad-based web app, and need to prevent overscrolling so that it seems less like a web page. I'm currently using this to freeze the viewport and disable overscroll:

document.body.addEventListener('touchmove',function(e){
      e.preventDefault();
  });

这非常适合禁用过度滚动,但我的应用程序有几个可滚动的 div,并且上面的代码阻止它们滚动.

This works great to disable overscroll but my app has several scrollable divs, and the above code prevents them from scrolling.

我只针对 iOS 5 及更高版本,所以我避免了像 iScroll 这样的黑客解决方案.相反,我将这个 CSS 用于我的可滚动 div:

I'm targeting iOS 5 and above only so I've avoided hacky solutions like iScroll. Instead I'm using this CSS for my scrollable divs:

.scrollable {
    -webkit-overflow-scrolling: touch;
    overflow-y:auto;
}

这在没有文档过度滚动脚本的情况下有效,但不能解决 div 滚动问题.

This works without the document overscroll script, but doesn't solve the div scrolling problem.

如果没有 jQuery 插件,有没有办法使用过度滚动修复但免除我的 $('.scrollable') div?

Without a jQuery plugin, is there any way to use the overscroll fix but exempt my $('.scrollable') divs?

我找到了一个不错的解决方案:

I found something that's a decent solution:

 // Disable overscroll / viewport moving on everything but scrollable divs
 $('body').on('touchmove', function (e) {
         if (!$('.scrollable').has($(e.target)).length) e.preventDefault();
 });

当您滚动超过 div 的开头或结尾时,视口仍然会移动.我也想找到一种方法来禁用它.

The viewport still moves when you scroll past the beginning or end of the div. I'd like to find a way to disable that as well.

推荐答案

这解决了当你滚动到 div 的开头或结尾时的问题

This solves the issue when you scroll past the beginning or end of the div

var selScrollable = '.scrollable';
// 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', selScrollable, 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;
  }
});
// Stops preventDefault from being called on document if it sees a scrollable div
$('body').on('touchmove', selScrollable, function(e) {
  e.stopPropagation();
});

请注意,如果您想在 div 没有溢出时阻止整个页面滚动,这将不起作用.要阻止它,请使用以下事件处理程序而不是上面的事件处理程序(改编自 this question):

Note that this won't work if you want to block whole page scrolling when a div does not have overflow. To block that, use the following event handler instead of the one immediately above (adapted from this question):

$('body').on('touchmove', selScrollable, function(e) {
    // Only block default if internal div contents are large enough to scroll
    // Warning: scrollHeight support is not universal. (https://stackoverflow.com/a/15033226/40352)
    if($(this)[0].scrollHeight > $(this).innerHeight()) {
        e.stopPropagation();
    }
});

这篇关于iOS Safari – 如何禁用过度滚动但允许可滚动的 div 正常滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆