Reputation: 63
Having an issue when getting the page position using scrollTop(). I am getting the position correctly but it is also causing the underlying page to scroll back to the top (see below). I have tried two ways:
var page_position = $(document).scrollTop();
$('#main-body').prepend(data.image).css('margin-top', page_position + 'px');
and:
$('#main-body').prepend(data.image).css('margin-top', $(document).scrollTop() + 'px');
What the data.image is is a div along with an image (large screenshot) that is returned from an ajax call. The div with the image is positioning correctly but as I mentioned above the underlying page is scrolling back to the top.
BTW, I am using Foundation and the jQuery that came packaged with it, I also tried is using jquery-1.11.3.min.js with the same results.
Upvotes: 0
Views: 77
Reputation: 63
Me be me I completely overlooked how I was adding the margin. I was adding it to the element I was adding the DIV to, not the DIV itself.
Upvotes: 0
Reputation: 73251
Plain javascript, but will do what you want:
var page_position = window.pageYOffset || document.documentElement.scrollTop
Upvotes: 1