vue寫一個(gè)簡單的彈幕動(dòng)效

因?yàn)橛泻芏嗟胤焦眠@個(gè)彈幕效果,所以做成了組件,方便引用。
該功能需要掌握vue的組件和動(dòng)畫。

template部分

<template>
    <div>
        <transition name="bullet"  @after-enter="afterEnter" @after-leave="afterLeave">
            <div v-show="isShowBullet" class="bullet-screen" v-bind:style="{'top': top,'left': left}">
                <span>{{bulletList[0]}}</span>
            </div>
        </transition>
    </div>
</template>

js部分

<script>
    export default {
        data(){
            return {
                isShowBullet: !1,
                bulletInterval: 1,
                bulletList: [],
                timer: null,
                afterTimer: null
            }
        },
        props:{
            bullets: {
                type: Object
            },
            paused: {
                type: Boolean,
                default: !1
      },
      top:{  type: String },
      left:{  type: String }
        },
        methods: {
            bulletEnter(el, done){
                el.classList.add('bullet-enter-active');
                setTimeout(()=>{
                    this.isShowBullet = !1;
                    el.classList.remove('bullet-enter-active');
                    setTimeout(()=>{
                        this.isShowBullet = !0;
                    },500);
                },2000);
            },
            bulletComplete(el){
                console.log(1)
            },
            afterEnter(){
                this.afterTimer = setTimeout(() => {
                    this.isShowBullet = !1;
                }, 2000);
            },
            afterLeave(){
                this.bulletList.shift();
                if(this.bulletList.length<=0 || this.paused){
                    return;
                }
                this.showBullet();
            },
            showBullet(){
                if(this.timer){
                    clearTimeout(this.timer);
                }
                this.timer = setTimeout(() => {
                    this.isShowBullet = !0;
                }, this.bulletInterval * 1000);
            }
        },
        watch:{
            bullets(val){
                this.bulletInterval = val.frequency;
                this.bulletList = val.msglist;
                if (this.bulletList.length > 0) {
                    this.showBullet();
                }
            },
            paused(isPaused){
                if(isPaused){
                    this.isShowBullet = !1;
                    clearTimeout(this.timer);
                    clearTimeout(this.afterTimer);
                }else{
                    this.showBullet();
                }
            }
        }
    }
</script>

css部分

<style scoped>
    .bullet-screen{
        position: absolute;
        left: 0.32rem;
        top: 0.6rem;
        height: 0.54rem;
        line-height: 0.56rem;
        max-width: 4rem;
        padding: 0 0.3rem;
        border-radius: 0.06rem;
        color: #fff;
        font-size: 0.24rem;
        text-align: center;
        background-image: linear-gradient(90deg, #FF8C00 0, #FF5400 100% );
        z-index: 2000;
        box-sizing: border-box;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        opacity: 0.8;
    }
    .bullet-enter-active{
        opacity: 0.8;
        transform: translate3d(0, 0, 0);
        transition: all .5s ease-in-out;
    }
    .bullet-enter{
        opacity: 0;
        transform: translate3d(0, 150%, 0);
    }
    .bullet-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }
    .bullet-leave-to{
        opacity: 0;
        transform: translate3d(0, -100%, 0);
    }
    @keyframes bullet{
        0%{
            opacity: 0;
            transform: translate3d(0, 150%, 0);
        }
        25%{
            opacity: 1;
            transform: translate3d(0, 0, 0);
        }
        75%{
            opacity: 1;
            transform: translate3d(0, 0, 0);
        }
        100%{
            opacity: 0;
            transform: translate3d(0, -100%, 0);
        }
    }
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容