vue 公告通知text左右滑动

效果图

<template>
    <div class="maq-div">
        <img class="icon-alarm" src="../assets/image/horn.png" />
        <div class="marquee-wrap" ref="marquee-wrap">
            <div class="scroll" ref="scroll">
                <p class="marquee">{{ text }}</p>
                <p class="copy" ref="copy"></p>
            </div>
            <p class="getWidth" ref="getWidth">{{ text }}</p>
        </div>
    </div>
</template>

<script>
export default {
    name: 'marquee',
    props: ['val'],
    data() {
        return {
            timer: null,
            text: '',
        }
    },
    created() {
        let timer = setTimeout(() => {
            this.move()
            clearTimeout(timer)
        }, 1000)
    },
    mounted() {
        for (let item of this.val) {
            this.text += item
        }
    },
    methods: {
        move() {
            let maxWidth = this.$refs['marquee-wrap'].clientWidth
            let width = this.$refs['getWidth'].scrollWidth
            if (width <= maxWidth) return
            let scroll = this.$refs['scroll']
            let copy = this.$refs['copy']
            copy.innerText = this.text
            let distance = 0
            this.timer = setInterval(() => {
                distance -= 1
                if (-distance >= width) {
                    distance = 16
                }
                scroll.style.transform = 'translateX(' + distance + 'px)'
            }, 20)
        },
    },
    beforeDestroy() {
        clearInterval(this.timer)
        this.timer = null
    },
}
</script>

<style scoped>
.maq-div{
    width: 100%;
    height: 74px;
    line-height: 74px;
    background: #171717;
    border-radius: 13px;
    padding-left: 10px;
}
.icon-alarm {
    margin: 17px 20px 0 10px;
    width: 40px;
    height: 40px;
    float: left;
}
.marquee-wrap {
    float: left;
    width: calc(100% - 80px);
    height: 74px;
    line-height: 74px;
    overflow: hidden;
    position: relative;
    
}

.marquee {
    margin-right: 0.16rem;
}
p {
    word-break: keep-all;
    white-space: nowrap;
    font-size: 0.28rem;
    color: #fff;
}
.scroll {
    display: flex;
}
.getWidth {
    word-break: keep-all;
    white-space: nowrap;
    position: absolute;
    opacity: 0;
    top: 0;
}
</style>

调用

<marquee :val="list" />
list: ["欢迎大家来一起学习","在一起学习","一起学习","学无止境"],
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值