Skip to content

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: