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

1.html代码

&lt;div id="scrollContainer"&gt;</div&gt;

2.引入jq

<script src="./script/jquery-1.8.3.js" type="text/javascript"&gt;</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 seconds
            const 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 second
        setInterval(animate, 1500);
      }

      // Start the animation
      startAnimation();
    </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 seconds
            const 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 second
        setInterval(animate, 1500);
      }

      // Start the animation
      startAnimation();
    </script>

原文地址:https://blog.csdn.net/yehaocheng520/article/details/134673314

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_6077.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注