javascript detect scroll position code example

Example 1: javascript get scroll position

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

Example 2: window log scrollpostion

$(window).on('scroll', function() {
    console.log( $(this).scrollTop() );
});

Example 3: scroll position

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

Example 4: checking scroll position with js

let scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;Code language: JavaScript (javascript)

Example 5: checking scroll position with js

document.documentElement.scrollTop = document.body.scrollTop = 1200;
document.documentElement.scrollLeft = document.body.scrollLeft = 600;Code language: JavaScript (javascript)