基于JavaScript實現(xiàn)彈幕特效
本文實例為大家分享了js實現(xiàn)彈幕特效的具體代碼,供大家參考,具體內(nèi)容如下
此處使用HBuilder編譯,最簡單的彈幕效果,希望各位前輩不吝指教。
注意用的是jquery-2.0.3.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.shooter{
width: 600px;
height: 60px;
/*background: black;*/
margin: 0 auto;
}
.shooter input{
width: 300px;
height: 40px;
border: none;
border-radius: 7px;
box-shadow: 0 0 8px rgba(182,195,214,0.6)inset;
padding-left: 15px;
margin-top: 10px;
}
.shooter button{
width: 80px;
height: 40px;
border: none;
margin-left: 10px;
background-color:#339B53;
border-radius:8px;
color: white;
cursor: pointer;
}
.shooter button:hover{
font-size: 14px;
background:#008000;
}
.content{
width: 100%;
height: 600px;
background: gray;
position: relative;
overflow: hidden;
}
.bullet{
position: absolute;
/*right: 0;*/
/*left:1600px;*/
word-break: keep-all;
/*不讓單詞折行*/
}
</style>
<body>
<div class="shooter">
<input type="text"/>
<button>發(fā)射</button>
</div>
<div class="content">
</div>
<script type="text/javascript" src="js/jquery-2.0.3.js" ></script>
<script type="text/javascript">
$("button").click(function(){
var msg = $("input").val();
//取出輸入框內(nèi)容
if(msg.length > 15){
alert("字?jǐn)?shù)不得超過15個!");
return;
}
var bullet = $("<div>");
//生成一條彈幕
bullet.text(msg);
//將輸入框內(nèi)容放置到div中
bullet.addClass("bullet");
//為bullet這個div添加樣式bullet
bullet.css("top",Math.round(Math.random()*500));
//隨機(jī)設(shè)置彈幕位置
bullet.css("left","1600px");
bullet.css("font-size",Math.round(Math.random()*60)+12+"px");
bullet.css("color","rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")");
// alert(window.getComputedStyle(bullet,null).width);
bullet.animate({
left:-1000//此處視為bug,應(yīng)該隨著彈幕的長短而變化
},Math.round(Math.random()*9000)+1000,"linear", function(){
bullet.remove();
//當(dāng)運(yùn)動結(jié)束時,刪除彈幕
});
$(".content").append(bullet);
//將彈幕添加到屏幕中
});
</script>
</body>
</html>
下面是展示的彈幕效果(顯示的白線在谷歌瀏覽器中是看不出來的,在錄制時會看出來,此處又一bug):

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
TypeScript快速上手語法及結(jié)合vue3用法詳解
TypeScript是一種由微軟開發(fā)的自由開源的編程語言,主要提供了類型系統(tǒng)和對ES6的支持,下面這篇文章主要給大家介紹了關(guān)于TypeScript快速上手語法及結(jié)合vue3用法的相關(guān)資料,需要的朋友可以參考下2024-02-02
JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome)
本篇文章主要介紹了JS控制圖片翻轉(zhuǎn)示例代碼(兼容firefox,ie,chrome) 需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
js立即執(zhí)行函數(shù): (function ( ){})( ) 與 (function ( ){}( )) 有什么區(qū)別?
這篇文章主要介紹了js立即執(zhí)行函數(shù): (function ( ){})( ) 與 (function ( ){}( )) 有什么區(qū)別,需要的朋友可以參考下2015-11-11
JavaScript利用normalizr實現(xiàn)復(fù)雜數(shù)據(jù)轉(zhuǎn)換
當(dāng)我們需要進(jìn)行數(shù)據(jù)轉(zhuǎn)換以便拆分和維護(hù)時,可以使用redux作者 Dan Abramov 編寫的normalizr來處理數(shù)據(jù),本文將為大家詳細(xì)講講其用法,感興趣的可以了解一下2022-07-07
JS交互點擊WKWebView中的圖片實現(xiàn)預(yù)覽效果
這篇文章主要介紹了JS交互點擊WKWebView中的圖片實現(xiàn)預(yù)覽效果,需要的朋友可以參考下2018-01-01

