Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollTop() returns 0 in Firefox, but not in Chrome

Not sure if there's another question regarding this, if so I apologize and please don't release the hounds.

Using the html5 doctype and doing a quick console.log off my scroll listener that tells me the value of scrollTop() value. I'm basically doing this so when I scroll past a point, I change the opacity of an element. I'm doing this using an MVS solution and I don't have the ability to push this to an external site so you can look. Here's a quick snippet:

var opacity = 1; var scrollTop = $('body').scrollTop(); if (scrollTop > 200) {    opacity = 0.1; } $('#element).css('opacity', opacity); 

If I scroll in Chrome, I get a console.log(scrollTop); displaying what I want (ie; 100 for each scroll I do) and my opacity disappears after I hit 200 scrollTop. If I scroll in FF and IE7+ the var returns "0" each scroll. If I change $('body').scrollTop() to $('document').scrollTop(); then I get a "null" return on scroll.

Any ideas? Thanks!

like image 404
lxndr Avatar asked Oct 17 '12 18:10

lxndr


People also ask

Why is scrollTop not working?

If your CSS html element has the following overflow markup, scrollTop will not function. To allow scrollTop to scroll, modify your markup remove overflow markup from the html element and append to a body element.

What is $( window scrollTop ()?

$(selector).scrollTop(position) Parameter. Description. position. Specifies the vertical scrollbar position in pixels.


2 Answers

Try to use var scrollTop = $(document).scrollTop();

like image 167
jovobe Avatar answered Oct 01 '22 18:10

jovobe


$(window).scrollTop() works as expected in both Firefox and Chrome.

For verification run the following jsfiddle in both chrome and firefox: http://jsfiddle.net/RBBw5/6/

like image 25
Rango Avatar answered Oct 01 '22 20:10

Rango