Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  63] [ 2]  / answers: 1 / hits: 16665  / 10 Years ago, tue, december 16, 2014, 12:00:00

I've searched on Stackoverflow but can't seem to find a satisfactory answer to this question. Basically I'd like to know if the scroll was done via mousewheel or the browser scrollbar.


More From » jquery

 Answers
34

Something like this might work for you but it is not the best solution.



If the a wheel event occurs right before the scroll event, then the scroll is done with the wheel otherwise it is done with using something else then the wheel. There is a slight time difference between both events that are triggered thats why I use a threshold currTime - lastWheelTime > 30.





$('.test').on('scroll wheel DOMMouseScroll mousewheel', function(e) {
var lastWheelTime,
currTime = (new Date()).getTime();

if( e.type === 'scroll' ) {
lastWheelTime = $(this).data().lastWheelTime || 0;

if( currTime - lastWheelTime > 30 ) {
$('.info').text('no wheel');
} else {
$('.info').text('with wheel');
}

} else {
$(this).data().lastWheelTime = (new Date()).getTime();
}
});

.test {
width: 200px;
height: 300px;
border: 1px solid red;
overflow: auto;
}

.inner {
height: 600px;
}

<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<div class=info></div>
<div class=test>
<div class=inner></div>
</div>




[#68466] Saturday, December 13, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marinalyssak

Total Points: 637
Total Questions: 101
Total Answers: 94

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;