Worldscope

JavaScript onmousewheel event

Palavras-chave:

Publicado em: 03/09/2025

JavaScript onmousewheel Event

The onmousewheel event in JavaScript allows you to detect and respond to mouse wheel movements over an HTML element. This article explores how to effectively use this event to create custom scrolling behaviors and interactions on your web pages.

Fundamental Concepts / Prerequisites

To understand the onmousewheel event, you should have a basic understanding of the following:

  • HTML: Structure of web pages.
  • CSS: Styling web pages, including positioning and overflow properties.
  • JavaScript: Basic JavaScript syntax, event handling, and DOM manipulation.

Familiarity with the Document Object Model (DOM) is crucial as you will be attaching the event listener to DOM elements.

Core Implementation

Here's an example of how to use the onmousewheel event to create a custom scrolling effect:





Mousewheel Event Example




This is some content to scroll through using the mousewheel. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

More content to scroll... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Even more content... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Code Explanation

The code first retrieves the HTML element with the ID "scrollableDiv". An event listener is then added to this element for the 'wheel' event, which is the standard event for mouse wheel actions, replacing the older `onmousewheel` event.

Inside the event handler:

  • event.preventDefault() prevents the default browser scrolling behavior. This allows you to implement your custom scrolling.
  • event.deltaY, event.detail, and event.wheelDelta are used to determine the scroll direction and amount. Browsers use different properties to provide this information, so we check them all for compatibility.
  • scrollAmount is calculated based on the scroll direction. If the scroll delta is positive, the element scrolls down; otherwise, it scrolls up. The value (50 in this example) controls the scroll speed.
  • scrollableDiv.scrollTop += scrollAmount actually scrolls the element by updating its scrollTop property.

Complexity Analysis

The time complexity of the event handler is O(1) because it performs a fixed number of operations regardless of the size of the HTML element or the content it contains. Attaching the event listener initially also typically takes O(1) time, assuming efficient event registration mechanisms in the browser.

The space complexity is also O(1) as the code uses a fixed amount of memory to store variables and event handlers, independent of the input size.

Alternative Approaches

An alternative approach is to use a library like Smoothscroll or similar JavaScript libraries that provide smoother and more customizable scrolling animations. These libraries often handle cross-browser compatibility and offer features like easing functions for more visually appealing scrolling effects. However, using a library adds a dependency to your project and might increase its overall size.

Conclusion

The wheel event (formerly onmousewheel) provides a powerful way to capture and respond to mouse wheel movements in JavaScript. By preventing default scrolling and manipulating the scrollTop property, you can create custom scrolling behaviors and interactions on your web pages. Remember to test across different browsers to ensure compatibility, and consider using libraries for more advanced scrolling features.