禁用iOS弹性体滚动和保持本机滚动正常工作

发布于 2025-01-01 21:32:13 字数 468 浏览 0 评论 0原文

我目前正在开发一个针对触摸设备(主要是 iOS)进行优化的单页 Web 应用程序。我已经通过 -webkit-overflow-scrolling: touch; 实现了新的 iOS 原生滚动,一切运行良好,除了我们仍在整个页面主体上体验苹果弹性滚动效果。

这涉及到当滚动结束或主体被推动时整个页面从视口的顶部/底部移开,并且真正暴露了这是一个网络应用程序的事实。我遵循了关于如何防止这种情况的各种指南,虽然它们确实有效,但它们阻止了内部可滚动元素一起工作。

这是一个小提琴来演示我到目前为止所使用的内容。

有没有人找到一种解决方案,禁用身体弹性滚动,但让内部可滚动工作?

I am currently working on a single page web app optimized for touch devices, mainly iOS. I've implemented the new iOS native scrolling via -webkit-overflow-scrolling: touch; and all works well except that we are still experiencing the apple elastic scroll effect on the whole page body.

This involves the whole page moving off the top/bottom of the viewport when a scroll ends or the body is pushed and really gives away the fact that this is a web app. I have followed various guidelines on how to prevent this and while they do work, they prevent inner scrollable elements from working altogether.

Here's a fiddle to demonstrate what I'm using so far.

Has anyone found a solution that disables body elastic scrolling but lets inner scrollables work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄离 2025-01-08 21:32:13

我已经采用了 Conditionally blockscrolling/touchmove event in mobile safari using Dojo 的良好解决方案:

var initialY = null;
dojo.connect(document, 'ontouchstart', function(e) {
    initialY = e.pageY;
});
dojo.connect(document, 'ontouchend', function(e) {
    initialY = null;
});
dojo.connect(document, 'ontouchcancel', function(e) {
    initialY = null;
});
dojo.connect(document, 'ontouchmove', function(e) {
    if(initialY !== null) {
        if(!dojo.query(e.target).closest('#content')[0]) {
            // The element to be scrolled is not the content node
            e.preventDefault();
            return;
        }

        var direction   = e.pageY - initialY;
        var contentNode = dojo.byId('content');

        if(direction > 0 && contentNode.scrollTop <= 0) {
            // The user is scrolling up, and the element is already scrolled to top
            e.preventDefault();
        } else if(direction < 0 && contentNode.scrollTop >= contentNode.scrollHeight - contentNode.clientHeight) {
            // The user is scrolling down, and the element is already scrolled to bottom
            e.preventDefault();
        }
    }
});

要滚动的元素是 #content在这种情况下。

I've adapted the good solution from Conditionally block scrolling/touchmove event in mobile safari using Dojo:

var initialY = null;
dojo.connect(document, 'ontouchstart', function(e) {
    initialY = e.pageY;
});
dojo.connect(document, 'ontouchend', function(e) {
    initialY = null;
});
dojo.connect(document, 'ontouchcancel', function(e) {
    initialY = null;
});
dojo.connect(document, 'ontouchmove', function(e) {
    if(initialY !== null) {
        if(!dojo.query(e.target).closest('#content')[0]) {
            // The element to be scrolled is not the content node
            e.preventDefault();
            return;
        }

        var direction   = e.pageY - initialY;
        var contentNode = dojo.byId('content');

        if(direction > 0 && contentNode.scrollTop <= 0) {
            // The user is scrolling up, and the element is already scrolled to top
            e.preventDefault();
        } else if(direction < 0 && contentNode.scrollTop >= contentNode.scrollHeight - contentNode.clientHeight) {
            // The user is scrolling down, and the element is already scrolled to bottom
            e.preventDefault();
        }
    }
});

The element to be scrolled is #content in this case.

一影成城 2025-01-08 21:32:13

也许 iScroll 就是您正在寻找的(如果我的问题正确的话)

Maybe iScroll is what you're looking for (if I got your question right)

醉梦枕江山 2025-01-08 21:32:13

冒着重复我的帖子的风险,我尝试解决同样的问题,到目前为止仅此而已:

        $(document).bind("touchmove",function(e){
            e.preventDefault();
        });
        $('.scrollable').bind("touchmove",function(e){
            e.stopPropagation();
        });

如果您有一个未覆盖整个屏幕的溢出元素(例如在 iPad 应用程序中),则此方法有效。但如果您有移动应用程序并且整个视口都被溢出的元素覆盖,则它不起作用。

我唯一能想到的就是检查$('.scrollable')的scrollTop(),然后有条件地绑定preventDefault()(如果它是0)。

尝试后,我注意到webkit UA报告scrollTop总是为0即使元素执行本机溢出滚动的“内部弹跳”,也会滚动到顶部。所以我不能做任何事情,因为我需要一个负的滚动顶部来设置我的条件。

叹。令人沮丧。

At the risk of duplicating my post, I try to solve the same issue and so far am only this far:

        $(document).bind("touchmove",function(e){
            e.preventDefault();
        });
        $('.scrollable').bind("touchmove",function(e){
            e.stopPropagation();
        });

This works if you have an overflow element that doesn't cover the whole screen, like in an iPad app for example. but it doesn't work if you have a mobile app and the entire viewport is covered by your overflowed element.

The only thing I could think of is to check the scrollTop() of the $('.scrollable') and then conditionally bind the preventDefault() if it's 0.

After trying, I noticed that the webkit UA reports scrollTop always as 0 when the element is scrolled to the top even when it does the "inside bounce" of the native overflow scroll. So i can't do anything since I'd require a negative scrollTop to set my condition.

Sigh. Frustrating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文