简体   繁体   English

以编程方式停止-webkit-overflow-scrolling

[英]Programmatically halt -webkit-overflow-scrolling

I have a phonegap application that uses iOS native scrolling through -webkit-overflow-scrolling in a div. 我有一个phonegap应用程序,它通过div中的-webkit-overflow-scrolling使用iOS本机滚动。 I want to be able to manually halt an ongoing scroll when the user clicks a button (to scroll back to the top of the page). 我希望能够在用户单击按钮时手动停止正在进行的滚动(以滚动回到页面顶部)。 Is this doable? 这可行吗?

This is actually very possible when using fastclick.js . 当使用fastclick.js时,这实际上是很有可能的。 The lib removes the 300ms click delay on mobile devices and enables event capturing during inertia/momentum scrolling. lib消除了移动设备上300ms的单击延迟,并启用了惯性/动量滚动过程中的事件捕获。

After including fastclick and attaching it to the body element, my code to stop scrolling and go to the top looks like this: 包含fastclick并将其附加到body元素后,我的代码停止滚动并转到顶部,如下所示:

scrollElement.style.overflow = 'hidden';
scrollElement.scrollTop = 0;
setTimeout(function() {
  scrollElement.style.overflow = '';
}, 10);

The trick is to set overflow: hidden , which stops the inertia/momentum scrolling. 诀窍是设置overflow: hidden ,以停止惯性/动量滚动。 Please see my fiddle for a full implementation of stop scrolling during inertia/momentum . 请参阅我的小提琴以获得在惯性/动量期间停止滚动的完整实现。

Unfortunately this is not possible at the moment. 不幸的是,目前这还不可能。 The scroll event is triggered only when the scrolling has come to an end. 仅当滚动结束时才触发scroll事件。 As long as the momentum keeps moving the content no events are fired at all. 只要动量持续移动内容,就不会触发任何事件。 You can see this in Figure 6-1 The panning gesture in Apple's " Safari Web Content Guide ". 您可以在图6-1 Apple的Safari Web Content Guide中找到 平移手势

I also created a fiddle to demonstrate this behavior. 我还创建了一个小提琴来演示这种行为。 The scrollTop value is set after iOS is done animating. 在完成动画设置后,将设置scrollTop值。

You can capture a touch event using 'touchstart' instead of 'click', as the click event sometimes doesn't seem to get fired until the momentum scroll completes. 您可以使用“ touchstart”而非“ click”捕获触摸事件,因为在动量滚动完成之前,似乎不会触发click事件。 Try this jQuery solution: 试试这个jQuery解决方案:

$('#yourTrigger').on('touchstart', function () {
    var $div = $('.yourScrollableDiv');
    if ($div.scrollTop() === 0) {
        return false; //if no scroll needed, do nothing.
    }
    $div.addClass('scrolling'); //apply the overflow:hidden style with a class
    $div.animate({
        scrollTop: 0
    }, 600, function () {
        $div.removeClass('scrolling'); //scrolling has finished, remove overflow:hidden
    });
}

where the 'scrolling' class simply has the CSS property, overflow:hidden, which as @Patrick-Rudolph said, will halt any momentum scrolling in progress. @ scatrick-Rudolph表示,“ scrolling”类仅具有CSS属性“ overflow:hidden”,它将停止进行中的任何动量滚动。

.scrolling {
    overflow: hidden;
}

Note: It's best to use a callback function to tell when your scroll animation finishes, rather than setting a timer function. 注意:最好使用回调函数来告知滚动动画何时结束,而不是设置计时器函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:-webkit-overflow-scrolling:触摸和滚动 - iOS: -webkit-overflow-scrolling:touch and scrollTop -webkit-overflow-scrolling打破绝对定位 - -webkit-overflow-scrolling breaks absolute positioning -webkit-overflow-scrolling:自动; 在触控装置上无法运作 - -webkit-overflow-scrolling: auto; not working on touch devices 移动网络:-webkit-overflow-scrolling:触摸与位置冲突:已修复 - Mobile web: -webkit-overflow-scrolling: touch conflicts with position:fixed scrollLeft通过-webkit-overflow-scrolling自动返回到旧位置: - scrollLeft automatically returning to old position with -webkit-overflow-scrolling: touch JQueryUI可拖动加上-webkit-overflow-scrolling隐藏拖动元素吗? - JQueryUI draggable plus -webkit-overflow-scrolling hides dragging elements? 如何在react组件上设置-webkit-overflow-scrolling内联样式 - How to set -webkit-overflow-scrolling inline style on react component 任何方式应用'webkit-overflow-scrolling:touch'内嵌javascript? - Any way to apply 'webkit-overflow-scrolling: touch' inline with javascript? -webkit-overflow-scrolling:触摸导致jQuery动态更改不显示? - -webkit-overflow-scrolling: touch causes jQuery dynamic changes to not show? scrollTop不能与-webkit-overflow-scrolling一起使用:touch - scrollTop doesn't work with -webkit-overflow-scrolling: touch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM