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());
});
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/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
?
Use $(window).scrollTop()
instead of $body.scrollTop(), here is the result http://jsfiddle.net/3cee7e2m/5/
I replaced $body
with $(window)
and it worked:
$( "#topHeadVal" ).css('top','27px');
var $window = $(window);
$window.scroll(function () {
$( "#topHeadVal" ).css('top', $window.scrollTop());
});