新聞中心
這篇文章主要介紹了jQuery如何實現(xiàn)彈幕APP,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
今天閑著無聊,寫了個彈幕APP,主要實現(xiàn)以下幾個功能:
1.點擊“彈幕發(fā)射”或回車可以輸出彈幕到彈幕墻上。
2.彈幕的運行軌跡是從彈幕墻的最右邊到最左邊,Y軸的數(shù)值在彈幕墻的高度內(nèi)隨機,顏色HEX隨機,速度隨機。
3.右側(cè)的表格可以儲存彈幕內(nèi)容以及彈幕的發(fā)射時間,越靠近現(xiàn)在的越靠前。
4.點擊“清除彈幕”可以把彈幕墻內(nèi)的所有彈幕清除掉,但不會影響到表格中的數(shù)據(jù)。
5.如果彈幕長度過長(我設置的是6個字符),則超過規(guī)定長度之外的彈幕內(nèi)容都會由“...”代替,并放入表格中。但彈幕墻中的內(nèi)容依然是完整的。
HTML代碼:
彈幕內(nèi)容 彈幕時間
*使用了Bootstrap3框架。
JQuery部分:
$(document).ready(function() { $(".shoot").on("click", startDanmu); $("form").keypress(function(event) { if (event.keyCode === 13) { event.preventDefault(); startDanmu(); } }); $(".clear").on("click", clearDanmu); }); //get random number in certain range function RandomNum(Min, Max) { var Range = Max - Min; var Rand = Math.random(); var num = Min + Math.round(Rand * Range); return num; } //time number add 0 before if <10 function plusZero(x) { if (x < 10) { x = "0" + x; } else { x = x; } return x; } //start danmu function startDanmu() { var message = $("input"); var messageVal = message.val(); var danmuMessage = "" + messageVal + ""; //get random color HEX //u can also save the colors u want by array var color = RandomNum(100000, 999999); //get random danmu speed var speed = RandomNum(10000, 20000); //get random position Y //danmu box height is 450, we set the danmu position Y max 400 in case it blocks the subtitle var positionY = RandomNum(50, 400); if (messageVal.length > 0) { //insert danmu message into danmu box $(".danmu-box").prepend(danmuMessage); //have to use first() cuz we prepend the message, u can try what's gonna happen if no first() //set it's style $(".danmu-message").first().css({ "right": "0", "top": positionY, "color": "#" + color }); //set it's animation //from right 0 to left 0 //hide it after move $(".danmu-message").first().animate({ left: '0px', }, speed, function() { $(this).fadeOut(); }); //get danmu time var time = new Date(); var month = time.getMonth() + 1; var day = time.getDay(); var hour = time.getHours(); var minute = time.getMinutes(); var danmuTime = plusZero(month) + "-" + plusZero(day) + " " + plusZero(hour) + ":" + plusZero(minute); //insert danmu message to table if (messageVal.length > 6) { messageVal = messageVal.substring(0, 6) + "..."; } var messageToTable = ""; $(".danmu-table > tbody").prepend(messageToTable); } else {} //empty the input message.val(""); } //clear danmu box function clearDanmu() { $(".danmu-box").html(""); } " + messageVal + " " + danmuTime + "
感謝你能夠認真閱讀完這篇文章,希望小編分享的“jQuery如何實現(xiàn)彈幕APP”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)建站,關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,更多相關知識等著你來學習!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:jQuery如何實現(xiàn)彈幕APP-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/dsgspe.html