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

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?