JQuery 弹幕可上下左右

水平弹幕,可设置向左、向右

containerId为显示弹幕容器ID, message弹幕内容, direction弹幕移动方向

// Jquery指定容器 向左或向右弹幕
      function sendBarrage_horizontal(containerId, message, direction) {
        // ===========第1部分,设置并挂载元素=========================
        // 定义弹幕元素,设置display:inline-block目的是获取内容实际的宽度
        var $barrage = $(
          '<div class="barrage" style="display:inline-block"></div>'
        );
        // 将弹幕内容放到元素中
        $barrage.text(message);
        // 将弹幕元素挂载到指定显示容器中,并设置容器,超过自动隐藏及相对位置
        $('#' + containerId).append($barrage);
        $('#' + containerId).css({ position: 'relative', overflow: 'hidden' });
        // ==========================================

        // ===========第2部分,显示高度位置及颜色=========================
        // 获取容器高度、弹幕元素高度,用于计算弹幕的上下偏移位置
        var containerHeight = $('#' + containerId).outerHeight();
        var barrageHeight = $barrage.outerHeight();

        // 获取容器宽度、弹幕元素宽度,用于计算弹幕开始显示的位置,结束显示的位置
        var containerWidth = $('#' + containerId).outerWidth();
        var barragewidth = $barrage.outerWidth();

        // 用于根据容器及弹幕元素的高度生成弹幕元素在容器显示时的位置高度
        var top =
          Math.floor(Math.random() * (containerHeight - barrageHeight)) + 'px';
        // 随机颜色
        var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
        $barrage.css({
          top: top,
          color: color,
          position: 'absolute',
          'white-space': 'nowrap',
        });
        // ==========================================

        // ===========第3部分,显示移动方向位置及颜色=========================
        // 从右侧开始向左移动
        if (direction == 'left' || direction == '') {
          $barrage
            .css({
              left: containerWidth,
            })
            .animate({ left: -barragewidth }, 10000, function () {
              $(this).remove();
            });
        }
        // 从左侧开始向右移动
        if (direction == 'right') {
          $barrage
            .css({
              left: -barragewidth,
            })
            .animate({ left: containerWidth }, 10000, function () {
              $(this).remove();
            });
        }
      }

垂直弹幕,可设置向上、向下

containerId为显示弹幕容器ID, message弹幕内容, direction弹幕移动方向

 // Jquery指定容器 向上或向下弹幕
      function sendBarrage_vertical(containerId, message, direction) {
        // ===========第1部分,设置并挂载元素=========================
        // 定义弹幕元素,设置writing-mode: vertical-rl;目的是设置显示问垂直方向
        var $barrage = $(
          '<div class="barrage" style="writing-mode: vertical-rl;"></div>'
        );
        // 将弹幕内容放到元素中
        $barrage.text(message);
        // 将弹幕元素挂载到指定显示容器中,并设置容器,超过自动隐藏及相对位置
        $('#' + containerId).append($barrage);
        $('#' + containerId).css({ position: 'relative', overflow: 'hidden' });
        // ==========================================

        // ===========第2部分,显示高度位置及颜色=========================
        // 获取容器高度、弹幕元素高度,用于计算弹幕的上下偏移位置
        var containerHeight = $('#' + containerId).outerHeight();
        var barrageHeight = $barrage.outerHeight();

        // 获取容器宽度、弹幕元素宽度,用于计算弹幕开始显示的位置,结束显示的位置
        var containerWidth = $('#' + containerId).outerWidth();
        var barragewidth = $barrage.outerWidth();

        // 用于根据容器及弹幕元素的高度生成弹幕元素在容器显示时的位置高度
        var left =
          Math.floor(Math.random() * (containerWidth - barragewidth)) + 'px';
        // 随机颜色
        var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
        $barrage.css({
          left: left,
          color: color,
          position: 'absolute',
          'white-space': 'nowrap',
        });
        // ==========================================

        // ===========第3部分,显示移动方向位置及颜色=========================
        // 从右侧开始向下移动
        if (direction == 'down' || direction == '') {
          $barrage
            .css({
              top: -barrageHeight,
            })
            .animate({ top: containerHeight }, 10000, function () {
              $(this).remove();
            });
        }
        // 从左侧开始向上移动
        if (direction == 'up') {
          $barrage
            .css({
              top: containerHeight,
            })
            .animate({ top: -barrageHeight }, 10000, function () {
              $(this).remove();
            });
        }
      }

标题完整示例代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <title>Document</title>

    <style>
      #barrage-container {
        width: 50%;
        height: 200px;
        margin-left: 200px;
        border: 1px solid red;
      }
    </style>
  </head>
  <body>
    <div id="barrage-container"></div>
    <input
      type="text"
      id="barrage-input"
      title="弹幕"
    />
    <button id="send-barrage-right">向右移动-发送</button>
    <button id="send-barrage-left">向左移动-发送</button>
    <button id="send-barrage-up">向上移动-发送</button>
    <button id="send-barrage-down">向左下移动-发送</button>
    <script
      src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"
      type="text/javascript"
    ></script>

    <!--自定义的JS代码-->
    <script type="text/javascript">
      $(document).ready(function () {
        $('#send-barrage-right').click(function () {
          var message = $('#barrage-input').val();
          sendBarrage_horizontal('barrage-container', message, 'right');
        });

        $('#send-barrage-left').click(function () {
          var message = $('#barrage-input').val();
          sendBarrage_horizontal('barrage-container', message, 'left');
        });

        $('#send-barrage-up').click(function () {
          var message = $('#barrage-input').val();
          sendBarrage_vertical('barrage-container', message, 'up');
        });
        $('#send-barrage-down').click(function () {
          var message = $('#barrage-input').val();
          sendBarrage_vertical('barrage-container', message, 'down');
        });
      });

      // Jquery指定容器 向左或向右弹幕
      function sendBarrage_horizontal(containerId, message, direction) {
        // ===========第1部分,设置并挂载元素=========================
        // 定义弹幕元素,设置display:inline-block目的是获取内容实际的宽度
        var $barrage = $(
          '<div class="barrage" style="display:inline-block"></div>'
        );
        // 将弹幕内容放到元素中
        $barrage.text(message);
        // 将弹幕元素挂载到指定显示容器中,并设置容器,超过自动隐藏及相对位置
        $('#' + containerId).append($barrage);
        $('#' + containerId).css({ position: 'relative', overflow: 'hidden' });
        // ==========================================

        // ===========第2部分,显示高度位置及颜色=========================
        // 获取容器高度、弹幕元素高度,用于计算弹幕的上下偏移位置
        var containerHeight = $('#' + containerId).outerHeight();
        var barrageHeight = $barrage.outerHeight();

        // 获取容器宽度、弹幕元素宽度,用于计算弹幕开始显示的位置,结束显示的位置
        var containerWidth = $('#' + containerId).outerWidth();
        var barragewidth = $barrage.outerWidth();

        // 用于根据容器及弹幕元素的高度生成弹幕元素在容器显示时的位置高度
        var top =
          Math.floor(Math.random() * (containerHeight - barrageHeight)) + 'px';
        // 随机颜色
        var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
        $barrage.css({
          top: top,
          color: color,
          position: 'absolute',
          'white-space': 'nowrap',
        });
        // ==========================================

        // ===========第3部分,显示移动方向位置及颜色=========================
        // 从右侧开始向左移动
        if (direction == 'left' || direction == '') {
          $barrage
            .css({
              left: containerWidth,
            })
            .animate({ left: -barragewidth }, 10000, function () {
              $(this).remove();
            });
        }
        // 从左侧开始向右移动
        if (direction == 'right') {
          $barrage
            .css({
              left: -barragewidth,
            })
            .animate({ left: containerWidth }, 10000, function () {
              $(this).remove();
            });
        }
      }

      // Jquery指定容器 向上或向下弹幕
      function sendBarrage_vertical(containerId, message, direction) {
        // ===========第1部分,设置并挂载元素=========================
        // 定义弹幕元素,设置writing-mode: vertical-rl;目的是设置显示问垂直方向
        var $barrage = $(
          '<div class="barrage" style="writing-mode: vertical-rl;"></div>'
        );
        // 将弹幕内容放到元素中
        $barrage.text(message);
        // 将弹幕元素挂载到指定显示容器中,并设置容器,超过自动隐藏及相对位置
        $('#' + containerId).append($barrage);
        $('#' + containerId).css({ position: 'relative', overflow: 'hidden' });
        // ==========================================

        // ===========第2部分,显示高度位置及颜色=========================
        // 获取容器高度、弹幕元素高度,用于计算弹幕的上下偏移位置
        var containerHeight = $('#' + containerId).outerHeight();
        var barrageHeight = $barrage.outerHeight();

        // 获取容器宽度、弹幕元素宽度,用于计算弹幕开始显示的位置,结束显示的位置
        var containerWidth = $('#' + containerId).outerWidth();
        var barragewidth = $barrage.outerWidth();

        // 用于根据容器及弹幕元素的高度生成弹幕元素在容器显示时的位置高度
        var left =
          Math.floor(Math.random() * (containerWidth - barragewidth)) + 'px';
        // 随机颜色
        var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
        $barrage.css({
          left: left,
          color: color,
          position: 'absolute',
          'white-space': 'nowrap',
        });
        // ==========================================

        // ===========第3部分,显示移动方向位置及颜色=========================
        // 从右侧开始向下移动
        if (direction == 'down' || direction == '') {
          $barrage
            .css({
              top: -barrageHeight,
            })
            .animate({ top: containerHeight }, 10000, function () {
              $(this).remove();
            });
        }
        // 从左侧开始向上移动
        if (direction == 'up') {
          $barrage
            .css({
              top: containerHeight,
            })
            .animate({ top: -barrageHeight }, 10000, function () {
              $(this).remove();
            });
        }
      }
    </script>
  </body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
视频幕特效可以使用 JavaScript 和 jQuery 来实现,具体步骤如下: 1. 创建幕元素 首先,在视频播放器下方创建一个 div 元素,用于显示幕,将其样式设置为绝对定位,宽度为视频播放器的宽度,高度为幕字体大小的两倍,背景色为透明。 ```html <div id="danmu-container"></div> ``` ```css #danmu-container { position: absolute; bottom: 0; width: 100%; height: 2em; background-color: transparent; overflow: hidden; } ``` 2. 发送幕 使用 jQuery 监听发送按钮的点击事件,获取文本框中的幕内容,并动态创建一个 span 元素,设置其样式和文本内容,将其添加到幕容器内,设置其初始位置为容器的右侧,使用 animate() 方法实现幕的滚动效果。 ```html <input type="text" id="danmu-input"> <button id="danmu-send">发送</button> ``` ```javascript $('#danmu-send').click(function() { var content = $('#danmu-input').val(); if (content) { var $danmu = $('<span>' + content + '</span>'); $danmu.css({ position: 'absolute', top: Math.random() * ($('#danmu-container').height() - 30) + 'px', right: 0, fontSize: '1em', whiteSpace: 'nowrap', color: '#fff' }); $('#danmu-container').append($danmu); $danmu.animate({ left: $('#danmu-container').width() }, 8000, function() { $(this).remove(); }); $('#danmu-input').val(''); } }); ``` 3. 暂停幕 使用 jQuery 监听视频播放器的暂停事件,遍历幕容器内的所有幕元素,使用 stop() 方法停止幕的滚动动画,即可实现暂停幕的效果。 ```javascript $('#video-player').on('pause', function() { $('#danmu-container span').stop(); }); ``` 4. 添加特效 为了实现幕特效,可以对幕元素的样式进行修改,例如使用 CSS3 的 transform 属性实现幕的旋转效果,使用 text-shadow 属性实现幕的阴影效果等。具体代码如下: ```css #danmu-container span { position: absolute; top: 0; right: 0; font-size: 1em; white-space: nowrap; color: #fff; transform: rotateY(180deg); text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000; } ``` 在代码中,使用 transform 属性设置幕元素的旋转角度为 180 度,使其水平翻转,使用 text-shadow 属性设置幕元素的阴影效果,增强其边缘清晰度。 需要注意的是,幕特效的实现需要根据具体需求进行调整,例如可以添加动态缩放、透明渐变等其他效果。同时,为了避免幕重叠,可以设置幕元素之间的间隔,避免幕重叠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值