event.movementX在IE11中无法正常工作,如果我在mousedown事件中使用mousemove事件。

huangapple go评论71阅读模式
英文:

event.movementX is not working in IE11 if i use mousemove event inside mousedown event

问题

在以下代码中,我试图传递鼠标坐标以进行列调整大小。在Chrome中可以正常工作,但在IE11中无法正常工作,因为IE11中event.movementX始终为未定义。有人能提供解决方案吗:

private mouseDown(): void {
    document.addEventListener("mouseup", this.mouseUp);
    document.addEventListener("mousemove", this.mouseMove, true);
}

private mouseMove(event: MouseEvent): void {
    if (this.props.resizing) {
        this.props.resizing(this.props.index, event.movementX);
    }
}
英文:

In the following code I am trying to pass the mouse coordinates for column resizing. It is working in chrome, but not in IE11, event.movementX is always undefined in IE11, can anyone suggest the solution for this:

private mouseDown(): void {
    document.addEventListener("mouseup", this.mouseUp);
    document.addEventListener("mousemove", this.mouseMove, true);
}

private mouseMove(event: MouseEvent): void {
    if (this.props.resizing) {
        this.props.resizing(this.props.index, event.movementX);
    }
}

答案1

得分: 0

如MouseEvent.movementX在IE11中不受支持,您需要以以下旧方式处理它:

<html>
<body>
<p id="log">移动鼠标。</p>
</body>
<script>
    function logMovement(event) {
      if(event.movementX){
          // 在Chrome 79中有效
          log.insertAdjacentHTML('afterbegin', '移动: ' + event.movementX + ', ' + event.movementY + '<br>');
      }else{
          // 在IE11中有效
          log.insertAdjacentHTML('afterbegin', '屏幕: ' + event.screenX + ',' + event.screenY + '<br>');
      }
    }

    const log = document.getElementById('log');
    document.addEventListener('mousemove', logMovement);
</script>
</html>

此代码示例已在IE11和Chrome 79上进行了测试,您可以在jsbin上尝试它:https://jsbin.com/kexuzuc/edit。还请注意,像${var}这样的语法元素以及许多其他功能在IE11中不受支持。

英文:

As MouseEvent.movementX is not supported by IE11 you need to handle it in an old fashion way like in the following:

&lt;html&gt;
&lt;body&gt;
&lt;p id=&quot;log&quot;&gt;Move your mouse around.&lt;/p&gt;
&lt;/body&gt;
&lt;script&gt;
    function logMovement(event) {
      if(event.movementX){
          //works with Chrome 79
          log.insertAdjacentHTML(&#39;afterbegin&#39;, &#39;movement: ${event.movementX}, ${event.movementY}&lt;br&gt;&#39;); 
      }else{
          //works with IE11 
          log.insertAdjacentHTML(&#39;afterbegin&#39;, &#39;screen: &#39; + event.screenX +&#39;,&#39; + event.screenY + &#39;&lt;br&gt;&#39;); 
      } 
    }

    const log = document.getElementById(&#39;log&#39;);
    document.addEventListener(&#39;mousemove&#39;, logMovement);
&lt;/script&gt;
&lt;/html&gt;

This code sample was tested on IE11 and Chrome 79 you can play with it in jsbin here: https://jsbin.com/kexuzuc/edit. Also note that syntaxic element like ${var} and many other feature are not supported by IE11.

huangapple
  • 本文由 发表于 2020年1月6日 15:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定