html js弹幕功能

2024-08-22 20:20
文章标签 功能 js html frontend 弹幕

本文主要是介绍html js弹幕功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
效果如上

html
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title></title><script charset="utf-8" src="https://unpkg.com/vue@2.6.14/dist/vue.min.js" type="text/javascript"></script><link rel="stylesheet" href="./css/index.css" /></head><body><div id="app"><div class="index" ref="index"><div class="btn stop" @click="stopDanmu">停止弹幕</div><div class="btn add" @click="adddanmu">添加弹幕</div></div></div></body><script src="./js/index.js"></script>
</html>
js
var app = new Vue({el: '#app',data: {list: [{text: '山河无恙,国泰民安'},{text: '我爱你,中国!'},{text: '辉煌七十五载,山河锦绣灿烂。'},{text: '生日快乐,我的国!'},{text: '清澈的爱,只为中国!'},{text: '岁月悠悠,山河如画!'},{text: '我爱中华!'},{text: '75年风雨兼程,共筑中国梦!'},{text: '鹭岛金秋红旗展,国庆佳节同欢庆'},{text: '泱泱中华, 千古风华依旧'},],windowWidth: '', // 屏幕的宽度marginLeft: 20, // 每一个距离左边的距离currentIndex: 0, // 当前弹幕列表的下标timeList: [], // 每一个弹幕的定时器currentDanmuNum: 0, // 当前的弹道addDanmuInterval:null, // 添加弹幕的定时器},mounted() {this.$nextTick(() => {this.windowWidth = document.querySelector('body').offsetWidththis.addDanmuInterval = setInterval(() => {// 获取弹幕总数 如果超过20个 不添加弹幕let danmuAllNum = document.querySelectorAll('.item').lengthif(danmuAllNum > 20) returnthis.addDanMuFn(this.list[this.currentIndex].text)this.currentIndex++if (this.currentIndex >= this.list.length) {this.currentIndex = 0}}, 1000)this.list.forEach((item, index) => {this.addDanMuFn(this.list[index].text)})// console.log(this.windowWidth);})},methods: {adddanmu() {this.addDanMuFn('添加新的弹幕', true)},stopDanmu() {// console.log(this.timeList);this.timeList.forEach(item => {clearInterval(item)// console.log(item);})clearInterval(this.addDanmuInterval)},addDanMuFn(text, isAddDanmu) {// 这里有个问题 添加太多计时器 就会错位 所以 弹幕量控制在 20以内this.$nextTick(() => {// 创建随机且唯一的弹幕类名idlet danmuName = 'danmu-' + this.randomString(6);// console.log(danmuName);// 生成弹幕的弹道 -- 随机弹道// let danmuNum = 'danmu-' + this.randomNum(0, 4)// 生成弹幕的弹道 -- 排序let danmuNum = 'danmu-' + this.currentDanmuNum;this.currentDanmuNum += 1if (this.currentDanmuNum > 4) {this.currentDanmuNum = 0}// console.log(danmuNum);// 获取 单前弹道的 所有元素let danmuNumAllDomNama = `.${danmuNum}`;let danmuNumAllDom = document.querySelectorAll(danmuNumAllDomNama)// 获取index元素let indexDom = document.querySelector('.index')// 判断当前弹道是否有元素if (danmuNumAllDom.length > 0) {// 获取最后一个元素let lastDom = danmuNumAllDom[danmuNumAllDom.length - 1]// 获取最后一个元素 本身的宽度let lastDomWidth = lastDom.offsetWidth;// 获取最后一个元素  距离左边的距离let lastDomLeft = lastDom.getBoundingClientRect().left;// 新的元素距离左边的距离let newDomLeft = lastDomWidth + lastDomLeft + this.marginLeft;if (newDomLeft < this.windowWidth) {newDomLeft = this.windowWidth}// 创建一个新的divlet div = document.createElement('div');if (isAddDanmu) {div.className = `${danmuName} ${danmuNum} item add`} else {div.className = `${danmuName} ${danmuNum} item`}div.style.left = newDomLeft + 'px';div.innerText = text;indexDom.appendChild(div);let currentDom = document.querySelector(`.${danmuName}`)let divWidth = currentDom.offsetWidth;danmuName = setInterval(() => {currentDom.style.left = currentDom.getBoundingClientRect().left - 1 +'px';if (currentDom.getBoundingClientRect().left < -divWidth) {clearInterval(danmuName)currentDom.remove()let index = this.timeList.findIndex(item => item == danmuName)this.timeList.splice(index,1)danmuName = nullindex = null}}, 10)this.timeList.push(danmuName)} else {// 单前弹道没有元素let newDomLeft = this.windowWidth;// 创建一个新的divlet div = document.createElement('div');if (isAddDanmu) {div.className = `${danmuName} ${danmuNum} item add`} else {div.className = `${danmuName} ${danmuNum} item`}div.style.left = newDomLeft + 'px';div.innerText = text;indexDom.appendChild(div);let currentDom = document.querySelector(`.${danmuName}`)let divWidth = currentDom.offsetWidth;danmuName = setInterval(() => {currentDom.style.left = currentDom.getBoundingClientRect().left - 1 +'px';if (currentDom.getBoundingClientRect().left < -divWidth) {clearInterval(danmuName)currentDom.remove()let index = this.timeList.findIndex(item => item == danmuName)this.timeList.splice(index,1)danmuName = nullindex = null}}, 10)this.timeList.push(danmuName)// console.log(div);}})},randomNum(a, b) {switch (arguments.length) {case 1:return parseInt(Math.random() * a + 1, 10);case 2:return parseInt(Math.random() * (b - a + 1) + a, 10);default:return 0}},randomString(a) {a = a || 32;var b = "";for (i = 0; i < a; i++)b += "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678".charAt(Math.floor(48 * Math.random()));return b}},})
css
*{margin: 0;padding: 0;
}.index{width: 100vw;height: 100vh;background: #fff;position: relative;overflow: hidden;.btn{position: absolute;bottom: 0;width: 50vw;height: 30vw;font-size: 4vw;text-align: center;line-height: 30vw;&.stop{left: 0;}&.add{right: 0;}}.item{height: 10vw;padding: 0 3vw;border-radius: 10vw;background-color: gainsboro;color: #fff;font-size: 4vw;display: inline-block;position: absolute;text-wrap: nowrap;line-height: 10vw;&.add{background: red;}&.danmu-0{top: 0;}&.danmu-1{top: 12vw;}&.danmu-2{top: 24vw;}&.danmu-3{top: 36vw;}&.danmu-4{top: 48vw;}}
}

这篇关于html js弹幕功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097302

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv