我是一个使用javascript和jquery的初学者,我还不太了解。我正在尝试编写一些代码,如果鼠标滚轮朝着与大多数或所有程序兼容的特定方向移动,这些代码将调用不同的函数。有人能给我演示一下如何使用一段示例代码来实现这一点吗?
这就是我需要的

// JavaScript Document
...
...
...
...
function wheelMove(){


    // NOTE: I'd like the event of scrolling not to fire
    // (i.e return false; or something like it)


    // if variables need to be set, they can go here
    //...
    //...
    //...
    if (// some code to detect if the wheel is moving towards the right){

        var sRight = // some code to detect the speed in this direction;
        moveRight(sRight); // calls a function when it moves right;

    }
    if (// some code to detect if the wheel is moving towards the left){

        var sLeft = // some code to detect the speed in this direction;
        moveLeft(sLeft); // calls a function when it moves left;

    }
    if (// some code to detect if the wheel is moving towards the up){

        var sUp = // some code to detect the speed in this direction;
        moveUp(sUp); // calls a function when it moves up;

    }
    if (// some code to detect if the wheel is moving towards the down){

        var sDown = // some code to detect the speed in this direction;
        moveDown(sDown); // calls a function when it moves down;

    }
}

最佳答案

下面是一个例子(这里是jsfiddle.com):

$( '#scrollCatcher' ).on( 'mousewheel', function ( event ) {

    // crude check to see events are supported
    if ( typeof event.originalEvent.wheelDeltaX === 'undefined'
        || typeof event.originalEvent.wheelDeltaY === 'undefined' ) {
        console.log( "could not find mouse deltas" );
        return;
    }

    var deltaX = event.originalEvent.wheelDeltaX;
    var deltaY = event.originalEvent.wheelDeltaY;

    var scrolledLeft = deltaX < 0;
    var scrolledRight = deltaX > 0;
    var scrolledUp = deltaY < 0;
    var scrolledDown = deltaY > 0;

    clearDisplay();

    if ( scrolledLeft ) { display( 'scrolled left' ); }
    if ( scrolledRight ) { display( 'scrolled right' ); }
    if ( scrolledUp ) { display( 'scrolled up' ); }
    if ( scrolledDown ) { display( 'scrolled down' ); }
});

function clearDisplay () {
    $( '#scrollCatcher' ).text( '' );
}

function display( message ) {
    $( '#scrollCatcher' ).text( $( '#scrollCatcher' ).text() + message + ' ' );
}

解释一下deltaXdeltaY是自上次事件以来滚轮所行驶的距离。在有display的地方,可以用自己的代码替换它。例如:
if ( scrolledLeft ) { moveLeft( deltaX ) }

10-08 18:28