基于jQuery實(shí)現(xiàn)彈幕APP
今天閑著無(wú)聊,寫了個(gè)彈幕APP,主要實(shí)現(xiàn)以下幾個(gè)功能:
1.點(diǎn)擊“彈幕發(fā)射”或回車可以輸出彈幕到彈幕墻上。
2.彈幕的運(yùn)行軌跡是從彈幕墻的最右邊到最左邊,Y軸的數(shù)值在彈幕墻的高度內(nèi)隨機(jī),顏色HEX隨機(jī),速度隨機(jī)。
3.右側(cè)的表格可以儲(chǔ)存彈幕內(nèi)容以及彈幕的發(fā)射時(shí)間,越靠近現(xiàn)在的越靠前。
4.點(diǎn)擊“清除彈幕”可以把彈幕墻內(nèi)的所有彈幕清除掉,但不會(huì)影響到表格中的數(shù)據(jù)。
5.如果彈幕長(zhǎng)度過(guò)長(zhǎng)(我設(shè)置的是6個(gè)字符),則超過(guò)規(guī)定長(zhǎng)度之外的彈幕內(nèi)容都會(huì)由“...”代替,并放入表格中。但彈幕墻中的內(nèi)容依然是完整的。

HTML代碼:
<div class="frame"> <div class="row"> <div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 danmu-box-frame"> <div class="danmu-box"> </div> </div> <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 danmu-table-frame"> <table class="table .table-condensed danmu-table"> <thead> <tr> <th> 彈幕內(nèi)容 </th> <th> 彈幕時(shí)間 </th> </tr> </thead> <tbody> </tbody> </table> </div> </div> <div class="danmu-form"> <form class="form-inline"> <input type="text" class="form-control" placeholder="開始吐槽!"> <button type="button" class="btn btn-primary shoot"> 發(fā)射彈幕! </button> <button type="button" class="btn btn-danger clear"> 清空彈幕 </button> </form> </div> </div> <hr> <footer> Designed By <a target="_blank"> Alen Hu </a> </footer>
*使用了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 = "<span class='danmu-message'>" + messageVal + "</span>";
//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 = "<tr><td>" + messageVal + "</td><td>" + danmuTime + "</td></tr>";
$(".danmu-table > tbody").prepend(messageToTable);
} else {}
//empty the input
message.val("");
}
//clear danmu box
function clearDanmu() {
$(".danmu-box").html("");
}
DEMO在這兒,歡迎來(lái)FORK:Danmu APP。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)七夕表白彈幕效果 jQuery實(shí)現(xiàn)彈幕技術(shù)
- jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕制作
- jQuery實(shí)現(xiàn)彈幕特效
- jquery實(shí)現(xiàn)直播彈幕效果
- jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕效果
- 簡(jiǎn)單實(shí)現(xiàn)jQuery彈幕效果
- 基于jquery實(shí)現(xiàn)彈幕效果
- 又一枚精彩的彈幕效果jQuery實(shí)現(xiàn)
- 終于實(shí)現(xiàn)了!精彩的jquery彈幕效果
- jQuery實(shí)現(xiàn)彈幕效果案例
相關(guān)文章
jQuery移動(dòng)端跑馬燈抽獎(jiǎng)特效升級(jí)版(抽獎(jiǎng)概率固定)實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery移動(dòng)端跑馬燈抽獎(jiǎng)特效升級(jí)版(抽獎(jiǎng)概率固定)實(shí)現(xiàn)方法,涉及jQuery數(shù)值運(yùn)算及條件判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
一個(gè)簡(jiǎn)單的實(shí)現(xiàn)下拉框多選的插件可移植性比較好
一個(gè)簡(jiǎn)單的實(shí)現(xiàn)下拉框多選的插件,可移植性也比較好,需要的朋友可以參考下2014-05-05
深入淺析ng-bootstrap 組件集中 tabset 組件的實(shí)現(xiàn)分析
這篇文章主要介紹了ng-bootstrap 組件集中 tabset 組件的實(shí)現(xiàn)分析 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
基于EasyUI的基礎(chǔ)之上實(shí)現(xiàn)樹形功能菜單
這篇文章主要介紹了基于EasyUI的基礎(chǔ)之上實(shí)現(xiàn)樹形功能菜單,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
基于jQuery實(shí)現(xiàn)返回頂部實(shí)例代碼
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)返回頂部實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-01-01

