JS 简单实现视频弹幕效果 ( + cookie)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
  <title>视频弹幕</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    /* 通过标签名称设置样式 */
    body {
      background: rgb(245, 245, 245);
      /* overflow: hidden; */
    }

    .container {
      width: 500px;
      background: #fff;
      /* 设置元素在页面中间 */
      margin: 100px auto;
      padding: 10px;
    }

    .container .top {
      width: 100%;
      position: relative;
      overflow: hidden;
    }

    .container .top video {
      width: 100%;
    }

    .container .bot {
      display: flex;
    }

    .container .bot .danmu-num {
      width: 100px;
    }


    .container .bot .danmu-color {
      width: 80px;
    }


    .container .bot .danmu-input {
      width: 240px;
      margin-left: -15px;
    }

    .container .bot .danmu-input input {
      width: 100%;
      height: 25px;
      font-size: 16px;
    }

    #textColor {
      width: 50px;
      height: 25px;
      border: none;
    }


    .container .bot .danmu-btn {
      flex: 1;
      text-align: center;
      background: rgb(169, 160, 209);
      line-height: 25px;
      cursor: pointer;
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;
      /* margin-left: 3px; */
    }

    .msg {


      transform: translateX(-100px);

      position: absolute;
      /* -webkit-text-stroke: 1px black; */
      font-weight: 800;

      /* 使用关键帧动画 */
      animation: l-2-r 6s linear;
    }

    @keyframes l-2-r {
      from {
        transform: translateX(500px);
      }
      to {
        transform: translateX(-100px);
      }
    }
  </style>
</head>

<body>

  <div class="container">
    <div class="top" id="dmList">
    //添加视频地址 (mp4)
      <video src="../TV/sp.mp4" id="video" width="300" height="300"></video>
    </div>
    <div class="bot">
      <div><span  id="numbers">0</span><span>条弹幕</span> </div>
      <div class="danmu-color">
        <input type="color" id="textColor">
      </div>

      <div class="danmu-input">
        <input type="text" id="text">
      </div>
      <div class="danmu-btn" onclick="sendDanmu()">发送弹幕</div>
    </div>
  </div>

  <script>

    let dmList = document.getElementById('dmList')
    let video = document.getElementById('video')
    
    video.addEventListener('click', function () {
        video.play()
    })
    
    function sendDanmu() {
        let text = document.getElementById("text").value;
        let p = document.createElement("p")
        p.innerText = text;
        p.setAttribute("class", "msg")
        let h = fn.random(10,100);
        p.style.top = h + "px"
        let color = document.getElementById("textColor").value
        p.style.color = color;
        dmList.appendChild(p)
        fn.setCookie("userMsg",p.innerHTML);


        let numbers = document.getElementById("numbers");
        let barrage = numbers.innerHTML++;

        console.log(numbers.innerHTML)

      document.getElementById("text").value = ""
    }



    
    const fn = {
        random(min,max){
        return Math.round(Math.random()*(max-min)+ min);
    },

    setCookie(key, val, exp){
        if (exp) {
          let date = new Date();
          let time = date - 0;
          date.setTime(exp * 60 * 1000 + time);
          date = date.toGMTString();
          exp = 'expires=' + date;
        }
        document.cookie = key + '=' + val + ';' + exp;
      }
    }

  </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值