JQuery Mobile - keep scroll position when device orientation changes?

I am using jQuery-mobile

to display a list of data. The list can be quite long, say ~ 3000px. The problem I see on mine Samsung G S5

is when I rotate the phone, the list goes through its scrollable position and is positioned to the top.

I tried the following but couldn't get it to work:

// This block is to handle the device rotating when viewing the list
    $(window).on('scrollstop', function() {
        if ($.mobile.activePage.attr('id') == 'pageBeingViewed') {
            self.currentScroll = $(window).scrollTop();
        }            
    });
    $(window).on("orientationchange", function (event) {
        if ($.mobile.activePage.attr('id') == 'pageBeingViewed') {
            console.log('Scroll Pos:'+self.currentScroll);
            $(window).scrollTop(self.currentScroll);  // also tried using the jQM silent scroll method here as well.  No luck.       
        }           
    });

      

This should logically work. It locks the scroll position on the fly, so when you rotate the device, it should move it back to where it was. But I can't understand for life why it doesn't work. Anyone have any ideas / suggestions?

+3


source to share


1 answer


Check divs

on firebug

and make sure it is an element that has a scrollbar. sometimes it happens that body

, page

and are ui-content

set to height 100%

, so the scrollbar ends in a div ui-content

. If so, you can solve it by directly accessing the scrollTop()

div that has the scrollbar.



0


source







All Articles