Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile 1.4 infinite scrolling: Window scroll not firing

In jQuery Mobile 1.4, why isn't $(window).scroll firing? Here's a non-working example trying to detect when the user has scrolled to the end of the page:

http://jsfiddle.net/5x6T2/

$(document).on('pagecontainershow', function () {
    $(window).scroll(function () {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            alert("Bottom reached!");
        }
    });
});

This was all working fine in jQuery Mobile 1.3 prior to the deprecation of pageshow:

http://jsfiddle.net/Demr7/

$(document).on('pageshow', '.ui-page', function() {
    $(window).scroll(function () {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            alert("Bottom reached!");
        }
    });
});

Anybody know what to do?

like image 981
Mark Boulder Avatar asked Jul 14 '14 00:07

Mark Boulder


People also ask

What is scroll event in JavaScript?

.scroll() Categories: Events > Browser Events. Description: Bind an event handler to the "scroll" JavaScript event, or trigger that event on an element. version added: 1.0.scroll( handler ) handler. Type: Function( Event eventObject ) A function to execute each time the event is triggered.

How do I trigger a scroll event manually?

To trigger the event manually, apply .scroll () without an argument: After this code executes, clicks on Trigger the handler will also append the message. A scroll event is sent whenever the element's scroll position changes, regardless of the cause.

Why am I getting an alert when I scroll down the page?

This means something else is probably wrong with your code. Scroll to the bottom and you see the alert. Am using json reteriving my db data. With this script i can load db while page scroll down.

How do I detach a scroll bar?

A mouse click or drag on the scroll bar, dragging inside the element, pressing the arrow keys, or using the mouse's scroll wheel could cause this event. As the .scroll () method is just a shorthand for .on ( "scroll", handler ), detaching is possible using .off ( "scroll" ) .


1 Answers

You don't have to use any third party plugin to achieve infinity scrolling. You simply need to listen to either scrollstart or scrollstop and do some math.

What you need is $(window).scrollTop(), $.mobile.getScreenHeight(), $(".ui-content").outerHeight(), $(".ui-header").outerHeight() and $(".ui-footer").outerHeight().

When $(window).scrollTop()'s value matches the value of viewport's height minus content div's height plus toolbars height, it means you have reached the bottom of the page.

Note that you should remove 1px of retrieved height of each fixed toolbars.

Attach scrollstop listener to document and then define heights variables.

$(document).on("scrollstop", function (e) {

        /* active page */
    var activePage = $.mobile.pageContainer.pagecontainer("getActivePage"),

        /* window's scrollTop() */
        scrolled = $(window).scrollTop(),

        /* viewport */
        screenHeight = $.mobile.getScreenHeight(),

        /* content div height within active page */
        contentHeight = $(".ui-content", activePage).outerHeight(),

        /* header's height within active page (remove -1 for unfixed) */
        header = $(".ui-header", activePage).outerHeight() - 1,

        /* footer's height within active page (remove -1 for unfixed) */
        footer = $(".ui-footer", activePage).outerHeight() - 1,

        /* total height to scroll */
        scrollEnd = contentHeight - screenHeight + header + footer;

    /* if total scrolled value is equal or greater
       than total height of content div (total scroll)
       and active page is the target page (pageX not any other page)
       call addMore() function */
    if (activePage[0].id == "pageX" && scrolled >= scrollEnd) {
        addMore();
    }
});

Demo (1)

(1) Tested on iPhone 5 Safari Mobile

like image 142
Omar Avatar answered Sep 18 '22 00:09

Omar