原生js实现弹幕效果

时间:2022-09-23 20:41:25

本文实例为大家分享了js实现弹幕效果的具体代码,供大家参考,具体内容如下

效果展示:

原生js实现弹幕效果

源码展示:

?
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
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>原生js实现弹幕效果</title>
  <style>
    * {
      padding:0;
      margin:0;
    }
    .all {
      width:600px;
      height:400px;
      background:#000000;
    }
    /*.danmu {*/
      /*width:600px;*/
      /*height:500px;*/
      /*background:#000000;*/
      /*overflow:hidden;*/
      /*z-index:50;*/
    /*}*/
  </style>
</head>
<body>
<div class="all">
 
</div>
<input type="text" class="texts" value="这是一个弹幕。。。">
<button>发送</button>
 
<script>
  var all = document.querySelector('.all');
  var danmu = document.querySelector('.danmu');
  var buttons = document.querySelector('button');
  var texts = document.querySelector('.texts');
  console.log(texts.value);
  buttons.onclick = function() {
    var p = document.createElement('p');
    p.style.position = 'absolute';
    p.innerHTML = texts.value;
    p.style.left = 600 + 'px';
    p.style.color = 'white';
    p.style.zIndex = 100;
    p.style.top = Math.random() * 490 + 'px';
    all.appendChild(p);
    var x = setInterval(function() {
      p.style.left = parseInt(p.style.left) - 10 + 'px';
      if ((parseInt(p.style.left) + 600) <= 0) {
        all.removeChild(p);
        clearInterval(x);
      }
 
    }, 100)
 
  }
</script>
<pre style="color:red">
 感: 最近贡献一下我在教学中的小案例 希望能给你一些帮助 ,希望大家继续关注我的博客
 
                                        --王
</pre>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wangzhneg123/article/details/88015656