-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
170 lines (157 loc) · 4.42 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>弹幕测试</title>
<style>
html,
body {
margin: 0;
padding: 0;
}
.danmu-content {
margin: 30px 10px;
height: 180px;
border: 1px solid;
}
.opeate button {
margin: 10px;
border: 1px solid;
}
.screen {
/* width: 100vw; */
height: 100%;
}
.danmu-item {
margin: 5px;
border: 1px solid;
overflow: hidden;
display: inline-block;
word-break: keep-all;
white-space: nowrap;
}
</style>
</head>
<body>
<div id="root">
<div class="app">
<div class="danmu-content">
<div class="screen"></div>
<div class="opeate">
<button id="pauseAll">暂停全部</button>
<button id="continueAll">继续全部</button>
</div>
<div class="opeate">
<button class="close-time">暂停计时器</button>
<button class="open-time">继续计时器</button>
</div>
<div class="opeate">
<input id="d-input" type="text" placeholder="手动发送的弹幕">
<button id="sendDanmu">发送弹幕</button>
</div>
</div>
</div>
</div>
</body>
<script src="/BulletJs.esm.js" type="module"></script>
<!-- <script src="/BulletJs.min.js"></script> -->
<!-- <script src="../dist/BulletJs.min.js"></script> -->
<script>
const danmuList = [
"你好科比布莱恩特",
"这个老哥的评论说的很对啊,这都能被违规隐藏?是戳痛到某些人了吗。",
"rip....",
"resepct",
"😂❤️😍😒👌☺️☺️😊😭😩😩😔😏😁👍🏿😁😏😏😔💕😭😘😊❤️😍",
"圣何塞-穆里尼奥",
"泰伦卢,泰伦卢,泰伦卢,泰伦卢",
"勒布朗-詹姆斯",
"emoji",
"湖人阵容也不是特别豪华为什么能打到西部第一? 由高田大叔 发表在篮球·湿乎乎的话题-说的很对啊,很对啊",
"----huhuh---hahahha---",
"😂❤️😍😒👌☺️☺️😊😭😩😩😔😏😁👍🏿😁😏😏😔💕😭😘😊❤️😍"
]
const getRandom2 = (min, max) => parseInt(Math.random() * (max - min + 1)) + min
const danmuTest = {
screen: null,
handler: null,
dom: {
closeTime: document.querySelector('.close-time'),
openTime: document.querySelector('.open-time'),
sendDanmu: document.querySelector('#sendDanmu'),
dInput: document.querySelector('#d-input'),
pauseAll: document.querySelector('#pauseAll'),
continueAll: document.querySelector('#continueAll'),
},
init() {
this.eventInit();
this.screen = new BulletJs('.screen', {
trackHeight: 35,
speed: undefined,
pauseOnClick: true,
pauseOnHover: true,
});
this.sendDanmu();
},
sendDanmu() {
this.sendAction();
},
sendAction() {
this.handler = setInterval(() => {
const index = getRandom2(0, 10);
const str = danmuList[index];
const dom = `<span>${str}</span>`
this.screen.push(dom)
}, 1000);
},
// 暂停
pause() {
this.screen.pause()
},
// 继续
continue() {
this.screen.resume()
},
closeTime() {
this.handler && clearInterval(this.handler)
},
eventInit() {
this.dom.closeTime.addEventListener('click', () => {
this.closeTime()
})
this.dom.openTime.addEventListener('click', () => {
this.sendDanmu()
})
this.dom.sendDanmu.addEventListener('click', () => {
const value = this.dom.dInput.value;
// 此处一定要对用户输入进行过滤防止 xss 攻击
const escapeHTML = value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\"/g, '"').replace(/\'/g, ''').replace(/\//g, '/')
console.log('escapeHTML--->', escapeHTML)
if (value) {
const danmu = `<span class="danmu-item" style="color: red;">${escapeHTML}</span>`
this.screen.push(danmu, {
onStart: (id, danmu) => {
// console.log('实例的方法onStart----->', 'id------->', id, 'danmu-------->', danmu)
},
onEnd: (id, danmu) => {
// console.log('实例的方法onEnd----->', 'id------->', id, 'danmu-------->', danmu)
}
}, true)
} else {
alert("请输入要发送的弹幕")
}
})
this.dom.pauseAll.addEventListener('click', () => {
this.pause();
})
this.dom.continueAll.addEventListener('click', () => {
this.continue();
})
}
}
danmuTest.init()
</script>
</html>