ajax实现直播功能,jquery实现直播弹幕效果

本文实例为大家分享了jquery直播弹幕展示的具体代码,供大家参考,具体内容如下

理论知识:

实现效果主要涉及jq对节点的操作,以及动画函数的使用…

思路:

1.获取到评论文本框的值,对文本内容进行处理

2.动态创建一条弹幕,将创建的弹幕动态的添加到需要显示的页面,并同时创建一条新的评论消息,动态添加到评论列表

3.利用数组将每次添加的弹幕保存起来,使用定时器让所有的弹幕循环动态的展示出来

大体上的思路就是这样,下面直接贴代码?

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;

}

吐槽:

发射

//生成随机颜色

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= $("").text(txt.val()).css({

"color":randomColor(),

"top":randomTop,

"right":$(".list-info").width(),

"z-index":888

});

saveDan.unshift(span);//将新的弹幕添加到数组内容的前面

//添加弹幕列表

$("

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());

});

最后看看最终的效果图,模仿直播弹幕完成了

eabf35500b29eca09d73c6adbe0ce3b6.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值