What substitute should we use for layerX/layerY since they are deprecated in webkit?

前端 未结 3 1214
野趣味
野趣味 2020-12-16 11:15

In chrome canary, layerX and layerY are deprecated, but what should we use instead ?

I\'ve find offsetX but it doesn\'t work with Firefox. So to get layerX without w

3条回答
  •  感情败类
    2020-12-16 11:40

    The only reasonably cross-browser ways to detect mouse position are clientX/clientY (relative to window), screenX/screenY (relative to entire screen) and pageX/pageY (relative to document, but not supported in IE8 and below).

    Quirksmode suggests this for standardising to a relative-to-document value:

    function doSomething(e) {
        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)     {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY)     {
            posx = e.clientX + document.body.scrollLeft
                + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                + document.documentElement.scrollTop;
        }
        // posx and posy contain the mouse position relative to the document
        // Do something with this information
    }
    

    Then you could use this to work out its position relative to your element.

    Horrible, I know, but the internet's a horrible place.

提交回复
热议问题