简体   繁体   中英

Want to Scroll to a position after loading but after a short stint it automatically scrolls back to the top (JQUERY,JQUERY-MOBILE)

I use SYMFONY 3 and JQUERY 1.11.1 + JQUERY MOBILE 1.4.2

On a page I have a table, each row of the table can be extended (containing a form regarding the row), when I open a row the page is generated again thru SYMFONY controller.

At the bottom of my TWIG file, I add a tag with $(document).ready(...). I want to manipulate the SCROLL thru JQUERY (or JS) to reach the position of my opened row.

I've seen that it could be done that way:

    $('body').animate({
          scrollTop: $('#myRowId').offset().top + 'px'
    }, "fast");

Or that way:

$(document).scrollTop($('#myRowId').offset().top));

It does work for a short time and then the SCROLL goes back automatically to the top (y-position:0) with not any explanation (that I am aware of).

I managed to go around by using the setTimeout like:

 setTimeout(function({$(document).scrollTop($('#myRowId').offset().top);},5000);

But it is not really nice, i'd rather know which event is triggered by JQUERY or JQUERY MOBILE under the hood that sets the SCROLL back to the top.

Has anyone got any idea?

Here is the solution with the onload event:

$(window).load({'rowId':'#'+rowRootId+'_id'},function(event){
     $('body').animate({scrollTop: $(event.data.rowId).offset().top + 'px'}, "normal");
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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