Open In App

HTML | DOM WheelEvent

Last Updated : 16 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.




<!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>


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.




<!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>


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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads