Skip to content

Instantly share code, notes, and snippets.

@madalinignisca
Created September 24, 2015 09:00
Show Gist options
  • Save madalinignisca/abce2f25de1f2ae57540 to your computer and use it in GitHub Desktop.
Save madalinignisca/abce2f25de1f2ae57540 to your computer and use it in GitHub Desktop.
Get position on window scroll
/* This is a snippet found on stackoverflow: http://stackoverflow.com/questions/3714628/jquery-get-the-location-of-an-element-relative-to-window
*/
function log(txt) {
$("#log").html("location : <b>" + txt + "</b> px")
}
$(function() {
var eTop = $('#element').offset().top; //get the offset top of the element
log(eTop - $(window).scrollTop()); //position of the ele w.r.t window
$(window).scroll(function() { //when window is scrolled
log(eTop - $(window).scrollTop());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment