Skip to content
Related Articles
Open in App
Not now

Related Articles

How to determine the direction of a jQuery scroll event?

Improve Article
Save Article
  • Last Updated : 16 Sep, 2022
Improve Article
Save Article

The scrollTop() method in jQuery is used to set or return the vertical scrollbar position for the selected elements. With the help of this method, we can find the mouse scroll direction. Syntax:


Parameters: This method accepts single parameter position which is optional. It is used to specify the vertical scrollbar position in pixels. Return Value: It returns the vertical position of the scrollbar of selected element. Example: 


<!DOCTYPE html>
        Determine the direction of scroll event
    <script src=
        html, body {
            height: 300%
        div {
            position: fixed;
            padding-left: 10px;
            padding-top: 30px;
            height: 10%;
            width: 35%;
            background: lightgrey;
            font-weight: bold;
            border: 2px solid green;
    <!-- Script to determine the direction
    of a jQuery scroll event -->
        var position = $(window).scrollTop();
        $(window).scroll(function() {
            var scroll = $(window).scrollTop();
            if (scroll > position) {
                $('div').text('Scrolling Down Scripts');
            } else {
                $('div').text('Scrolling Up Scripts');
            position = scroll;


  • Scrolling in up direction:
  • Scrolling in down direction:

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!