jquery实现直播视频弹幕效果


Posted in jQuery onFebruary 25, 2020

JQ实现弹幕效果,快来吐糟你的想法吧

jquery实现直播视频弹幕效果

代码如下,复制即可使用:

<!DOCTYPE html>

<html>

 <head>

  <meta charset="utf-8" />

  <title>JQ实现弹幕效果</title>

  <style type="text/css">

   *{

    padding: 0;

    margin: 0;

   }

   #box{

    height:700px;

    width:1000px;

    margin: 0 auto;

    border:1px solid #000000;

    position: relative;

   }

   #main{

    width:100%;

    height:605px;

    position: relative;

    overflow: hidden;

   }

   p{

    position: absolute;

    left:1000px;

    width:200px;

    top:0;

   }

   #bottom{

    width:100%;

    height:80px;

    background: #ABCDEF;

    text-align: center;

    padding-top: 15px;

    position: absolute;

    left: 0;

    bottom: 0;

   }

   #txt{

    width:300px;

    height:50px; 

   }

   #btn{

    width:100px;

    height:50px;    

   }

  </style>

 </head>

 <body>

  <div id="box">

   <div id="main">

     

   </div>

   <div id="bottom">

    <input type="text" id="txt" placeholder="请输入内容" />

    <input type="button" id="btn" value="发射" />

   </div>

  </div>

  <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>

  <script type="text/javascript">

   $(function(){

    var pageH=parseInt($("#main").height());

    var colorArr=["#cfaf12","#12af01","#981234","#adefsa","#db6be4","#f5264c","#d34a74"];    

    $("#btn").bind("click",auto);

    document.onkeydown=function(e){

     if(e.keyCode == 13){

      auto();

     }

    };

    function auto(){

     var $value = $("#txt").val();

     $("#main").append("<p>" + $value + "</p>");

     $("#txt").val("");

     var _top=parseInt(pageH*(Math.random()));

     var num=parseInt(colorArr.length*(Math.random()));

     $("p:last-child").css({"top":_top,"color":colorArr[num],"font-size":"20px"});

     $("p:last-child").animate({"left":"-200px"},10000);

     $("p:last-child").stop().animate({"left":"-300px"},10000,"linear",function(){

      $(this).remove();

     });    

     //console.log($value);

    };

     

   })

  </script>

 </body>

</html>

实例扩展

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
    html, body {
      margin: 0px;
      padding: 0px;
      width: 100%;
      height: 100%;
      font-family: "微软雅黑";
      font-size: 62.5%;
    }
 
    .boxDom {
      width: 100%;
      height: 100%;
      position: relative;
      overflow: hidden;
    }
 
    .idDom {
      width: 100%;
      height: 10%;
      background: #666;
      position: fixed;
      bottom: 0px;
    }
 
    .content {
      display: inline-block;
      width: 460px;
      height: 40px;
      position: absolute;
      left: 0px;
      right: 0px;
      top: 0px;
      bottom: 0px;
      margin: auto;
    }
 
    .title {
      display: inline;
      font-size: 4em;
      vertical-align: bottom;
      color: #fff;
    }
 
    .text {
      border: none;
      width: 300px;
      height: 30px;
      border-radius: 5px;
      font-size: 2.4em;
    }
 
    .btn {
      width: 60px;
      height: 30px;
      background: #f90000;
      border: none;
      color: #fff;
      font-size: 2.4em;
    }
 
    span {
      height: 40px;
      position: absolute;
      overflow: hidden;
      color: #000;
      font-size: 4em;
      line-height: 1.5em;
      cursor: pointer;
      white-space: nowrap;
    }
    #boxDom video{
      width: 80%;
      height: 90%;
      float: left;
    }
    #boxDom .list-info{
      width: 20%;
      height: 90%;
      float: left;
      background-color: #666;
      overflow-y: auto;
      overflow-x: hidden;
      position: relative;
      z-index: 999;
    }
    .list-info p{
      font-size: 15px;
      line-height: 20px;
      border-bottom: 1px dashed #ccc;
      text-indent: 2em;
      color: #fff;
    }
 
  </style>
</head>
<body>
<div class="boxDom" id="boxDom">
  <video controls="controls" src="movie/10_2fdd2bbcd15c68497744f07d4527cef2_3.mp4"></video>
  <div class="list-info"> </div>
  <div class="idDom" id="idDom">
    <div class="content">
      <p class="title">吐槽:</p>
      <input type="text" class="text" id="text"/>
      <button type="button" class="btn" id="btn">发射</button>
    </div>
  </div>
</div>
<script src="js/jquery-1.12.4.js"></script>
<script>
 //生成随机颜色
  var randomColor=function(){
    var r= Math.floor(Math.random()*257);
    var g= Math.floor(Math.random()*257);
    var b= Math.floor(Math.random()*257);
    return "rgb("+r+","+g+","+b+")";
  }
  //定义数组保存弹幕
  var saveDan=[],index=0,tm;
  //弹幕自动移动方法
  var autoAnimate=function(){
    index++;
    index>saveDan.length-1 && (index=0);
    saveDan[index].appendTo($("#boxDom")).animate({
      "right":$(document).width(),
    },10000,function () {
      $(this).css("right",$(".list-info").width()-$(this).width()).remove();
    });
  }
  //点击创建弹幕
  $("#btn").click(function () {
    var txt=$("#text");
    var randomTop=Math.floor(Math.random()*($("#boxDom video").height()-40));
    if(txt.val().trim().length==0)return;
    var span= $("<span></span>").text(txt.val()).css({
      "color":randomColor(),
      "top":randomTop,
      "right":$(".list-info").width(),
      "z-index":888
    });
    saveDan.unshift(span);//将新的弹幕添加到数组内容的前面
    //添加弹幕列表
    $("<p></p>").text(txt.val()).css("color",randomColor()).prependTo($(".list-info"));
    txt.val("");//清空文本框
    //自动跑起来
    clearInterval(tm);
    index=saveDan.length-1
    tm=setInterval(autoAnimate,1000);
  });
  //enter键确认发送
  $(document).keydown(function (e) {
    var e=e||window.event;
    e.keyCode==13 && ($("#btn").click());
  });
</script>
</body>
</html>

到此这篇关于jquery实现直播视频弹幕效果的文章就介绍到这了,更多相关JQ实现弹幕效果内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

jQuery 相关文章推荐
jquery中关于bind()方法的使用技巧分享
Mar 30 jQuery
jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
May 19 jQuery
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
Jun 26 jQuery
jQuery中.attr()和.data()的区别分析
Sep 03 jQuery
JQuery用$.ajax或$.getJSON跨域获取JSON数据的实现代码
Sep 23 jQuery
jQuery实现鼠标移到某个对象时弹出显示层功能
Aug 23 jQuery
用jQuery将JavaScript对象转换为querystring查询字符串的方法
Nov 12 jQuery
JQuery判断radio单选框是否选中并获取值的方法
Jan 17 jQuery
jQuery Ajax async=&gt;false异步改为同步时,解决导致浏览器假死的问题
Jul 22 jQuery
jquery实现直播弹幕效果
Nov 28 jQuery
jquery实现吸顶导航效果
Jan 08 jQuery
jQuery带控制按钮轮播图插件
Jul 31 jQuery
jquery制作的移动端购物车效果完整示例
Feb 24 #jQuery
jquery实现的放大镜效果示例
Feb 24 #jQuery
jquery向后台提交数组的代码分析
Feb 20 #jQuery
jquery实现垂直手风琴导航栏
Feb 18 #jQuery
jQuery表单校验插件validator使用方法详解
Feb 18 #jQuery
jquery传参及获取方式(两种方式)
Feb 13 #jQuery
jQuery实现简易QQ聊天框
Feb 10 #jQuery
You might like
一个MYSQL操作类
2006/11/16 PHP
codeigniter集成ucenter1.6双向通信的解决办法
2014/06/12 PHP
PHP扩展CURL的用法详解
2014/06/20 PHP
php获取从html表单传递数组的方法
2015/03/20 PHP
PHP中使用register_shutdown_function函数截获fatal error示例
2015/04/21 PHP
php中Snoopy类用法实例
2015/06/19 PHP
PHP自动补全表单的两种方法
2017/03/06 PHP
PHP实现随机生成水印图片功能
2017/03/22 PHP
PHP手机短信验证码实现流程详解
2018/05/17 PHP
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
JavaScript 嵌套函数指向this对象错误的解决方法
2010/03/15 Javascript
ASP.NET jQuery 实例11 通过使用jQuery validation插件简单实现用户登录页面验证功能
2012/02/03 Javascript
一个奇葩的最短的 IE 版本判断JS脚本
2014/05/28 Javascript
在JavaScript中构建ArrayList示例代码
2014/09/17 Javascript
Bootstrap轮播图学习使用
2017/02/10 Javascript
JS数组搜索之折半搜索实现方法分析
2017/03/27 Javascript
JS简单判断滚动条的滚动方向实现方法
2017/04/28 Javascript
Nodejs处理异常操作示例
2018/12/25 NodeJs
js实现点赞按钮功能的实例代码
2020/03/06 Javascript
python数据类型判断type与isinstance的区别实例解析
2017/10/31 Python
python微信跳一跳系列之棋子定位颜色识别
2018/02/26 Python
对python中的xlsxwriter库简单分析
2018/05/04 Python
python去除文件中重复的行实例
2018/06/29 Python
Windows下实现将Pascal VOC转化为TFRecords
2020/02/17 Python
如何在sublime编辑器中安装python
2020/05/20 Python
解决python运行启动报错问题
2020/06/01 Python
tensorflow使用CNN分析mnist手写体数字数据集
2020/06/17 Python
Python使用windows设置定时执行脚本
2020/11/12 Python
Python3利用openpyxl读写Excel文件的方法实例
2021/02/03 Python
浅析css3中matrix函数的使用
2016/06/06 HTML / CSS
淘宝网店营销策划书
2014/01/11 职场文书
星级党支部申报材料
2014/05/31 职场文书
单位实习鉴定评语
2015/01/04 职场文书
毕业班工作总结
2015/08/10 职场文书
python opencv常用图形绘制方法(线段、矩形、圆形、椭圆、文本)
2021/04/12 Python
Python还能这么玩之用Python修改了班花的开机密码
2021/06/04 Python