MouseEvent movementX property apparently not supported in internet explorer

后端 未结 1 1763
南旧
南旧 2021-01-14 17:17

I am developing an application that needs to be compatible on IE 9 and above. I am using the movementX property on a MouseEvent object, however this same MouseEvent object d

1条回答
  •  囚心锁ツ
    2021-01-14 17:52

    You will need to replicate it's behaviour by taking the delta between subsequent screenX and screenY values.

    // Store previous screenX/screenY somewhere outside of callback
    var prevX = 0;
    var prevY = 0;
    
    // Callback whenever the mouse moves
    function mousemove(e) {
        var movementX = (prevX ? e.screenX - prevX : 0)
        var movementY = (prevY ? e.screenY - prevY : 0)
    
        prevX = e.screenX;
        prevY = e.screenY;
    }
    

    Note that if you are listening to mousemove to say, figure out if something is being dragged, you should reset prevX and prevY to 0 on mouseup or else you'll get the old screen values!

    0 讨论(0)
提交回复
热议问题