首页 > 其他 > 详细

16-1 ECMA5与ECMA6的函数定义

时间:2018-07-06 22:07:25      阅读:112      评论:0      收藏:0      [点我收藏+]
ECMA5:

function Drag(id){
    this.ele = document.getElementById(id);
    var that = this;
    this.ele.onmousedown = function(evt){
        that.fnDown(evt);
    }
    this.fnDown = function(evt){
        var e = evt || window.event;
        this.disX = e.offsetX;
        this.disY = e.offsetY;
        var that = this;
        document.onmousemove = function(evt){
            that.fnMove(evt);
        }
        document.onmouseup = this.fnUp;
    }
    this.fnMove = function(evt){
        var e = evt || window.event;
        this.ele.style.left = e.pageX - this.disX + ‘px‘;
        this.ele.style.top = e.pageY - this.disY + ‘px‘;
    }
    this.fnUp = function(){
        document.onmousemove = null;
    }
}



ECMA6:

class Drag{
    constructor(id){
        //属性
        this.ele = document.getElementById(id);
        var that = this;
        this.ele.onmousedown = function(evt){
            that.fnDown(evt);
        }
    }
    fnDown(evt){
        var e = evt || window.event;
        this.disX = e.offsetX;
        this.disY = e.offsetY;
        var that = this;
        document.onmousemove = function(evt){
            that.fnMove(evt);
        }
        document.onmouseup = this.fnUp;
    }
    fnMove(evt){
        var e = evt || window.event;
        this.ele.style.left = e.pageX - this.disX + ‘px‘;
        this.ele.style.top = e.pageY - this.disY + ‘px‘;
    }
    fnUp(){
        document.onmousemove = null;
    }
}

16-1 ECMA5与ECMA6的函数定义

原文:https://www.cnblogs.com/zhongchao666/p/9275566.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!