JavaScript实现简易弹幕效果教程
作者:像素猫时间:2026-10-25 19:07:27浏览:507
本文详细介绍如何使用JavaScript、HTML和CSS实现一个简易的弹幕功能。通过获取用户输入,动态创建DOM元素,利用CSS绝对定位设置初始位置,并结合定时器或CSS动画实现从右向左的滚动效果。文章包含完整的代码示例及关键逻辑解析,帮助开发者掌握前端动态交互与动画处理技巧。
本文通过具体代码实例,展示如何利用JavaScript实现一个简易的弹幕功能,供读者参考学习。
以下是最终实现的效果图展示。

实现步骤分析如下:
第一步,获取用户输入的内容,并基于该内容创建一个span标签,将其添加到指定的容器元素中。
第二步,为生成的弹幕元素设置初始位置。
- 水平方向(left):初始位置设置在容器的右侧外部,确保弹幕从屏幕外进入。
- 垂直方向(top):随机设置在容器的上半部分区域内。
第三步,通过CSS样式或JavaScript定时器来实现元素的移动动画效果。
第四步,处理一些细节逻辑。
- 弹幕文本的颜色需要随机生成,以增加视觉效果。
- 当span元素的动画执行完毕后,应自动将其从DOM中移除,避免内存泄漏。
以下是HTML结构代码。
<div class="boxDom" id="boxDom">
<div class="idDom" id="idDom">
<div class="content">
<p class="title">吐槽:</p>
<input type="text" class="text" id="text" />
<button type="button" class="btn" id="btn">发射</button>
</div>
</div>
</div>以下是CSS样式代码。
<style type="text/css">
html,
body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
font-family: '微软雅黑';
font-size: 62.5%;
}
.boxDom {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.idDom {
width: 100%;
height: 100px;
background: #666;
position: fixed;
bottom: 0px;
}
.content {
display: inline-block;
width: 430px;
height: 40px;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin: auto;
}
.title {
display: inline;
font-size: 4em;
vertical-align: bottom;
color: #fff;
}
.text {
border: none;
width: 300px;
height: 30px;
border-radius: 5px;
font-size: 2.4em;
}
.btn {
width: 60px;
height: 30px;
background: #f90000;
border: none;
color: #fff;
font-size: 2.4em;
}
span {
/* width: 300px; */
height: 40px;
position: absolute;
overflow: hidden;
color: #000;
font-size: 4em;
line-height: 1.5em;
cursor: pointer;
white-space: nowrap;
}
</style>以下是JavaScript逻辑代码。
// 获取元素
let btn = document.querySelector('#btn')
let text = document.querySelector('#text')
let boxDom = document.querySelector('#boxDom')
// 为按钮绑定事件
btn.addEventListener('click', function() {
// 获取用户输入内容
// 表单元素input的值的获取是使用value
let content = text.value
// trim:去除左右空格
if (content.trim().length == 0) {
alert('请输入一个内容再发弹幕')
return
}
// 创建一个元素
// createElement:创建元素
let span = document.createElement('span')
span.innerText = content
// 为元素设置样式
// clientWidth:获取元素的实际宽度
// 设置left值为元素右侧外
span.style.left = boxDom.clientWidth + 'px'
// 设置top为上半区随机位置
span.style.top =
parseInt((Math.random() * boxDom.clientHeight) / 2) + 'px'
// span.style.color = setColor()
//设置字体的随机颜色
span.style.color = `rgb(${Math.random() * 255},${Math.random() *
255},${Math.random() * 255})`
// 让元素动起来 -- 配合过渡样式
// setTimeout(() => {
// span.style.left = -span.clientWidth + 'px'
// }, 200)
// 距停止位置的距离
let dis = boxDom.clientWidth
// setInterval(需要执行的函数,时间间隔)
let tid = setInterval(function() {
dis -= 1
span.style.left = dis + 'px'
// 移动到目标位置,清除定时器
if (dis < -span.clientWidth) {
clearInterval(tid)
// 将当前的span移除
span.remove()
}
}, 4)
// 添加到指定容器中
// insertBefore:将指定的元素插入到参照元素的前面:父容器.insertBefore(子元素,参照元素)
// appendChild:将元素追加到所有子元素的最后: 父容器.appendChild(子元素)
// insertBefore:一定传入两个参数
boxDom.insertBefore(span, boxDom.children[0])
})以上就是本文的全部内容,希望对各位的学习有所帮助,也欢迎大家继续支持本站。
作者最新文章
vtk.js 渲染窗口黑屏或无内容显示的常见原因及排查
2026-11-05 17:30:30
js-cookie 安装与基础用法:set、get、remove 详解
2026-11-05 16:35:06
适合JavaScript初学者的实战项目:制作简易待办事项列表
2026-11-05 16:17:07
JS打开新标签页时怎么通过URL传递参数
2026-11-05 12:21:08
sigma.js 自定义节点样式:设置颜色、大小及标签显示的技巧
2026-11-05 09:55:00
