user2772219
user2772219

Reputation: 129

Add 1 to variable if scrolldown and subtract 1 to variable if scrollup

The title pretty much sums it up.

How do I add 1 to a variable if the user scrolls down, and subtract 1 to the same variable if the user scrolls up.

The variable must start with 0.

EDIT: For clarification, it must constantly add/subtract 1 if the user is scrolling up/down, and stop adding/subtracting if the user stops.

Upvotes: 2

Views: 3220

Answers (1)

Rusty Fausak
Rusty Fausak

Reputation: 7525

var x = 0;
$(document).on('mousewheel DOMMouseScroll', function (event) {
    if (event.type == 'mousewheel') {
        // scroll 
        if (event.originalEvent.wheelDelta > 0) {
            // scroll down
            x++;
        }
        else {
            // scroll up
            x--;
        }
    }
    else if (event.type == 'DOMMouseScroll') {
        if (event.originalEvent.detail > 0) {
            // scroll down
            x++;
        }
        else {
            // scroll up
            x--;
        }
    }
});

See jsFiddle

Upvotes: 4

Related Questions