在要寫一個彈幕案例的時候,首先要清楚每一步要干什么。
首先搭好框架之后在要發(fā)送彈幕時應(yīng)該準備進行如下步驟:

里面還有許多細節(jié),仔細看就會有收獲!
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8" />
<title>彈幕案例</title>
<script src = "http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
var boxDom = $("#boxDom");
//var domContent = $("#domContent");
var top, right;
var pageWidth = parseInt($(document).width());
var pageHeight =parseInt($(document).height());
//點擊按鈕
$("#btn").bind("click",auto);//按鈕綁定方法
//按下回車
document.onkeydown = function(){
if(event.keyCode == 13){
auto();
}
}
function auto(){
//1.獲取輸入的字符串
var str = $(".text").val();
//2.生成一個元素
var createSpan = $("<span class = 'string' ></span>");
//3.給生成的元素賦值
createSpan.text(str);
//為了頁面友好,清空剛剛輸入的值
$(".text").val("");
//生成元素一個隨機的位置,為了使每條彈幕都出現(xiàn)在屏幕上不同的位置
top = Math.floor(Math.random()*pageHeight);
createSpan.css({"top":top, "right": -400, "color": getRandomColor()});
boxDom.append(createSpan);
//4.設(shè)置元素的動畫效果,animate(css樣式值,時間, 執(zhí)行完動畫調(diào)用的方法)
//頁面上有N個span,只讓最后一個動起來
var spandom = $("#boxDom>span:last-child");//找到最后一個span
spandom.animate({"right":pageWidth+300},10000,function(){
//移除元素
$(this).remove();
});
}
//定義一個可以生成隨機顏色的方法,可以使每條彈幕的顏色不同
function getRandomColor(){
var colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var color = "";
for(var i = 0; i < 6; i++){
color += colorArr[Math.floor(Math.random()*16)];
}
return "#"+color;
}
});
</script>
<style type="text/css">
html,body{
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
font-family: "微軟雅黑";
background: #ccc;
}
.boxDom{
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.idDom{
width: 100%;
height: 60px;
background:#666;
position: fixed;
bottom: 0px;
}
.contet{
width: 500px;
height: 40px;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
margin: auto;
}
.title{
display: inline;
font-size: 24px;
vertical-align: bottom;
color: #ffffff;
padding-left: 300px;
}
.text{
width: 300px;
height: 30px;
border:none;
border-radius:5px;
font-size: 20px;
margin-left:60px;
}
.btn{
width: 60px;
height: 30px;
color: #ffffff;
background-color: red;
border:none;
font-size:16px;
margin-left:60px;
margin-top: 20px;
}
.string {
width: 300px;
height: 40px;
margin-top: 20px;
position: absolute;
color: #000;
font-size: 20px;
font-family: "微軟雅黑";
}
</style>
</head>
<body>
<div class = "boxDom" id = "boxDom">
<img src="../images/bg_2.jpg" />
<div id = "idDom" class = "idDom">
<div class = "content">
<p class = "title"> 說點什么:</p>
<input type = "text" class = "text"/>
<button type = "button" class = "btn" id = "btn" >發(fā)送</button>
</div>
</div>
</div>
</body>
</html>另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:簡單實現(xiàn)jQuery彈幕效果-創(chuàng)新互聯(lián)
當前地址:http://chinadenli.net/article18/cdoggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、小程序開發(fā)、動態(tài)網(wǎng)站、網(wǎng)站維護、域名注冊、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容