iPad相当于onmousemove?

时间:2013-07-09 04:16:37

标签: javascript events mobile-safari compatibility

我正在尝试让一个网站在我的iPad上运行,但唯一不起作用的是onmousemove方法。当用户将手指放在屏幕上时,我希望onmousemove能够被触发。

1 个答案:

答案 0 :(得分:1)

启用触摸的设备中的mousemove事件相当于touchmove事件。

所以你可以像这样绑定事件:

var addEvent = (function () {
    var tester, allHandlers, attachGenerator, addFunc, removeFunc;

    tester = document.createElement("div");
    allHandlers = [];

    attachGenerator = function (el, cb) {
        var ret;
        ret = function (e) {
            e = e || window.event;
            cb.call(el, e);
        };
        return ret;
    };

    if (tester.addEventListener) {
        addFunc = function (element, eventName, callback) {
            allHandlers.push({
                element: element,
                eventName: eventName,
                callback: callback
            });
            element.addEventListener(eventName, callback, false);
        };
        removeFunc = function (element, eventName, callback) {
            element.removeEventListener(eventName, callback);
        };
    } else if (tester.attachEvent) {
        addFunc = function (element, eventName, callback) {
            var finalCallback;
            finalCallback = attachGenerator(element, callback);
            allHandlers.push({
                element: element,
                eventName: eventName,
                callback: finalCallback
            });
            element.attachEvent("on" + eventName, finalCallback);
        };
        removeFunc = function (element, eventName, callback) {
            element.detachEvent("on" + eventName, callback);
        };
    }

    addFunc(window, "unload", function () {
        var i, j, cur;
        // Don't remove this unload handler
        for (i = 1, j = allHandlers.length; i < j; i++) {
            cur = allHandlers[i];
            removeFunc(cur.element, cur.eventName, cur.callback);
        }
    });

    return addFunc;
}());

function loadHandler() {
    addEvent(document, "touchmove", touchmoveHandler);
}

function touchmoveHandler() {
    // this callback should fire for every pixel (or whatever unit they may use) the touch moves
}

addEvent(window, "load", loadHandler);

addEvent函数只是一个通用事件绑定函数,可以帮助在旧的IE中工作......如果你绑定到touch个事件,可能就没必要了)

请注意,我原来的通用addEvent函数并不完美:http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html,但我修改了以前用来更好地支持this处理程序中attachEvent的可用性{{1}必须使用,并在窗口的unload事件发生时删除所有处理程序。

参考文献: