观察者模式(ES6)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div0{
            width: 538px;
            height: 540px;
            position: relative;
            overflow: hidden;
        }
        input{
            width: 420px;
            height: 40px;
            font-size: 30px;
            float: left;
        }
        button{
            float: right;
            width: 80px;
            height: 46px;
            background-color: #d34949;
            border: none;
            border-radius: 5px;
        }
        button:focus {
            border: 0 none;
            outline: none;
        }
    </style>
</head>
<body>
    <div class="div0">
        <video src="./test3.mp4" controls autoplay muted></video>
        <br>
        <input type="text">
        <button type="submit">发送</button>
    </div>
    <script type="module">
        import Bullet from "./ServiceAMD/Bullet.js";

        var input=document.querySelector("input");
        document.addEventListener("keyup",keyHandler);
        var bn=document.querySelector("button");
        bn.addEventListener("click",clickHandler);
        function keyHandler(e){
            
            if(e.keyCode!==13) return;
            // 如果是,生成弹幕,并设置格式以及显示内容
            if(input.value.trim().length===0) return;
            var bullet=new Bullet(input.value);
            console.log(bullet);
            bullet.appendTo(".div0");
            input.value="";
        }
        function clickHandler(e){
            if(e.type==="click") {
                var input=document.querySelector("input");
                if(input.value.trim().length===0) return;
                var bullet=new Bullet(input.value);
                bullet.appendTo(".div0");
                input.value="";
            }
        }
    </script>
</body>
</html>
```javascript

Bullet.js

import TimeManager from "./TimeManager.js";
export default class Bullet{
    elem;
    rect;
    x;
    speed=2;
    width;
    constructor(txt){
        this.elem=this.createElem(txt);
    }
    createElem(txt){
        if(this.elem){
            return this.elem;
        } 
        var div=document.createElement("div");
        // 设置div样式
        Object.assign(div.style,{
            whiteSpace:"nowrap",
            position:"absolute",
            color:"rgba("+Math.floor(Math.random()*256)+","
            +Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")",
        })
        div.textContent=txt;
        return div;
    }
    appendTo(parent){
        if(typeof parent==="string") parent=document.querySelector(parent);
        parent.appendChild(this.elem);
        this.rect=parent.getBoundingClientRect();
        Object.assign(this.elem.style,{
            top:Math.random()*this.rect.height/4+"px",
            left:this.rect.width+"px",
        });
        this.x=this.rect.width;
        this.width=this.elem.offsetWidth;
        TimeManager.instance.add(this);
    }
    update(){
        if(!this.width) return;
        this.x-=this.speed;
        this.elem.style.left=this.x+"px";
        if(this.x<-this.width){
            TimeManager.instance.remove(this);
            this.elem.remove();
            this.elem=null;
        }
    }
}
TimeManager.js
import TimeManager from "./TimeManager.js";
export default class Bullet{
    elem;
    rect;
    x;
    speed=2;
    width;
    constructor(txt){
        this.elem=this.createElem(txt);
    }
    createElem(txt){
        if(this.elem){
            // Object.assign(this.elem.style,{
            //     color:"rgba("+Math.floor(Math.random()*256)+","
            // +Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")",
            // });
            return this.elem;
        } 
        var div=document.createElement("div");
        // 设置div样式
        Object.assign(div.style,{
            whiteSpace:"nowrap",
            position:"absolute",
            color:"rgba("+Math.floor(Math.random()*256)+","
            +Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")",
        })
        div.textContent=txt;
        return div;
    }
    appendTo(parent){
        if(typeof parent==="string") parent=document.querySelector(parent);
        parent.appendChild(this.elem);
        this.rect=parent.getBoundingClientRect();
        Object.assign(this.elem.style,{
            top:Math.random()*this.rect.height/4+"px",
            left:this.rect.width+"px",
        });
        this.x=this.rect.width;
        this.width=this.elem.offsetWidth;
        TimeManager.instance.add(this);
    }
    update(){
        if(!this.width) return;
        this.x-=this.speed;
        this.elem.style.left=this.x+"px";
        if(this.x<-this.width){
            TimeManager.instance.remove(this);
            this.elem.remove();
            this.elem=null;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值