一篇文章教你學(xué)會(huì)js實(shí)現(xiàn)彈幕效果
下面是彈幕效果 :

相信小伙伴們都看過了,那么它實(shí)現(xiàn)的原理是什么呢,那么我們前端怎么用我們web技術(shù)去實(shí)現(xiàn)呢??
新建一個(gè)html文件:

哈哈哈,大家別像我一樣用中文命名。
中文命名是不合規(guī)范的,行走江湖,大佬們看見你的中文命名會(huì)笑話你的。
上圖中,我們引入了jquery插件,沒錯(cuò)我們用jq寫,回歸原始(找不到cdn鏈接的小伙伴可以百度bootcdn,在里面搜索jquery)。并且取了個(gè)彈幕網(wǎng)的標(biāo)題。
搞出初始模版
在這里不得不提一句,我推薦前端開發(fā)的小伙伴們用vscode來開發(fā),很好用的。
一個(gè)小技巧:在空白html文件輸入!會(huì)自動(dòng)幫你初始化html模板

模板已經(jīng)建立完畢,并且引入jquery后,正文開始了:
HTML添加
<body>
<div class="con">
<div id="screen">
<div class="dm_show">
<!-- <div>text message</div> -->
</div>
</div>
<div class="edit">
<p class="msg">
<input placeholder="說點(diǎn)什么吧?" class="content" type="text" />
</p>
<p class="btns">
<input type="button" class="send" value="發(fā)送" />
<input type="button" class="clear" value="清屏" />
</p>
</div>
</div>
</body>
一段樸實(shí)無華的html。
再來寫好css:
CSS填充
<style>
.con {
background-color: floralwhite;
padding: 40px 80px 80px;
}
#screen {
background-color: #fff;
width: 100%;
height: 380px;
border: 1px solid rgb(229, 229, 229);
font-size: 14px;
}
.content {
border: 1px solid rgb(204, 204, 204);
border-radius: 3px;
width: 320px;
height: 35px;
font-size: 14px;
margin-top: 30px;
}
.send {
border: 1px solid rgb(230, 80, 30);
color: rgb(230, 80, 0);
border-radius: 3px;
text-align: center;
padding: 0;
height: 35px;
line-height: 35px;
font-size: 14px;
width: 159px;
background-color: white;
}
.clear {
border: 1px solid;
color: darkgray;
border-radius: 3px;
text-align: center;
padding: 0;
height: 35px;
line-height: 35px;
font-size: 14px;
width: 159px;
background-color: white;
}
.edit {
margin: 20px;
text-align: center;
}
.edit .btns{
margin-top: 20px;
}
.edit .msg input{
padding-left: 5px;
}
.text {
position: absolute;
}
* {
margin: 0;
padding: 0;
}
.dm_show {
margin: 30px;
}
</style>
看看效果:

整體結(jié)構(gòu)已經(jīng)出來了,下面就是真二八經(jīng)的js:
js邏輯代碼
<script>
$(function () {
//設(shè)置“發(fā)送”按鈕點(diǎn)擊事件,將彈幕體顯示在彈幕墻上
$('.send').click(function () {
//獲取文本輸入框的內(nèi)容
var val = $('.content').val();
//將文本框的內(nèi)容賦值給val后,將文本框的內(nèi)容清除,以便用戶下一次輸入
$('.content').val('');
//將文本框內(nèi)容用div包裹起來,便于后續(xù)處理
var $content = $('<div class="text">' + val + '</div>');
//獲取彈幕墻對(duì)象
$screen = $(document.getElementById("screen"));
//設(shè)置彈幕體出現(xiàn)時(shí)的上邊距,為任意值
var top = Math.random() * $screen.height() + 40;
//設(shè)置彈幕體的上邊距和左邊距
$content.css({
top: top + "px",
left: 80
});
//將彈幕體添加到彈幕墻中
$('.dm_show').append($content);
//彈幕體從左端移動(dòng)到最右側(cè),時(shí)間為8秒,然后直接刪除該元素
$content.animate({
left: $screen.width() + 80 - $content.width()
}, 8000, function () {
$(this).remove();
});
});
//設(shè)置“清屏”點(diǎn)擊事件,清除彈幕墻中的所有內(nèi)容
$('.clear').click(function () {
$('.dm_show').empty();
});
});
</script>
意外嗎?就這么幾行,哈哈哈。
注釋里寫的很詳細(xì),小伙伴們可以好好看來看,這里我給大家演示演示:
動(dòng)畫效果

請(qǐng)?jiān)徫疫@糟糕的畫質(zhì),還好不影響看效果,這里只是簡(jiǎn)單的實(shí)現(xiàn)了一個(gè)彈幕的效果,還不能夠達(dá)到企業(yè)級(jí)的應(yīng)用,有需求的也可以自行完善,道理就是這么個(gè)道理,嘿嘿。
如果企業(yè)級(jí)視頻彈幕的話,這里也推薦dplayer播放器,一個(gè)非常完美的播放器。

關(guān)于前端彈幕的討論就差不多到此了,以上就是一篇文章教你學(xué)會(huì)用js實(shí)現(xiàn)彈幕效果的詳細(xì)內(nèi)容,更多關(guān)于js實(shí)現(xiàn)彈幕的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
yocto queue微型隊(duì)列數(shù)據(jù)結(jié)構(gòu)源碼解讀
這篇文章主要為大家介紹了yocto queue微型隊(duì)列數(shù)據(jù)結(jié)構(gòu)源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
dotenv源碼解讀從.env文件中讀取環(huán)境變量
這篇文章主要為大家介紹了dotenv源碼解讀從.env文件中讀取環(huán)境變量示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
微信小程序 簡(jiǎn)單實(shí)例(閱讀器)的實(shí)例開發(fā)
這篇文章主要介紹了微信小程序 簡(jiǎn)單實(shí)例(閱讀器)的實(shí)例開發(fā)的相關(guān)資料,需要的朋友可以參考下2016-09-09

