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

jq——实现弹幕滚动(往左滚动+往右滚动)——基础积累

在这里插入图片描述
最近同事在写弹幕功能,下面记录以下代码:

1.html代码

<div id="scrollContainer"></div>

2.引入jq

<script src="./script/jquery-1.8.3.js" type="text/javascript"></script>

3.jq代码——往左滚动

<script>function getRandomName() {const names = ['王','熊','张','郑','刘','李','秦','付','肖','宋','陈','杨','黄','周','吴','赵','孙','钱','朱','杜','董','程','曹','田','谢','韩','杜','叶','吕','丁',];const randomName = names[Math.floor(Math.random() * names.length)];const gender = Math.random() < 0.5 ? '先生' : '女士';return randomName + gender + '已领取';}function createFloatingBox() {const box = document.createElement('div');box.className = 'floatingBox';box.textContent = getRandomName();return box;}function startAnimation() {const container = document.getElementById('scrollContainer');function animate() {for (let i = 0; i < 3; i++) {const box = createFloatingBox();const duration = 6000; // 4 secondsconst endPosition = container.offsetWidth + 50;const startPosition = -box.offsetWidth - 50;const speed = (startPosition - endPosition) / duration;let startTime = null;function step(timestamp) {if (!startTime) startTime = timestamp;const progress = timestamp - startTime;const distance = speed * progress;if (progress < duration) {box.style.right = startPosition - distance + 'px';requestAnimationFrame(step);} else {container.removeChild(box);}}box.style.right = startPosition + 'px';if (i === 0) {box.classList.add('line1');} else if (i === 1) {box.classList.add('line2');} else {box.classList.add('line3');}container.appendChild(box);requestAnimationFrame(step);}}// Start the animation every 1 secondsetInterval(animate, 1500);}// Start the animationstartAnimation();</script>

4.jq代码——往右滚动

 <script>function getRandomName() {const names = ['王','熊','张','郑','刘','李','秦','付','肖','宋','陈','杨','黄','周','吴','赵','孙','钱','朱','杜','董','程','曹','田','谢','韩','杜','叶','吕','丁',];const randomName = names[Math.floor(Math.random() * names.length)];const gender = Math.random() < 0.5 ? '先生' : '女士';return randomName + gender + '已领取';}function createFloatingBox() {const box = document.createElement('div');box.className = 'floatingBox';box.textContent = getRandomName();return box;}function startAnimation() {const container = document.getElementById('scrollContainer');function animate() {for (let i = 0; i < 3; i++) {const box = createFloatingBox();const duration = 6000; // 4 secondsconst startPosition = container.offsetWidth + 50;const endPosition = -box.offsetWidth - 50;const speed = (endPosition - startPosition) / duration;let startTime = null;function step(timestamp) {if (!startTime) startTime = timestamp;const progress = timestamp - startTime;const distance = speed * progress;if (progress < duration) {box.style.right = startPosition + distance + 'px';requestAnimationFrame(step);} else {container.removeChild(box);}}box.style.right = startPosition + 'px';if (i === 0) {box.classList.add('line1');} else if (i === 1) {box.classList.add('line2');} else {box.classList.add('line3');}container.appendChild(box);requestAnimationFrame(step);}}// Start the animation every 1 secondsetInterval(animate, 1500);}// Start the animationstartAnimation();</script>
http://www.lryc.cn/news/247528.html

相关文章:

  • 深度学习第2天:RNN循环神经网络
  • 深度学习之基于百度飞桨PaddleOCR图像字符检测识别系统
  • 九、LuaTable(表)
  • 每日一题(LeetCode)----链表--链表最大孪生和
  • 腾讯云轻量服务器通过Docker搭建外网可访问连接的redis5.x集群
  • C++学习之路(十一)C++ 用Qt5实现一个工具箱(增加一个进制转换器功能)- 示例代码拆分讲解
  • C语言每日一题(40)栈实现队列
  • Vue.js 的生命周期
  • SeaTunnel引擎下的SQL Server CDC解决方案:构建高效数据管道
  • 【攻防世界-misc】Encode
  • visual c++ 2019 redistributable package
  • WPF中DataGrid解析
  • 在数据库中进行表内容的修改(MYSQL)
  • Android中的多进程
  • Apache2.4 AliasMatch导致301重定向问题?
  • 广州华锐视点:基于VR元宇宙技术开展法律法规常识在线教学,打破地域和时间限制
  • Maven——Maven使用基础
  • U4_2:图论之MST/Prim/Kruskal
  • springboot 注解@JsonInclude
  • Python 中文完整教程目录
  • C/C++---------------LeetCode第35. 搜索插入位置
  • 网络安全--基于Kali的网络扫描基础技术
  • C语言——求π的近似值
  • 如何使用ffmpeg转换图片格式
  • 11 动态规划解最后一块石头的重量II
  • LeetCode算法题解(动态规划,股票买卖)|LeetCode121. 买卖股票的最佳时机、LeetCode122. 买卖股票的最佳时机 II
  • python实验3 石头剪刀布游戏
  • 米贸搜|如何设置 Facebook 转换 API + 事件重复数据删除
  • python每日一题——11滑动窗口最大值
  • 【C++ 程序设计入门基础】- 第3节-循环结构01