jquery實(shí)現(xiàn)直播視頻彈幕效果
JQ實(shí)現(xiàn)彈幕效果,快來吐糟你的想法吧

代碼如下,復(fù)制即可使用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JQ實(shí)現(xiàn)彈幕效果</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="請(qǐng)輸入內(nèi)容" />
<input type="button" id="btn" value="發(fā)射" />
</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>
實(shí)例擴(kuò)展:
<!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">發(fā)射</button>
</div>
</div>
</div>
<script src="js/jquery-1.12.4.js"></script>
<script>
//生成隨機(jī)顏色
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+")";
}
//定義數(shù)組保存彈幕
var saveDan=[],index=0,tm;
//彈幕自動(dòng)移動(dòng)方法
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();
});
}
//點(diǎn)擊創(chuàng)建彈幕
$("#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);//將新的彈幕添加到數(shù)組內(nèi)容的前面
//添加彈幕列表
$("<p></p>").text(txt.val()).css("color",randomColor()).prependTo($(".list-info"));
txt.val("");//清空文本框
//自動(dòng)跑起來
clearInterval(tm);
index=saveDan.length-1
tm=setInterval(autoAnimate,1000);
});
//enter鍵確認(rèn)發(fā)送
$(document).keydown(function (e) {
var e=e||window.event;
e.keyCode==13 && ($("#btn").click());
});
</script>
</body>
</html>
到此這篇關(guān)于jquery實(shí)現(xiàn)直播視頻彈幕效果的文章就介紹到這了,更多相關(guān)JQ實(shí)現(xiàn)彈幕效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery和hwSlider實(shí)現(xiàn)內(nèi)容響應(yīng)式可觸控滑動(dòng)切換效果附源碼下載(二)
這篇文章主要介紹了jQuery和hwSlider實(shí)現(xiàn)內(nèi)容響應(yīng)式可觸控滑動(dòng)切換效果附源碼下載(二)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
jQuery焦點(diǎn)圖切換簡易插件制作過程全紀(jì)錄
本文主要講訴了本人制作一個(gè)jquery焦點(diǎn)圖切換的簡易插件的過程,十分的詳細(xì),希望對(duì)大家能有所幫助2014-08-08
一些主流JS框架中DOMReady事件的實(shí)現(xiàn)小結(jié)
在實(shí)際應(yīng)用中,我們經(jīng)常會(huì)遇到這樣的場景,當(dāng)頁面加載完成后去做一些事情:綁定事件、DOM操作某些結(jié)點(diǎn)等。2011-02-02
jquery之a(chǎn)jaxfileupload異步上傳插件(附工程代碼)
在處理文件上傳時(shí)需要使用到文件的異步上傳,這里使用Jquery Ajax File Uploader這個(gè)組件,服務(wù)器端采用struts2來處理文件上傳2013-04-04
基于jquery的點(diǎn)擊鏈接插入鏈接內(nèi)容的代碼
基于jquery的點(diǎn)擊鏈接插入鏈接內(nèi)容的代碼,感覺有bug,解決的朋友可以留言方便更多的朋友2012-07-07

