vue实现 横向跑马灯组件封装

<template>
    <div class="marquee" :style="{  height: height + 'px' }">
        <span class="marquee__title" ref="marqueeTitle" :style="{ color, fontSize: fontSize + 'px', animationDuration: speed + 's', '--speed': speed }" v-html="title"></span>
    </div>
</template>

<script>
/**
 * NoticeBar 公告栏跑马灯
 * @description 从右往左的跑马灯
 * @property {String} content 显示的内容,支持html
 * @property {String} color 文字颜色,默认#EFF0DB
 * @property {Number} fontSize 文字大小,默认40
 * @property {Number} speed 滚动速度,单位s(播放一次所用时间,默认根据内容宽度计算时间)
 * @property {String} bgColor 背景颜色,默认#CC2529
 * @property {Number} height 背景高度,默认78
 */
export default {
    name: 'NoticeBar',
    props: {
        content: {
            type: String,
            default: ''
        },
        color: {
            type: String,
            default: '#ffff'
        },
        fontSize: {
            type: Number,
            default: 16
        },
        // speed: {
        //     type: Number,
        //     default: 0
        // },
        // bgColor: {
        //     type: String,
        //     default: '#CC2529'
        // },
        height: {
            type: Number,
            default: 20
        }
    },
    data() {
        return {
            DEFAULT_SPEED: 30, // 默认速度,每秒跑的距离,单位:px/s
            title: '',
            speed:0
        };
    },
    watch: {
        content(value) {
            this.title = value;
            this.calcSpeed();
        }
    },
    created() {
        if (this.content) {
            this.title = this.content;
            this.calcSpeed();
        }
    },
    methods: {
        calcSpeed() {
            if (this.title !== '' && this.speed === 0) {
                this.$nextTick(() => {
                    let width = this.$refs.marqueeTitle.clientWidth;
                    this.speed = Number(width / this.DEFAULT_SPEED).toFixed(2);
                });
            }
        }
    }
};
</script>

<style scoped="scoped">
.marquee {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    word-break: break-all;
    white-space: nowrap;
    overflow: hidden;
}
.marquee__title {
    letter-spacing: 3px;
    cursor: default;
    display: inline-block;
    padding-left: 100%;
    animation: marqueeMove calc(var(--speed) * 1s) linear infinite;
}
.marquee:hover .marquee__title {
    animation-play-state: paused;
}
@keyframes marqueeMove {
    0% {
        transform: translateX(0);
    }

    100% {
        transform: translateX(-100%);
    }
}
</style>

个人记录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值