<!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;
}
}
}
观察者模式(ES6)
最新推荐文章于 2022-12-30 08:58:36 发布