1

I want to move an element on scroll event. My code works fine on chrome but not on firefox.

$body = $(document.body);

$(window).scroll(function () { 

$( "#topHeadVal" ).css('top', $body.scrollTop());    
});

http://jsfiddle.net/3cee7e2m/1/

falsarella
  • 12,217
  • 9
  • 69
  • 115
Petran
  • 7,677
  • 22
  • 65
  • 104
  • 1
    possible duplicate of [Can't get scrollTop() to work in both Chrome & Firefox](http://stackoverflow.com/questions/18778020/cant-get-scrolltop-to-work-in-both-chrome-firefox) – falsarella Feb 27 '15 at 21:47

3 Answers3

2

http://jsfiddle.net/3cee7e2m/2/

Use $(document) instead of $(document.body).

Also, your example code makes me concerned. If you're trying to stick something to a fixed position on the screen, are you sure you're not looking for the position: fixed?

szupie
  • 826
  • 10
  • 18
1

Use $(window).scrollTop() instead of $body.scrollTop(), here is the result http://jsfiddle.net/3cee7e2m/5/

YourFriend
  • 434
  • 4
  • 7
0

I replaced $body with $(window) and it worked:

$( "#topHeadVal" ).css('top','27px');
var $window = $(window);

$window.scroll(function () { 
    $( "#topHeadVal" ).css('top', $window.scrollTop());
});

Fiddle.

John Bupit
  • 10,406
  • 8
  • 39
  • 75