在要寫(xiě)一個(gè)彈幕案例的時(shí)候,首先要清楚每一步要干什么。
首先搭好框架之后在要發(fā)送彈幕時(shí)應(yīng)該準(zhǔn)備進(jìn)行如下步驟:
里面還有許多細(xì)節(jié),仔細(xì)看就會(huì)有收獲!
<!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()); //點(diǎn)擊按鈕 $("#btn").bind("click",auto);//按鈕綁定方法 //按下回車 document.onkeydown = function(){ if(event.keyCode == 13){ auto(); } } function auto(){ //1.獲取輸入的字符串 var str = $(".text").val(); //2.生成一個(gè)元素 var createSpan = $("<span class = 'string' ></span>"); //3.給生成的元素賦值 createSpan.text(str); //為了頁(yè)面友好,清空剛剛輸入的值 $(".text").val(""); //生成元素一個(gè)隨機(jī)的位置,為了使每條彈幕都出現(xiàn)在屏幕上不同的位置 top = Math.floor(Math.random()*pageHeight); createSpan.css({"top":top, "right": -400, "color": getRandomColor()}); boxDom.append(createSpan); //4.設(shè)置元素的動(dòng)畫(huà)效果,animate(css樣式值,時(shí)間, 執(zhí)行完動(dòng)畫(huà)調(diào)用的方法) //頁(yè)面上有N個(gè)span,只讓最后一個(gè)動(dòng)起來(lái) var spandom = $("#boxDom>span:last-child");//找到最后一個(gè)span spandom.animate({"right":pageWidth+300},10000,function(){ //移除元素 $(this).remove(); }); } //定義一個(gè)可以生成隨機(jī)顏色的方法,可以使每條彈幕的顏色不同 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"> 說(shuō)點(diǎn)什么:</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)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果-創(chuàng)新互聯(lián)
當(dāng)前地址:http://muchs.cn/article18/cdoggp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、小程序開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、域名注冊(cè)、網(wǎng)站內(nèi)鏈
聲明:本網(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)
猜你還喜歡下面的內(nèi)容