简体   繁体   中英

scrollTop not working automatically

I know there are a couple of questions to scrollTop already out there but I haven't really seen anything resembling my problem.

Using jquery 1.7.2 on an IE9 we have a page with three Tabs (JqueryUI). The Data is connected and that resulted in us only having the current tab on the page. Changing tabs will remove the unseen one and reload the one we jump into.

The Scroll-Positions are stored correctly in variables on the base page but trying to set that position in the document-ready-function does not work. An alert shows the correct number, so the function is actually called but the scrollbar does not move.

Calling the same function with a button on the page afterwards however works perfectly. The document-ready-function on the tab's jsp is quite simple:

<script type="text/javascript">
jQuery(document).ready(function(){
    setAhaScrollbar();
});

</script>

and the called function is quite simple as well:

function setAhaScrollbar() {
    var scrollWert = $('#scrollbarAnhaengeartikel').val();
    alert(scrollWert);
    $('#anhaengeGridScrollable').scrollTop(scrollWert); 
}

Called from document-ready it does nothing. Called from a button later on it works fine.

The div where the scroll position is supposed to be set is defined with overflow: auto and a fixed height

crollTop( value )
Description: Set the current vertical position of the scroll bar for each of the set of matched elements.
.scrollTop( value )
value
Type: Number An integer indicating the new position to set the scroll bar to.

More Information
As the documentation said value should be number.
Try

var scrollWert = Number($('#scrollbarAnhaengeartikel').val()); 

or

var scrollWert = parseInt($('#scrollbarAnhaengeartikel').val());

Apparently it was primarily a timing problem. Maybe there were still things going on when document ready fired.

Changing that function to

jQuery(document).ready(function(){
    setTimeout("setAhaScrollbar()", 500);
});

did the trick so my problem is solved.

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