HTML | DOM WheelEvent

The DOM WheelEvent contains the events that happen whenever a scroll is detected. It can be used to find out the scroll direction, amount and mode of scroll. It has the onwheel event that is called whenever scrolling takes place.

Properties:

  • deltaX: It returns the amount of horizontal scroll of the mouse wheel i.e. on the x-axis.
  • deltaY: It returns the amount of vertical scroll of the mouse wheel i.e. on the y-axis.
  • deltaZ: It returns the amount of scroll on the z-axis of the mouse wheel.
  • deltaMode: It returns the unit of measurement that is used for the delta values. This value may be in pixels, lines or pages.

Example: This example displays the scroll properties.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>HTML DOM WheelEvent</title>
      
    <style>
        .area {
            height: 200px;
            width: 200px;
            border: 5px solid;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>HTML | DOM WheelEvent</b>
      
    <p>
        Try scrolling on the element below
        to check the scroll properties.
    </p>
      
    <div class="area" 
        onwheel="isScrolling(event)">
    </div>
      
    <script type="text/javascript">
        function isScrolling(event) {
            console.log("X scroll direction:", event.deltaX);
            console.log("Y scroll direction:", event.deltaY);
            console.log("Z scroll direction:", event.deltaZ);
            console.log("Scroll Mode is:", event.deltaMode);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before scrolling the div element:
    wheel-props-output
  • Console Output after scrolling the div element:
    wheel-props-console

Event Types:

  • onwheel: This event occurs whenever the mouse wheel is scrolled up or down on the element.

Example: This example illustrates the onwheel event.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>onwheel event type</title>
      
    <style>
        .area {
            height: 200px;
            width: 200px;
            border: 5px solid;
        }
    </style>
</head>
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
      
    <b>HTML | DOM WheelEvent</b>
      
    <p>
        Try scrolling on the element below
        to call the onwheel function.
    </p>
      
    <div class="area" 
        onwheel="isScrolling()">
    </div>
      
    <script type="text/javascript">
        function isScrolling() {
            console.log('You are scrolling"
                    + " in the element.')
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before scrolling the div element:
    onwheel-output
  • Console output after scrolling the div element:
    onwheel-console

Supported Browsers: The browser supported by HTML DOM WheelEvent are listed below:

  • Google Chrome 31
  • Internet Explorer 9
  • Firefox 17
  • Apple Safari
  • Opera 18


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.