JavaScript onwheel Event

Why onwheel ?

The JavaScript onwheel feature works when the mouse wheel is rolled up or down on an element. The onwheel event also occurs when the user scrolls or zooms an item using a touchpad (such as a laptop’s “mouse”).

Syntax:

HTML Attribute<element onmousewheel = “handler(event)“>
Event Propertyobject.onmousewheel = handler;
attachEvent Methodobject.attachEvent(“onmousewheel“, handler)
addEventListener Methodobject.addEventListener(“mousewheel“, handler, useCapture)

onwheel Examples

Example 1

onwheel example 1
<body>

    <div class="container">
        <h2>onwheel Event</h2>
        <div id="box" onwheel="wheelFunction();"></div>
    </div>

    <script>

        function wheelFunction() {
            let box = document.getElementById("box")
            box.style.transform = "scale(2)"
            box.style.backgroundColor = "#b63434"
        }

    </script>
</body>

Example 2

onwheel example 2
<body>

    <div class="container">
        <h2>onwheel Event</h2>
        <div id="box"></div>
    </div>

    <script>

        document.getElementById("box").onwheel = function () {
            wheelFunction();
        };

        function wheelFunction() {
            let box = document.getElementById("box")
            box.style.transform = "scale(2)"
            box.style.backgroundColor = "#b63434"
        }

    </script>
</body>

Example 3

onwheel example 3
<body>

    <div class="container">
        <h2>onwheel Event</h2>
        <div id="box"></div>
    </div>

    <script>

        document.getElementById("box").addEventListener("wheel", wheelFunction);

        function wheelFunction() {
            let box = document.getElementById("box")
            box.style.transform = "scale(2)"
            box.style.backgroundColor = "#b63434"
        }

    </script>
</body>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started