首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onMouseDown for file的函数

onMouseDown for file的函数
EN

Stack Overflow用户
提问于 2020-02-06 17:23:23
回答 1查看 38关注 0票数 0

我的输入类型是文件。我正在为文件顺序定义onMouseDown函数,更改文件预览的位置。我得到以下错误: TypeError:无法设置未定义的属性'left‘

代码语言:javascript
复制
 <input 
     type="file"
     className="file"
     id="file-browser-input"
     name="file-browser-input"
     ref ={input=>this.fileInput=input}
     onDragOver={(e)=>{
     e.preventDefault();
     e.stopPropagation();
     }
    }
     onDrop={this.onFileLoad.bind(this)
     onChange={this.onFileLoad.bind(this)}
     onMouseDown={this.catchItem.bind(this)} 
/>

catchItem(e) {

    this.style.left = e.pageX - this.clientWidth / 2 + "px";
    this.style.top = e.pageY - this.clientHeight / 2 + "px";
    this.onmousemove = function(e) {
    this.style.left = e.pageX - this.clientWidth / 2 + "px";
    this.style.top = e.pageY - this.clientHeight / 2 + "px";
    }
    this.onmouseup = function() {
    this.onmousemove = null; // onmouseup event [ redirect mousemove event signal to null instead of the 
    drag-able element]
        }
      }
EN

回答 1

Stack Overflow用户

发布于 2020-02-06 17:32:18

this.style似乎是未定义的

代码语言:javascript
复制
catchItem(e) {
    this.style.left = e.pageX - this.clientWidth / 2 + "px"; // either this one
    this.style.top = e.pageY - this.clientHeight / 2 + "px";
    this.onmousemove = function(e) {
       this.style.left = e.pageX - this.clientWidth / 2 + "px"; // or this one
       this.style.top = e.pageY - this.clientHeight / 2 + "px";
    }
    ...
}

因为您没有将this.onmousemove的上下文绑定到类的上下文,所以我怀疑它是第二个。

在这种情况下,您不使用箭头函数的原因是什么?

修复:

代码语言:javascript
复制
catchItem(e) {

    this.onmousemove = (e) => {
       ...
    }

    or

    this.onmousemove = function(e){
       ...
    }
    this.onmousemove.bind(this);


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60091505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档