Home >Web Front-end >JS Tutorial >How Can I Capture Mouse Wheel Events in jQuery?
Capturing Mouse Wheel Events in jQuery
jQuery offers an effective method to capture specific mouse wheel events, distinct from scroll events. This article will guide you through the process of achieving this functionality.
The mouse wheel event allows you to detect precise wheel movements without relying on scrolling actions. To initiate event handling, you can utilize the bind() function as follows:
<code class="javascript">$(document).ready(function(){ $('#foo').bind('mousewheel', function(e){ if(e.originalEvent.wheelDelta /120 > 0) { console.log('scrolling up !'); } else{ console.log('scrolling down !'); } }); });</code>
In this example, the mousewheel event listener is attached to an element with the ID foo. The e.originalEvent.wheelDelta property designates the direction of the wheel movement, where positive values represent upward scrolling and negative values denote downward scrolling.
This approach provides a direct and efficient way to monitor mouse wheel interactions, enabling you to respond to precise user inputs in real-time.
The above is the detailed content of How Can I Capture Mouse Wheel Events in jQuery?. For more information, please follow other related articles on the PHP Chinese website!