Event自定义事件

Event自定义事件

//index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            top: 0;
            left: 0;
        } 
    </style>
</head>
<body>
    <div id="box"></div>
</body>
</html>
<script src="./event.js"></script>
<script>
    class Drag extends Event{
        constructor(el){
            super();
            this.el = el;  //元素
            this.startPoint = {}; //起始鼠标位置
            this.startEl = {}; //起始元素位置
            let move = e => {
                this.move(e);
            }
            let end = (e) => {
                document.removeEventListener('mousemove',move);
                document.removeEventListener('mouseup',end);
                this.end(e); //触发事件
            }
            document.addEventListener('mousedown',e => {
                this.start(e);
                this.startPoint = {
                    x : e.clientX,
                    y : e.clientY
                }
                this.startEl = {
                    x : this.el.offsetLeft,
                    y : this.el.offsetTop
                }
                document.addEventListener('mousemove',move);
                document.addEventListener('mouseup',end)
            })
        }
        move(e){
            let nowPoint = {
                x : e.clientX,
                y : e.clientY
            }
            let dix = {
                x : nowPoint.x - this.startPoint.x,
                y : nowPoint.y - this.startPoint.y
            } 
            this.el.style.left = this.startEl.x + dix.x + 'px';
            this.el.style.top = this.startEl.y + dix.y + 'px';
        }
        start(e){
            this.trigger('dragStart',e,this.el)
        }
        end(e){
            this.trigger('dragEnd',e,this.el)
        }
    }

    var box = document.getElementById('box');
    var drag = new Drag(box);
    drag.on('dragStart',function(e){
        this.style.background = 'yellow';
    })
    drag.on('dragEnd',function(e){
        this.style.background = 'red';
    })

    
</script>



 

//event.js文件

class Event{
    constructor(){
        this.handlers = {}//记录所有的事件和处理函数
    }
    /**
     * 添加事件监听
     *@param type : 事件类型
      @param hander : 处理函数
     */
    on(type,hander,once = false){
        if(!this.handlers[type]){ //判断是否添加了事件函数
            this.handlers[type] = []
        }
        if(!this.handlers[type].includes(hander)){ //查看是否重复添加事件函数
            this.handlers[type].push(hander);
            hander.once = once; //给事件函数添加一个once属性
        }
        
    }
    /**
     * 取消事件监听
     * @param {*} type 
     * @param {*} hander 
     */
    off(type,hander){
        if(this.handlers[type]){
            if(hander === undefined){
                this.handlers[type] = [];
            }else{
                this.handlers[type] = this.handlers[type].filter(item => {
                    return item != hander
                })
            }
        }
    }
    /**
     * 触发事件监听
     * @param {*} type 
     * @param {*} e 
     * @param {*} that 
     */
    trigger(type,e,that){
        if(this.handlers[type]){
            this.handlers[type].forEach(f => {
                f.call(that,e);
                if(f.once){
                    this.off(type,f); //根据once属性取消事件
                }
            })
        } 
    }
    /**
     * 执行一次
     * @param {*} type 
     * @param {*} hander 
     */
    once(type,hander){
        this.on(type,hander,true); //true控制执行一次
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值