thesmart / jquery-scrollspy

A ScrollSpy library for detecting enter/exit of elements in the viewport when the user scrolls
Other
276 stars 99 forks source link

jQuery Mobile page navigation stops scrollspy #11

Open mrextreme opened 10 years ago

mrextreme commented 10 years ago

I am developing a jQuery Mobile 1.4 multi-page mobile app, also using ScrollSpy. It lists items on a page, and when it is (supposedly) seen by the user, it pings a server. You can click on the list item to go to a detail view page. When I navigate back to the list view, ScrollSpy doesn't spy any more.

As a fix, I had to add this to the scrollspy source code:

    $.clearScrollSpy = function()
    {
        jWindow = $( window );
        elements = [];
        elementsInView = [];
        isSpying = false;
        ticks = 0;
        offset = {
            top : 0,
            right : 0,
            bottom : 0,
            left : 0,
        };

        return this;
    };

It sets the object properties to their default values. Then call it like this after navigating back to the list view page: $.clearScrollSpy().scrollSpy( '.coupon_tracker' ); . I use the class "coupon_tracker" to identify HTML elements to be tracked.

thesmart commented 10 years ago

Are you removing the elements from DOM and disposing of them during navigation? Scollspy works by inspecting each element that matches the scollspy selector upon each scroll tick. So, unless those elements are being disposed, it should still work.

In the next version, I'll add some destructor functionality to clean up scrollspy's element watch list. Thanks for the submission.

mrextreme commented 10 years ago

I don't remove anything, and I have no clue how jQM does the page changes internally, but for sure scroll spying stops after a page change away from and back to a page. It might work if I change the selector to be more specific, I never tried that, it just popped into my mind. Like not just targeting any "coupon_tracker" class, but in a specific (active) page, content div, etc. etc. and then the class. I'll give it a try later.