为了账号安全,请及时绑定邮箱和手机立即绑定

作为前端小白的我,试着用面向对象的方式写了一个拖拽,麻烦大神指正下哪里写得不好。

作为前端小白的我,试着用面向对象的方式写了一个拖拽,麻烦大神指正下哪里写得不好。

慕田峪8701529 2017-11-26 10:47:50
Drag.prototype = {    mousedown: function(dom) {        var that = this;        dom.onmousedown=function(e) {            var ev=e || event;            that.disX=ev.clientX-dom.offsetLeft;            that.disY=ev.clientY-dom.offsetTop;            document.onmousemove=function(e){                var ev=e||event;                dom.style.left=ev.clientX-that.disX+'px';                dom.style.top=ev.clientY-that.disY+'px';            };            that.mouseup();        };    },    mouseup: function () {        document.onmouseup=function () {            document.onmousemove=null;            document.onmouseup=null;        }    },    init:function (dom){        this.mousedown(dom);    }};var dd = new Drag();var oDiv= document.getElementById('div1');dd.init(oDiv);
查看完整描述

1 回答

?
Stardust1001

TA贡献261条经验 获得超78个赞

有点觉得呢,你的代码写的似乎不怎么整洁,像函数里的函数,或者闭包这种东西也不要乱用,起码也要学懂了,不然会有问题的

查看完整回答
反对 回复 2017-11-27
  • 慕田峪8701529
    慕田峪8701529
    感谢~ 麻烦您能告诉一下,我哪块的写法算闭包么。一直没弄懂闭包到底是怎么回事,网上有的说是函数里包含着函数就是闭包,有的又说是子函数能访问父函数的变量才算是闭包,我都晕了,不知道相信哪种说法
  • Stardust1001
    Stardust1001
    其实我一般不这样写,都是写在外面,比较清楚,当然,我的水平比较低。像那些库,经常用到闭包的
  • 1 回答
  • 0 关注
  • 1042 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信