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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With