Detecting scroll direction

detect scroll direction jquery
react detect scroll direction
javascript detect scroll
javascript reverse scroll direction
scroll direction traditional
javascript on scroll down
mouse scroll event up/down example in javascript
javascript change scroll direction

So I am trying to use the JavaScript on scroll to call a function. But I wanted to know if I could detect the direction of the the scroll without using jQuery. If not then are there any workarounds?

I was thinking of just putting a 'to top' button but would like to avoid that if I could.

I have now just tried using this code but it didn't work:

if document.body.scrollTop <= 0 {
    alert ("scrolling down")
} else {
    alert ("scrolling up")

It can be detected by storing the previous scrollTop value and comparing the current scrollTop value with it.

JS :

var lastScrollTop = 0;
// element should be replaced with the actual target element on which you have applied scroll, use window in case of no target element.
element.addEventListener("scroll", function(){ // or window.addEventListener("scroll"....
   var st = window.pageYOffset || document.documentElement.scrollTop; // Credits: ""
   if (st > lastScrollTop){
      // downscroll code
   } else {
      // upscroll code
   lastScrollTop = st <= 0 ? 0 : st; // For Mobile or negative scrolling
}, false);

Simple JS scroll-direction detection, Format on Save. If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting. The accepted answer doesn't really "detect" scrolling up or down. It won't work if the current visible item is really huge. Using onTouchListener is the way to go.

Simple way to catch all scroll events (touch and wheel)

window.onscroll = function(e) {
  // print "false" if direction is down and "true" if up
  console.log(this.oldScroll > this.scrollY);
  this.oldScroll = this.scrollY;

Help with detecting scroll direction with js? : javascript, Hi guys, how can I detect when a user scrolls up, I want to display a navigation bar when the user scrolls up, someone help me please, oh and no jquery just  We may earn a commission for purchases using our links. You can reverse the scrolling direction whether you use a mouse or touchpad on Windows 10, and in this guide, we'll show you how to do it

Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.

var scrollableElement = document.body; //document.getElementById('scrollableElement');

scrollableElement.addEventListener('wheel', checkScrollDirection);

function checkScrollDirection(event) {
  if (checkScrollDirectionIsUp(event)) {
  } else {

function checkScrollDirectionIsUp(event) {
  if (event.wheelDelta) {
    return event.wheelDelta > 0;
  return event.deltaY < 0;


How To Determine The Scroll Direction Using JQuery?, element to detect scroll direction of var el = $(window), // initialize last scroll position lastY = el.scrollTop(); el.on('scroll', function() { // get current  How to detect the scroll direction for a scrollable div , From OnScroll event? I want to add 15 to scrollTop, if user scrolls up and subtract 15, if user scrolls down.

This is an addition to what prateek has answered.There seems to be a glitch in the code in IE so i decided to modify it a bit nothing fancy(just another condition)

$('document').ready(function() {
var lastScrollTop = 0;
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
   else if(st == lastScrollTop)
     //do nothing 
     //In IE this is an important condition because there seems to be some instances where the last scrollTop is equal to the new one
   else {
   lastScrollTop = st;

detect scroll direction - JSFiddle, show hide subnav depending on scroll direction. 4. var position = $(window).​scrollTop();. 5. ​. 6. $(window).scroll(function () {. 7. var scroll = $(window).​scrollTop  Scroll Event. The scroll event behaves oddly in FF (it is fired a lot of times because of the smoothness scrolling) but it works. Note: The scroll event actually is fired when dragging the scroll bar, using cursor keys or mousewheel.

You can try doing this.

function scrollDetect(){
  var lastScroll = 0;

  window.onscroll = function() {
      let currentScroll = document.documentElement.scrollTop || document.body.scrollTop; // Get Current Scroll Value

      if (currentScroll > 0 && lastScroll <= currentScroll){
        lastScroll = currentScroll;
        document.getElementById("scrollLoc").innerHTML = "Scrolling DOWN";
        lastScroll = currentScroll;
        document.getElementById("scrollLoc").innerHTML = "Scrolling UP";



  background: #ffad33;

  background: red;

<div class="cont">
  <div class="item"></div>
  <div class="item red"></div>
  <p id="scrollLoc">0</p>

Detecting scroll distances with vanilla JS, This morning, Sami Keijonen, one of my students, asked me if there was a way to detect how far a user has scrolled (and in what direction)  So I am trying to use the JavaScript on scroll to call a function. But I wanted to know if I could detect the direction of the the scroll without using jQuery. If not then are there any workarounds

How to determine the direction of a jQuery scroll event , With the hep of this method, we can find the mouse scroll direction. Syntax: $(​selector).scrollTop(position). Parameters: This method accepts single parameter​  All code belongs to the poster and no license is enforced. JSFiddle or its authors are not responsible or liable for any loss or damage of any kind during the usage of provided code. Links. Bug tracker Roadmap (vote for features) About Docs Service status

Scroll, Also direction can be detect by some complicated Detect-Assign combo. It means​, we really need Scroll Detection Trigger to the point where using this complicated  I need to detect the direction in that a user scrolls - "up" or "down". Based on the code found in this answer: How can I determine the direction of a jQuery scroll event? I tried to wrap it in a

GasparErrobidart/scroll-direction: Detect scroll direction, Simple library for detecting the scroll vertical direction. LIVE DEMO. Provides a custom event scrollDirectionChange; Automatically adds classes to an element 

  • It's the wheelDelta of the event. Quite not cross-browser. See
  • hmmm not quite what I was looking for but I appreciate your help :P Any other suggestions?
  • It would be safer to initialize lastScrollTop to pageYOffset || scrollTop rather than assuming 0
  • Totally agree !! Thanks @EdBallot. We should initialize the same on window.onload event.
  • @Prateek Thanks for your answer but it isn't working for me... I am trying to 'change scene' in my webapp which is built using Tumult Hype.
  • I have added few comments in my answer, please check it. I guess you are using "element.addEventListener".
  • @Prateek still nothing I'm afraid. Could it be to do with the fact that, I am running it on page load? Here is a screenshot:
  • Welcome to SO, if you add a desctription to your answer, this can be more helpfull to the OP and to others.
  • This is good, but only seems to work for using the scroll wheel
  • Thanks for your hint ... this seems to be connected to IE "Smooth Scrolling" option
  • this is not working fine for me. When I scroll up even upto some certain height it shows down
  • Ok and could I still use this on a website which won't traditionally allow scrolling (i.e. it fits the browser 100% width and height. Thanks