var direction = ""
var mousemovemethod = function (e) {
    var oldx = 0;
    if (e.movementX < oldx) {
        direction = "left"
    } else if (e.movementX > oldx) {
        direction = "right"
    }
    oldx = e.pageX;
}

这就是我检测鼠标方向的方式,效果很好,但仅在Chrome上有效,我如何使其与其他浏览器(Firefox,Opera和至少ie8 +或ie9 +)兼容。
请不要jQuery。

最佳答案

坚持使用pageX并在更高的范围内定义oldx,否则它始终为零

var direction = "",
    oldx = 0,
    mousemovemethod = function (e) {

        if (e.pageX < oldx) {
            direction = "left"
        } else if (e.pageX > oldx) {
            direction = "right"
        }

        oldx = e.pageX;

}

FIDDLE

关于javascript - 检测鼠标方向-JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24294452/

10-13 03:10