网络编程 
首页 > 网络编程 > 浏览文章

原生js实现弹幕效果

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

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

效果展示:

原生js实现弹幕效果

源码展示:

<!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>

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

上一篇:原生js+canvas实现验证码
下一篇:javascript实现点击小图显示大图
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网