24

This script:

function onscroll(){
    document.getElementById("divs").style.top=""+$('body').scrollTop()+"px";
}

Fiddle: http://jsfiddle.net/Hed2J/1/

Doesn't work on the latest Firefox version!

What am I doing wrong? :) Thanks for the help!

Edit: Edited with JSFiddle and full script :) as attached to an onscroll event

A-312
  • 10,203
  • 4
  • 38
  • 66
seanlevan
  • 1,269
  • 2
  • 13
  • 32
  • There's nothing wrong with the code you have shown. Please post more of your code - the question shouldn't be dependent on external resources. Also, "doesn't work" is pretty vague – John Dvorak Jul 21 '13 at 20:31
  • 1
    But it does "work" in other browsers/previous versions of FF? – Teemu Jul 21 '13 at 20:33
  • Sorry, it works in Chrome, and here is a JSfiddle for more details :D http://jsfiddle.net/Hed2J/ – seanlevan Jul 21 '13 at 20:34
  • Here it is attached to an onscroll event: http://jsfiddle.net/Hed2J/1/ – seanlevan Jul 21 '13 at 20:36
  • And I haven't tested it in any previous version of FF. – seanlevan Jul 21 '13 at 20:37
  • please post the code _here_ (as well as in a fiddle), and clarify "doesn't work" – John Dvorak Jul 21 '13 at 20:37
  • Done :) and @JanDvorak I decided to do that for purely testing purposes... though I could have just made any element large in size... – seanlevan Jul 21 '13 at 20:39
  • If you want to derive some value from a scroll position, perhaps you should react to changes in the scroll position? It's likely to be 0 on page load. – John Dvorak Jul 21 '13 at 20:39
  • I attached it through `onscroll`, though that would suffice... – seanlevan Jul 21 '13 at 20:40
  • Why not use `position:fixed` to achieve that effect? – John Dvorak Jul 21 '13 at 20:42
  • Jan, I'm using a dynamically scrolling thing. In a real life situation, it would look more like a spotify splash page... I've already finished programming it, and the div moves up (negative value multiplied) while you scroll. Nice effect to achieve, if it's cross-browser! – seanlevan Jul 21 '13 at 20:42
  • Just saying, animating `scrolltop` to a specific position doesn't work either in Firefox for me... – seanlevan Jul 21 '13 at 20:43

2 Answers2

45

Try this fiddle, it is working in chrome, ie and the latest version of mozilla : http://jsfiddle.net/Hed2J/3/

I replace $('body').scrollTop() with $(window).scrollTop().

Lucas Willems
  • 5,931
  • 2
  • 25
  • 41
  • Woah, thanks dood. I thought I had tried it- but I guess not! Thanks a heap... Isn't it ridiculous how the easiest things turn out hardest to figure out? – seanlevan Jul 21 '13 at 20:52
  • For me, simply working with $(window).scrollTop() did the trick! Thanks. – TheCuBeMan Oct 25 '15 at 16:26
  • This fix works for me great! And I have compared $(window) with $('body') in Chrome, they return the same value. Thanks!! – hailong Aug 04 '17 at 13:57
35

A little off-topic, but if you want to animate using scrolltop, you must do

$('html,body').animate({scrollTop:0}, 'slow');

Note that we target both html and body because html will make scroll in Firefox works and body for other browsers.

Thanh Trung
  • 3,114
  • 3
  • 25
  • 37