Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

jquery - The same old issue, .scrollTop(0) not working in Chrome & Safari

First, let me point out, i've googled and even looked at answers on here like this and this, however, I'm still yet to find a working solution for my case.

I've designed a page that has several fixed elements covering the page and makes of html5/css3 to create a clean "mask" over the main document, thus allowing the body scroll bar to still scroll the underlying content.

In firefox and ie (bleh), scrollTop(0) is working perfect. However, as stated by the question, not so much in my fav browsers.

Something I've made note of is to call the following both before the scrollTo event and after

$("body,html,document").each(function(){ console.log($(this).scrollTop()); });

The results were not pleasing, it tells me that the scrolltop is already 0 and thus is not even attempting a scrollTop, or at least that's what I "think" thus far.

And before you ask, i made that console call on each of those 3 items as the document scrolltop should be covered within one of those items (i would think body, but like in ie you have to call html too)

Any takers on ideas?

FYI, it may be a css oddity (tho how it works in IE and not chrome i really cant understand) but I have already tried the following with negative results:

$(window).scrollTop(0);
$(document).scrollTop(0);
$("html").scrollTop(0);
$("body").scrollTop(0);
window.scroll(0,0);
$("body,html,document").scrollTop(0);
$("body,html").scrollTop(0);

Which I suppose extends my question, is this a css issue? I dont have an outside link and the code is too long (made with CI view Partials) to post all of it, but to CLARIFY what i've done:

  • Fixed header, footer, and sidebar leaving content to scroll with documet scrollbar
  • very little javascript or jquery implemented thus far, almost 0 custom css outside of fixing position of presaid elements
  • the "content" is ajax'd in using jQuery's .load function based on list items clicked in sidebar navigator

temp Fiddle no longer up

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem. If I put

$(window).scrollTop(0);

in the document ready handler, it didn't work; but if I test it in the javascript console panel of Chrome developer toolkit, it worked! The only difference was the execution time of the script. So I tried

window.setTimeout(function() {
    $(window).scrollTop(0); 
}, 0);

and it worked. Setting a delay greater than 0 is not neccesary, although that also worked. It's not jQuery's fault, because it is the same with

window.scrollTo(0, 0);  window.scroll(0, 0);

So the reason might be the browser populates the window.pageYOffset property after it renders the page and executes the js.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...