js實(shí)現(xiàn)彈幕墻效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)彈幕墻效果的具體代碼,供大家參考,具體內(nèi)容如下
1.首先要考慮彈幕墻需要什么:一面墻,輸入框,發(fā)射按鈕,關(guān)閉和開啟彈幕按鈕,在此關(guān)閉和開啟設(shè)置為同一個(gè)按鈕;2.其次彈幕上墻以后需要移動(dòng),從墻的最右邊移動(dòng)到最左邊,當(dāng)移動(dòng)到最左邊時(shí),這條彈幕就要消失;3.初步的想法應(yīng)該就是當(dāng)在輸入框輸入你要發(fā)送的內(nèi)容,點(diǎn)擊發(fā)送按鈕,在墻上會(huì)新建一個(gè)div來包含這條彈幕內(nèi)容,再給這個(gè)div相應(yīng)的移動(dòng)動(dòng)畫class;
4.彈幕顏色隨機(jī),單條彈幕之間有間隔;
取隨機(jī)顏色這里用的是
'#'+(Math.random()*0x1000000<<0).toString(16)
首先,寫出它的靜態(tài)頁面;
<!--墻--><h1>彈幕墻</h1><div id='container'></div><!--彈幕發(fā)送關(guān)閉--><div class='s_c'> <input type='text' placeholder='說點(diǎn)什么'> <div class='btn'> <button id='sent'>發(fā)射彈幕</button> <button id='clear'>關(guān)閉彈幕</button> </div></div>
css樣式
#container{ /*width:700px;*/ height:500px; margin:50px 100px; border:solid 2px #7a2a1d;}h1{ text-align:center;}.s_c{ width:500px; margin:0 auto;}#message{ width:400px; height:30px; margin:0 auto; position:relative; left:50px;}.btn{ padding-top:20px; height:30px; margin-left:150px;}#sent,#clear{ width:100px;}
js代碼部分:
var arr = [];//用于保存彈幕數(shù)據(jù)的數(shù)組;var start = true;//用于判斷是否需要開啟彈幕 $(document).ready(function(){ var showscreen = $('#container');//彈幕墻的div var showHeight = showscreen.height();//彈幕墻div的高度 var showWidth = showscreen.width();//彈幕墻div的寬度 //點(diǎn)擊發(fā)射按鈕事件 $('#sent').click(function(){ var text = $('#message').val();//獲取用戶輸入的待發(fā)送彈幕 $('#message').val('');//清空彈幕發(fā)送區(qū) arr.push(text);//將數(shù)據(jù)存入實(shí)現(xiàn)定義好的用于保存彈幕數(shù)據(jù)的數(shù)組 var send_div=$('<div>'+text+'</div>'); showscreen.append(send_div); // var send_text=$('<div>+text+</div>');//新建div彈幕條 // var send_div = document.createElement('div'); // var inner = document.createTextNode(text); // send_div.appendChild(inner); // document.getElementById('container').appendChild(send_div)//把彈幕掛在墻上 move_text(send_div); }) //按回車發(fā)送 $('input').keydown(function(event){ if(event.keyCode == 13){ $('#sent').trigger('click');//trigger觸發(fā)被選元素的指定事件類型,觸發(fā)#send事件的click類型 } }) if(start==false){ start = true; $('#clear').html('關(guān)閉彈幕'); run(); } //關(guān)閉/開啟彈幕按鈕點(diǎn)擊事件 $('#clear').click(function(){ if(start == true){start = false;$('#clear').html('開啟彈幕');showscreen.empty(); }else if(start == false){start = true;$('#clear').html('關(guān)閉彈幕');run() } }); var topMin = showscreen.offset().top; var topMax = topMin+showHeight; var top = topMin; var move_text = function(obj){ obj.css({ display:'inline', position:'absolute' }) var begin = showscreen.width() - obj.width(); //一開始的起點(diǎn) top+=50; if(top > topMax-50){ top = topMin; } //console.log('showscreenWidth'+showscreen.width()); //console.log('objWidth',obj.width()); obj.css({ left:begin, top:top, color:getRandomColor() }); var time = 20000 + 10000*Math.random(); obj.animate({ left:'-'+begin+'px' },time,function(){ obj.remove(); }); }; var getRandomColor = function(){ return ’#’+(’00000’+(Math.random()*0xffffff <<0).toString(16)).substr(-6); } var run = function(){ if(start == true){if(arr.length > 0){ var n = Math.floor(Math.random()* arr.length + 1)-1; var textObj = $('<div>'+arr[n]+'</div>'); showscreen.append(textObj); //console.log('loop:'+textObj.html()); move_text(textObj);} } setTimeout(run,3000); } jQuery.fx.interval = 50; run();})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Java類加載機(jī)制實(shí)現(xiàn)步驟解析2. python3實(shí)現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)3. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊4. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決5. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享6. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼7. 匹配模式 - XSL教程 - 48. ASP中if語句、select 、while循環(huán)的使用方法9. ASP常用日期格式化函數(shù) FormatDate()10. html小技巧之td,div標(biāo)簽里內(nèi)容不換行
