javascript scroll event position code example

Example 1: javascript get scroll position

function getYPosition(){
  var top  = window.pageYOffset || document.documentElement.scrollTop
  return top;
}

Example 2: js onscroll event

// HTML:
<element onscroll="myScript">
// JavaScript:
object.addEventListener("scroll", myScript)
// or
object.onscroll = function() { /*...*/ }

Example 3: scroll position

$(window).scroll(function() {
	var $height = $(window).scrollTop();
  if($height > 50) {
		//do something
	} else {
		//do something
	}
});

Tags:

Css Example