vue消息滚动 鼠标移入停止滚动,鼠标移开继续滚动

vue无缝滚动效果,直接上代码so easy

 <div id="box">
            <ul id="con1" ref="con1" :class="{anim:animate==true}"  @mouseenter="Stop()" @mouseleave="Up()">
                <li v-for='item in items'>{{item.name}}</li>
            </ul>
        </div>
data() {
    return {
      animate: false,
      items: [{ name: "马云" }, { name: "雷军" }, { name: "王勤" }],
      timer:'',
    };
  },
  created() {
    this.timer = setInterval(this.scroll, 1000);
  },
  methods: {
    scroll() {
      this.animate = true; // 因为在消息向上滚动的时候需要添加css3过渡动画,所以这里需要设置true
      setTimeout(() => {
        //  这里直接使用了es6的箭头函数,省去了处理this指向偏移问题,代码也比之前简化了很多
        this.items.push(this.items[0]); // 将数组的第一个元素添加到数组的
        this.items.shift(); //删除数组的第一个元素
        this.animate = false; // margin-top 为0 的时候取消过渡动画,实现无缝滚动
      }, 500);
    },
    //鼠标移上去停止
    Stop() {
      clearInterval(this.timer);
    },
    Up() {
      this.timer = setInterval(this.scroll, 1000);
    },
  },

<style scoped>
#box {
  width: 300px;
  height: 60px;
  overflow: hidden;
  padding-left: 30px;
  border: 1px solid black;
}
ul{
  margin: 0;
}
.anim {
  transition: all 0.5s ease;
  margin-top: -30px;
}
#con1 li {
  list-style: none;
  line-height: 30px;
  height: 30px;
}
</style>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值