dc dalton
dc dalton

Reputation: 63

Using scrollTop() to get position but it is also scrolling the page

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

Answers (2)

dc dalton
dc dalton

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

baao
baao

Reputation: 73251

Plain javascript, but will do what you want:

var page_position = window.pageYOffset || document.documentElement.scrollTop

Upvotes: 1

Related Questions