亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2021年08月23日 10:31:13   作者:紙 飛機(jī)  
彈幕效果隨著b站的越做越強(qiáng),出現(xiàn)了越來(lái)越多的仿照b站的視頻站點(diǎn)。然而這些視頻站仿照的最多的只有一點(diǎn)!那就是彈幕,現(xiàn)在也越來(lái)越多的人喜歡上了彈幕本文就教你如何制作

下面是彈幕效果 :

彈幕效果

相信小伙伴們都看過(guò)了,那么它實(shí)現(xiàn)的原理是什么呢,那么我們前端怎么用我們web技術(shù)去實(shí)現(xiàn)呢??

新建一個(gè)html文件:

哈哈哈,大家別像我一樣用中文命名。

中文命名是不合規(guī)范的,行走江湖,大佬們看見(jiàn)你的中文命名會(huì)笑話你的。

上圖中,我們引入了jquery插件,沒(méi)錯(cuò)我們用jq寫,回歸原始(找不到cdn鏈接的小伙伴可以百度bootcdn,在里面搜索jquery)。并且取了個(gè)彈幕網(wǎng)的標(biāo)題。

搞出初始模版

在這里不得不提一句,我推薦前端開(kāi)發(fā)的小伙伴們用vscode來(lái)開(kāi)發(fā),很好用的。

一個(gè)小技巧:在空白html文件輸入!會(huì)自動(dòng)幫你初始化html模板

模板已經(jīng)建立完畢,并且引入jquery后,正文開(kāi)始了:

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="說(shuō)點(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í)無(wú)華的html。

再來(lái)寫好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)出來(lái)了,下面就是真二八經(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包裹起來(lái),便于后續(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ì),小伙伴們可以好好看來(lái)看,這里我給大家演示演示:

動(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)文章

最新評(píng)論