本文實(shí)例為大家分享了js實(shí)現(xiàn)彈幕特效的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、隴南網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為隴南等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
此處使用HBuilder編譯,最簡(jiǎn)單的彈幕效果,希望各位前輩不吝指教。
注意用的是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ù)不得超過(guò)15個(gè)!"); return; } var bullet = $("<div>"); //生成一條彈幕 bullet.text(msg); //將輸入框內(nèi)容放置到div中 bullet.addClass("bullet"); //為bullet這個(gè)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)該隨著彈幕的長(zhǎng)短而變化 },Math.round(Math.random()*9000)+1000,"linear", function(){ bullet.remove(); //當(dāng)運(yùn)動(dòng)結(jié)束時(shí),刪除彈幕 }); $(".content").append(bullet); //將彈幕添加到屏幕中 }); </script> </body> </html>
下面是展示的彈幕效果(顯示的白線在谷歌瀏覽器中是看不出來(lái)的,在錄制時(shí)會(huì)看出來(lái),此處又一bug):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章標(biāo)題:基于JavaScript實(shí)現(xiàn)彈幕特效
URL分享:http://jinyejixie.com/article48/ipgghp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、云服務(wù)器、軟件開(kāi)發(fā)、微信公眾號(hào)、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)