vue 跑马灯顶部显示轮播效果

vue 跑马灯顶部显示轮播效果

gif动画展示

在这里插入图片描述

在线预览

在线预览demo

实现原理

使用定时器setInterval对元素的left进行间隔操作,逐渐减小,实现从右到左的移动。
mouseover时对定时器停止,mouseout时重新启动定时器。
由于移动元素需要加点击事件,所以使用了v-html指令,如不需要,请换成v-text

demo代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>跑马灯效果</title>
    <script src="https://cdn.bootcss.com/vue/2.5.15/vue.min.js"></script>
    <style>
        body,
        p {
            margin: 0;
        }

        .marBox {
            background-color: antiquewhite;
            width: 100%;
            height: 30px;
            line-height: 30px;
        }

        .marBox__child {
            width: 100%;
            height: 30px;
            line-height: 30px;
            position: absolute;
            top: 0;
        }
    </style>
</head>

<body>
    <!--容器-->
    <div id="app">
        <div @mouseover="stop" @mouseout="start" class="marBox">
            <p class="marBox__child" :style="styles" v-html="msgStr"></p>
        </div>
    </div>
    <script>
        // 创建一个vue实例
        var vm = new Vue({
            el: '#app', // 绑定id为appid容器
            data: {
                // 通知信息
                msg: [
                    {
                        txt:"获取浏览器的宽度",
                        link:"https://haley1688.gitee.io/toolshaley/pages/checkBrowser.html"
                    },
                    {
                        txt:"Haley的博客",
                        link:"https://blog.csdn.net/weixin_42708208"
                    },
                    {
                        txt:"百度",
                        link:"http://www.baidu.com"
                    },
                    {
                        txt:"颜色模式转换",
                        link:"https://haley1688.gitee.io/toolshaley/pages/colorTransformation.html"
                    },
                ],
                timer: null,
                speed: 100, //速度,数字越大,速度越慢
                left: 2000, //默认起始位置
                step: 5, //每步移动的距离
            },
            mounted() {
                this.start();
            },
            methods: {
                // 启动
                start() {
                    this.timer = setInterval(() => { //定时任务
                        // console.log(this.left, -this.msgStr.length * 10)
                        if (this.left < -this.msgStr.length * 10) {
                            this.left = this.screenWidth
                        } else {
                            this.left -= this.step
                        }
                        // console.log(this.left)
                    }, this.speed);
                },
                // 停止
                stop() {
                    clearInterval(this.timer);
                }

            },
            computed: {
                // 获取屏幕宽度
                screenWidth() {
                    return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
                },
                // 将通知数组拼接为字符串,
                msgStr() {
                    let arr=this.msg.map(item=>{
                        return `<a href="${item.link}">${item.txt}</a>`
                    })
                    return "<b>通告:</b>" + arr.join(Array(30).join('&nbsp;'))
                },
                // 移动left值
                styles() {
                    return {
                        'left': this.left + 'px'
                    }
                }
            },
            beforeDestroy() {
                this.stop()
            }
        })
    </script>
</body>

</html>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值