jquery實(shí)現(xiàn)直播彈幕效果
本文實(shí)例為大家分享了jquery直播彈幕展示的具體代碼,供大家參考,具體內(nèi)容如下
理論知識(shí):
實(shí)現(xiàn)效果主要涉及jq對(duì)節(jié)點(diǎn)的操作,以及動(dòng)畫(huà)函數(shù)的使用…
思路:
1.獲取到評(píng)論文本框的值,對(duì)文本內(nèi)容進(jìn)行處理
2.動(dòng)態(tài)創(chuàng)建一條彈幕,將創(chuàng)建的彈幕動(dòng)態(tài)的添加到需要顯示的頁(yè)面,并同時(shí)創(chuàng)建一條新的評(píng)論消息,動(dòng)態(tài)添加到評(píng)論列表
3.利用數(shù)組將每次添加的彈幕保存起來(lái),使用定時(shí)器讓所有的彈幕循環(huán)動(dòng)態(tài)的展示出來(lái)
大體上的思路就是這樣,下面直接貼代碼?
<!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)跑起來(lái)
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>
最后看看最終的效果圖,模仿直播彈幕完成了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)七夕表白彈幕效果 jQuery實(shí)現(xiàn)彈幕技術(shù)
- jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕制作
- jQuery實(shí)現(xiàn)彈幕特效
- jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕效果
- 簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果
- 基于jQuery實(shí)現(xiàn)彈幕APP
- 基于jquery實(shí)現(xiàn)彈幕效果
- 又一枚精彩的彈幕效果jQuery實(shí)現(xiàn)
- 終于實(shí)現(xiàn)了!精彩的jquery彈幕效果
- jQuery實(shí)現(xiàn)彈幕效果案例
相關(guān)文章
jquery 多個(gè)radio的click事件實(shí)例
下面小編就為大家?guī)?lái)一篇jquery 多個(gè)radio的click事件實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦2016-12-12
基于jQuery的JavaScript模版引擎JsRender使用指南
這篇文章主要介紹了基于jQuery的JavaScript模版引擎JsRender使用指南,需要的朋友可以參考下2014-12-12
jQuery中:last-child選擇器用法實(shí)例
這篇文章主要介紹了jQuery中:last-child選擇器用法,實(shí)例分析了:last-child選擇器功能、定義及匹配父元素的最后一個(gè)子元素用法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
jQuery validate插件實(shí)現(xiàn)ajax驗(yàn)證重復(fù)的2種方法
這篇文章主要介紹了jQuery validate插件實(shí)現(xiàn)ajax驗(yàn)證重復(fù)的2種方法,結(jié)合完整實(shí)例形式分析了jQuery validate插件的使用技巧,需要的朋友可以參考下2016-01-01
jQuery niceScroll滾動(dòng)條錯(cuò)位問(wèn)題的解決方法
下面小編就為大家?guī)?lái)一篇jQuery niceScroll滾動(dòng)條錯(cuò)位問(wèn)題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
jQuery實(shí)現(xiàn)ajax調(diào)用WCF服務(wù)的方法(附帶demo下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)ajax調(diào)用WCF服務(wù)的方法,以完整實(shí)例形式分析了jQuery的ajax前端調(diào)用及后臺(tái)交互調(diào)用WCF服務(wù)的相關(guān)技巧,并附帶完整實(shí)例共讀者下載,需要的朋友可以參考下2015-12-12
基于JQuery實(shí)現(xiàn)的Select級(jí)聯(lián)
Select級(jí)聯(lián),想必大并不陌生吧,本文為大家介紹下使用jquery是如何快速實(shí)現(xiàn)的,希望對(duì)大家有所幫助2014-01-01

