js觀察者模式的彈幕案例
本文實(shí)例為大家分享了js觀察者模式的彈幕案例代碼,供大家參考,具體內(nèi)容如下
觀察者模式的彈幕案例
上代碼

彈幕.html:
<!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>
input{
width:1208px;
height: 38px;
font-size: 30px;
position: absolute;
}
.div0{
width:1208px;
height: 768px;
position: relative;
overflow: hidden;
}
video{
width:1208px;
height: 720px;
}
</style>
</head>
<body>
<div class="div0">
<video src="./y2mate.com - nylonjapan11_wjhIR0JdoXA_1080p.mp4" controls></video>
<br>
<input type="text">
</div>
<script type="module">
import Bullet from "./observer/Bullet.js";//導(dǎo)入模塊
var input=document.querySelector("input");
document.addEventListener("keyup",keyHandler);
//input的鍵盤監(jiān)聽事件按回車實(shí)例化BUllet
function keyHandler(e){
if(e.keyCode!==13) return;
if(input.value.trim().length===0) return;
var bullet=new Bullet(input.value);
bullet.appendTo(".div0");
input.value="";
}
</script>
</body>
</html>
bullet.js:
import TimeManager from "./TimeManager.js";//導(dǎo)入觀察者
export default class Bullet {
rect;
x;
speed = 2;
width;
constructor(txt) {
this.elem = this.createElem(txt);
}
//創(chuàng)建彈幕
createElem(txt) {
if (this.elem) return this.elem;
var div = document.createElement("div");
Object.assign(div.style, {
whiteSpace: "nowrap",
position: "absolute",
})
div.textContent = txt
return div;
}
//將彈幕隨機(jī)位置插入視屏中并且將彈幕插入到觀察者中的list數(shù)組中
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);
}
//使彈幕移動(dòng),并給限制如果彈幕移出視頻,則在list中刪除此元素
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:
export default class TimeManager {
static _instance
list = new set()//設(shè)置一個(gè)set類型的變量
ids
constructor() {
}
static get instance() {
//設(shè)置一個(gè)靜態(tài)方法,此為單例模式,
if (!TimeManager._instance) {
Object.defineProperty(TimeManager, '_instance', {
value: new TimeManager()
})//給TimeManager設(shè)置屬性如果沒有則賦值為實(shí)例并保存,有則直接給保存的實(shí)例
}
return TimeManager._instance;
}
//將帶入的變量加入到list中如果list中有元素時(shí)則沒16ms調(diào)用updata方法
add(elem) {
this.list.add(elem)
if (this.list.size > 0 && !this.ids) {
this.ids = setInterval(() => this.updata(), 16)
}
}
//將帶入的變量從list中刪除,如果list中沒有變量了就將定時(shí)器關(guān)閉,銷毀定時(shí)器
remove(elem) {
this.list.delete(elem)
if (this.list.size === 0 && this.ids) {
clearInterval(this.ids)
this.ids = undefined
}
}
//遍歷所有l(wèi)ist里的元素,如果元素帶有updata方法,則調(diào)用updata方法
updata() {
this.list.forEach(item => {
if (item.updata) item.updata
})
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用JavaScript輕松實(shí)現(xiàn)拖拽功能
- vue elementUi+sortable.js實(shí)現(xiàn)嵌套表格拖拽問題
- 淺析原生JavaScript中拖拽屬性draggable的使用
- JavaScript實(shí)現(xiàn)文件的拖拽上傳功能
- vue結(jié)合el-table實(shí)現(xiàn)表格行拖拽排序(基于sortablejs)
- element-plus結(jié)合sortablejs實(shí)現(xiàn)table行拖拽效果
- 手把手教你用Javascript實(shí)現(xiàn)觀察者模式
- js觀察者模式的介紹及使用
- JavaScript設(shè)計(jì)模式之觀察者模式與發(fā)布訂閱模式詳解
- javascript設(shè)計(jì)模式 – 觀察者模式原理與用法實(shí)例分析
- JavaScript 拖拽與觀察者模式的實(shí)現(xiàn)及應(yīng)用小結(jié)
相關(guān)文章
JS使用window.requestAnimationFrame()實(shí)現(xiàn)逐幀動(dòng)畫
這篇文章介紹了JS使用window.requestAnimationFrame()實(shí)現(xiàn)逐幀動(dòng)畫的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
JavaScript常用的3種彈出框(提示框?alert/確認(rèn)框?confirm/輸入框?prompt)
三種彈框在系統(tǒng)中都是同步執(zhí)行的,也就是說,三種彈框中的任一彈框彈出,代碼都不在執(zhí)行,直到點(diǎn)擊確認(rèn)或取消,關(guān)閉彈窗后,代碼繼續(xù)執(zhí)行,本文通過實(shí)例代碼給大家分享JS常用的3種彈出框,感興趣的朋友一起看看吧2022-07-07
js計(jì)算兩個(gè)時(shí)間差 天 時(shí) 分 秒 毫秒的代碼
這篇文章主要介紹了js計(jì)算兩個(gè)時(shí)間差 天 時(shí) 分 秒 毫秒的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
統(tǒng)計(jì)出現(xiàn)最多的字符次數(shù)的js代碼
一小段代碼,經(jīng)常出現(xiàn)在面試筆試題中的:統(tǒng)計(jì)一個(gè)字符串中出現(xiàn)最多的字符的次數(shù),可以是英文或者數(shù)字。2010-12-12
BootStrap Typeahead自動(dòng)補(bǔ)全插件實(shí)例代碼
本文給大家介紹BootStrap Typeahead自動(dòng)補(bǔ)全插件的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-08-08

