barrage.html
1.83 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>畅移2020年会盛典</title>
<style>
html, body {padding: 0;margin: 0;height:100%;overflow: hidden;}
img {display: block;}
* {padding: 0;margin: 0;}
.container {width: 100%;height: 100%;background: #000 url('./img/pc/barrage_bg.png') no-repeat center;background-size: 420px 100px;}
</style>
</head>
<body>
<div class="container" id="container"></div>
<script src="./js/jquery.min.js"></script>
<script src="./js/danmaku.js"></script>
<script src="//cdn.bootcss.com/socket.io/2.0.3/socket.io.js"></script>
<script>
var socketUrl = '//106.54.64.163:3000';
var colors = ['#FFD590', '#DE452D', '#FFFFFF'];
var danmaku = new Danmaku();
danmaku.init({
container: document.getElementById('container')
});
var socket = io(socketUrl);
var oldTopNum, topNum;
socket.on('barrage', function(msg) {
// console.log('socket.on: ', msg);
var colorIndex = Math.floor(Math.random() * (2 - 0 + 1)); // 生成 0-2 随机数
topNum = Math.floor(Math.random() * (10 - 1 + 1) + 1); // 生成 1-10 随机数
while (topNum === oldTopNum) {
topNum = Math.floor(Math.random() * (10 - 1 + 1) + 1);
}
danmaku.emit({
text: msg,
style: {
fontSize: '35px',
fontWeight: '500',
color: colors[colorIndex],
top: topNum * 50 + 'px'
}
});
oldTopNum = topNum;
});
</script>
</body>
</html>