HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML onwheel Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onwheel
attribute is a powerful feature in HTML that allows developers to define JavaScript code that will be executed when the user rotates the mouse wheel over a specified element.
This attribute is commonly used to add dynamic behavior to web pages, providing a responsive and interactive user experience.
🎯 Purpose of onwheel
The primary purpose of the onwheel
attribute is to enable developers to capture and respond to mouse wheel events.
By attaching this attribute to an HTML element, you can execute custom JavaScript code when the user scrolls using the mouse wheel, allowing for a wide range of interactive possibilities.
💎 Values
The onwheel
attribute can be assigned values that represent JavaScript code to be executed when the wheel event occurs.
Common values include function calls and inline JavaScript code. Here's a basic example:
<div onwheel="myFunction(event)">Scroll over me!</div>
🧠 How it Works
In this example, the onwheel
attribute is set to call the myFunction JavaScript function when the user scrolls over the specified <div> element.
📄 Example
Let's explore a simple example of using the onwheel
attribute to change the font size of a text element dynamically:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>onwheel Example</title>
<style>
#scrollText {
font-size: 16px;
transition: font-size 0.5s ease;
}
</style>
</head>
<body>
<p id="scrollText" onwheel="changeFontSize(event)">Scroll to change font size!</p>
<script>
function changeFontSize(event) {
const scrollText = document.getElementById("scrollText");
event.deltaY > 0 ? scrollText.style.fontSize = "18px" : scrollText.style.fontSize = "14px";
}
</script>
</body>
</html>
🧠 How it Works
In this example, the onwheel
attribute is applied to a <p> element, triggering the changeFontSize JavaScript function when the user scrolls. The font size of the text is dynamically adjusted based on the direction of the scroll.
🔄 Dynamic Values with JavaScript
You can also dynamically set the onwheel
attribute using JavaScript. This allows you to adapt event handling based on changing conditions or user interactions. Here's a brief example:
<script>
// Dynamically set onwheel event for an element
document.getElementById("dynamicElement").onwheel = function(event) {
// Your dynamic event handling code here
console.log("Mouse wheel event detected!");
};
</script>
🧠 How it Works
In this script, the onwheel
attribute is dynamically set for an element with the id dynamicElement. Adjust the event handling code as needed for your specific requirements.
🏆 Best Practices
- Use the
onwheel
attribute judiciously to enhance user interactions without overwhelming the user experience. - Consider accessibility aspects when implementing wheel event handling, as not all users interact with a webpage using a mouse.
- Test your code across different browsers to ensure consistent behavior.
🎉 Conclusion
The onwheel
attribute is a valuable tool for creating dynamic and responsive web pages by allowing developers to capture and respond to mouse wheel events.
By leveraging this attribute, you can enhance the interactivity of your web content and provide a more engaging user experience.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML onwheel Attribute), please comment here. I will help you immediately.