javascript - Detect scroll event on Android browser - Stack Overflow

I'm trying to detect the scroll event in Android browser (my specific version is 2.1, but U want i

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). 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.

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. As soon as we scroll the page in between (touchstart + touchmove + touchend), it breaks everything.

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.

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). 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.

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. As soon as we scroll the page in between (touchstart + touchmove + touchend), it breaks everything.

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.

Share Improve this question asked Sep 27, 2010 at 12:43 SavagemanSavageman 9,4876 gold badges42 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You may want to crawl the source for JQuery Mobile, it supports android browsers and has scroll event listeners.

Or at least they say it does in the docs. :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 );
            });
        }
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745534179a4631831.html

相关推荐

  • javascript - Detect scroll event on Android browser - Stack Overflow

    I'm trying to detect the scroll event in Android browser (my specific version is 2.1, but U want i

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信