当前位置: 首页 > news >正文

滚动弹幕案例

滚动弹幕案例

一、需求

1.页面上漂浮字体大小不一、颜色不一,从左向右滚动的弹幕;
2.底部中间有一个发送功能,可以发送新的弹幕;
3.底部的发送部分可以向下收起和弹出。

二、html

<div class="container">
  <div class="content">
    <p>哈哈哈哈哈</p>
    <p>哈哈哈哈哈哈哈哈</p>
    <p>哈哈哈哈哈</p>
    <p>哈哈哈哈哈哈哈</p>
    <p>哈哈哈哈哈哈哈哈哈哈哈哈哈</p>
    <p>哈哈哈哈哈哈哈哈哈哈</p>
    <p>哈哈哈</p>
  </div>
  <div class="input-box">
    <input type="text">
    <button>发射</button>
  </div>
  <button class="show">收起</button>
</div>

三、css

<style>
  .container{
    margin:0 auto;
    width: 500px;
    height: 400px;
    border:1px solid black;
    position:relative;
  }
  .content {
    width: 500px;
    height: 365px;
    position:relative;
    overflow: hidden;
  }
  .input-box {
    width: 500px;
    height: 30px;
    position:absolute;
    bottom:0;
  }
  .input-box input {
    width: 400px;
    height: 25px;
    float:left;
  } 
  .input-box button{
    width: 90px;
    height: 30px;
    float:right;
    cursor:pointer;
  }
  @keyframes moveDanmu {
    from{
      left:-100%
    }
    to{
      left:100%
    }
  }
  p{
    white-space: nowrap;
    position:absolute;
    animation: moveDanmu 8s linear infinite;
    left:-100%;
  }
  .show {
    width: 50px;
    height: 30px;
    position:absolute;
    left:500px;
    bottom:0px;
  }
</style>

四、javascript

<script>
  const container =document.querySelector('.container')
  const content =document.querySelector('.content')
  const danmus = document.querySelectorAll('.content p')
  const text = document.querySelector('.input-box input')
  const button = document.querySelector('.input-box button')
  const inputBox = document.querySelector('.input-box')
  const show = document.querySelector('.show')
  //创建随机颜色
  function getRandomRGBColor(){
    const r = Math.floor(Math.random()*255)
    const g = Math.floor(Math.random()*255)
    const b = Math.floor(Math.random()*255)
    return `rgb(${r},${g},${b})`
  }
  //创建top值
  function getRandomTop(){
    return (Math.floor(Math.random()*content.offsetHeight)- 44 +'px')
  }
  console.log(content.offsetHeight);
  
  //创建随机大小
  function getRandomFontSize(){
    return (Math.floor(Math.random()*30)+14+'px')
  }
  //发送弹幕
  button.addEventListener('click',function(){
    if(text.value!==''){
      const danmu = document.createElement('p')
      danmu.textContent = text.value
      danmu.style.color = getRandomRGBColor()
      danmu.style.top= getRandomTop()
      danmu.style.fontSize = getRandomFontSize()
      content.appendChild(danmu)
      text.value = ''

      // 弹幕滚动结束后移除
      danmu.addEventListener('animationend', () => {
      	danmuContainer.removeChild(danmu);
 	  });
    }
  })
  
  //已有弹幕
  danmus.forEach((hadDanmu)=>{
    const randomDelay=Math.floor(Math.random()*10000)
    hadDanmu.style.color = getRandomRGBColor()
    hadDanmu.style.top= getRandomTop()
    hadDanmu.style.fontSize = getRandomFontSize()
    hadDanmu.style.animationDelay = randomDelay+'ms'
  })

  //展开收起
  let isInputHidden = false
  show.addEventListener('click',function(){
    if(isInputHidden){
      inputBox.style.display='block'
      show.textContent='收起'
    }
    else{
      inputBox.style.display='none'
      show.textContent='展开'
    }
    isInputHidden=!isInputHidden
  })
</script>

五、样式截图

请添加图片描述

六、实现原理

  1. 使用Math.random随机生成方法,构建随机颜色、随机大小、随机绝对定位高度
  2. 对于已有的弹幕,针对每一个弹幕随机生成颜色大小位置,并且设置css动画(animation)使其从左到右运动,对每个弹幕设置不同的延迟时间出现,形成弹幕效果。
  3. 对于即将发送的弹幕,需新建p元素,且同样随机生成颜色大小位置,弹幕内容为input的value值,最后将其添加至已有弹幕p元素后。
  4. 发送条的展开收起,引入布尔变量isInputHidden并设置初始值为false,按钮内容初始设置为收起,发送条display初始值为block,当点击收起按钮,按钮收起–>展开,发送条block–>none,isInputHidden值取反,相反同理。

相关文章:

  • 面基Spring Boot项目中实用注解一
  • 电子电气架构 --- 电子电器新技术及发展趋势
  • [JVM篇]垃圾回收器
  • Windows第九章 控件的介绍
  • 共指消解问题的早期研究成果主要来自自然语 言处理领域
  • AdaMix
  • PH热榜 | 2025-02-16
  • c语言基础09
  • 什么决定了硬件设计质量?
  • Web后端 - Maven管理工具
  • 深入解析计算机网络请求头:常见类型与安全性影响
  • FFmpeg源码:url_find_protocol函数分析
  • maven——使用idea创建maven项目(文件夹上颜色)
  • 15.2 ProcessStartInfo类
  • 浏览器Cookies、SessionStorage 和 LocalStorage
  • RL--2
  • 机器学习:十大算法实现汇总
  • 组件库地址
  • 神经网络新手入门(3)光明顶复出(2006-2012)
  • 每日OJ_牛客_Pre-Post(英文题树的遍历_排列组合)
  • 试点首发进口消费品检验便利化措施,上海海关与上海商务委发文
  • 征稿启事|澎湃·镜相第三届非虚构写作大赛暨2026第六届七猫现实题材征文大赛
  • 英德宣布开发射程超2000公里导弹,以防务合作加强安全、促进经济
  • 中国进出口银行:1-4月投放制造业中长期贷款超1800亿元
  • 把中国声音带向世界,DG和Blue Note落户中国
  • 网约车座椅靠背张贴“差评者得癌症”,如祺出行:未收到投诉无法处理