3

I want to make a div scroll for the number of times the wheel spins rather than the length the wheel was spun. Is this possible?

E.g. If the user spin the mouse wheel x length I need to pass a value as 10. Also if a user spin the mouse wheel x/4 length I still need to pass the value 10.

example:: something like this - Each time it spins increase the value by 1 only.

var i = 0;
$("#divD").on('scroll', function() {
  i++;
});
Becky
  • 5,467
  • 9
  • 40
  • 73
  • 2
    It may not be possible, but [here is an article that may help](http://tech.pro/tutorial/705/javascript-tutorial-the-scroll-wheel). There's also [another stack overlflow question](http://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers) which may help you. – evolutionxbox Jun 05 '15 at 14:29

2 Answers2

2

Something to this effect might work for your purposes. Try binding the mousewheel.

var i = 0;
$(document).bind('mousewheel', function(){
    i++;
    $('div').text(i);
});

DEMO

Additionally, if you wanted to take it a step further you could determine the scroll direction.

var i = 0;
var u = 0;
var d = 0;
$(document).bind('mousewheel', function(e){
    i++;
    $('#all').text(i);
    if(e.originalEvent.wheelDelta > 0) {
        u++;
        $('#up').text(u);
    } else {
        d++;
        $('#down').text(d);
    }
});

DEMO

wrxsti
  • 3,434
  • 1
  • 18
  • 30
2

If you want to differentiate between separate continuous scroll and not consider the duration of the scroll you can unbind mousewheel and rebind after a delay. Expanding on @wrxsti solution, it would look like this:

var i = 0;
var u = 0;
var d = 0;
$(document).bind('mousewheel', updateValues)

function updateValues(e){
    i++;
    $('#all').text(i);
    if(e.originalEvent.wheelDelta > 0) {
        u++;
        $('#up').text(u);
    } else {
        d++;
        $('#down').text(d);
    }
    $(document).unbind('mousewheel');
   setTimeout(function(){$(document).bind('mousewheel', updateValues)}, 500);
};

http://jsfiddle.net/g6enbeq2/2/

Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
  • 1
    Not working with kinetic scrolling (all Apple laptops trackpads and magic mouses). You have to be very careful when trying to deal with the mouse wheel... – Alvaro Jun 09 '15 at 09:25