简体   繁体   English

在Android浏览器上检测滚动事件

[英]Detect scroll event on Android browser

I'm trying to detect the scroll event in Android browser (my specific version is 2.1, but U want it to work also on older versions). 我正在尝试在Android浏览器中检测滚动事件(我的特定版本是2.1,但是你希望它也适用于旧版本)。 This seems impossible! 这似乎不可能!

I first tried this: 我第一次尝试这个:

document.addEventListener('scroll', function(){ alert('test'); }, false);

But nothing is triggered (except when the page load). 但没有触发任何内容(页面加载时除外)。

I thought: well, let's be crazy and emulate it by : 1. Detecting touchend 2. Polling the window.pageYOffset so we know when the window stops scrolling 3. Manually trigger a user function I want on scroll. 我想:好吧,让我们疯狂并通过以下方式模仿:1。检测touchend 2.轮询window.pageYOffset以便我们知道窗口何时停止滚动3.在滚动时手动触发我想要的用户功能。

Unfortunately, the touchend event doesn't look to be triggered either... in fact, when we don't scroll and only tap the screen (touchstart + touchend), it works. 不幸的是,touchend事件看起来也不会被触发......事实上,当我们不滚动并只点击屏幕(touchstart + touchend)时,它就可以工作了。 As soon as we scroll the page in between (touchstart + touchmove + touchend), it breaks everything. 一旦我们在中间滚动页面(touchstart + touchmove + touchend),它就会破坏所有内容。

Now my most basic example only contains this: 现在我最基本的例子只包含这个:

document.addEventListener('touchend', function(){ alert('test'); }, false);

But the alert doesn't show up when we scroll with the finger and release the touch... 但是当我们用手指滚动并释放触摸时,警报不会显示出来......

Does anyone has a suggestion? 有没有人有建议?

Thanks. 谢谢。

You may want to crawl the source for JQuery Mobile, it supports android browsers and has scroll event listeners. 您可能想要抓取JQuery Mobile的源代码,它支持Android浏览器并具有滚动事件侦听器。

Or at least they say it does in the docs. 或者至少他们说它在文档中做了。 :p :p

Here's the source 这是源头

$.event.special.scrollstart = {
    enabled: true,

        setup: function() {
            var thisObject = this,
                $this = $( thisObject ),
                    scrolling,
                    timer;

            function trigger( event, state ) {
                scrolling = state;
                var originalType = event.type;
                event.type = scrolling ? "scrollstart" : "scrollstop";
                $.event.handle.call( thisObject, event );
                event.type = originalType;
            }

            // iPhone triggers scroll after a small delay; use touchmove instead
            $this.bind( scrollEvent, function( event ) {
                if ( !$.event.special.scrollstart.enabled ) {
                    return;
                }

                if ( !scrolling ) {
                    trigger( event, true );
                }

                clearTimeout( timer );
                timer = setTimeout(function() {
                    trigger( event, false );
                }, 50 );
            });
        }
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM