vue 实现无缝向左滚动 鼠标悬停、离开时停止、开始滚动

效果:
在这里插入图片描述

可以根据此代码改为轮播

html代码:

<template>
  <div class="announcement">
    <div class="cont">
      <div
        class="contlist"
        ref="contlist"
        @mouseover="stopScroll"
        @mouseout="startScroll"
      >
        <ul>
          <li v-for="(item, index) in list" :key="index">
            {{ index + 1 }}{{ item.name }} {{ item.time }}
          </li>
        </ul>
        <ul>
          <li v-for="(item, index) in list" :key="index">
            {{ index + 1 }}{{ item.name }} {{ item.time }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

JS代码:

<script>
export default {
  name: "",
  data() {
    return {
      list: [
        { name: "全运会女子三人篮球成年组资格赛本周开赛", time: "2019-12-18 16:20:34" },
        { name: "河南建业更名为 “河南嵩山龙门”", time: "2019-12-18 16:20:34" },
        { name: "河南队勇夺全国女篮 联赛三人制比赛 亚军", time: "2019-12-18 16:20:34" },
        { name: "省足协俱乐部杯锦标赛圆满落幕", time: "2019-12-18 16:20:34" },
        { name: "备战全运会建业女足迎来新教练", time: "2019-12-18 16:20:34" },
        { name: "2020年河南省体育传统项目学校国家级体育俱乐部中学生篮球锦标赛开幕式在河南", time: "2019-12-18 16:20:34" },
        { name: "全国男排超级联赛 河南男排获得第八", time: "2019-12-18 16:20:34" },
      ],
      scrollW: 0, //记录滚动到哪了
    };
  },
  created() {},
  methods: {
    //鼠标悬停,停止滚动
    stopScroll() {
      var target = this.$refs.contlist;
      clearInterval(this.scrollTime);
    },
    //鼠标移开 ,接着滚动
    startScroll() {
      var target = this.$refs.contlist;
      this.scrollW = target.offsetLeft; // 移开时记录向左移动的距离
      this.scroll();
    },
    //循环滚动
    scroll() {
      var that = this; //因为定时器里要使用vue对象时不能用this, 在定时器中的 this 代表
      var target = this.$refs.contlist;
      var initLeft = 0;
      if (this.scrollW < 0) {
        initLeft = this.scrollW * -1;
      }
      //定时器
      this.scrollTime = setInterval(function () {
        initLeft++;
        if (initLeft >= target.offsetWidth / 2) {
          initLeft = 0;
        }
        target.style.left = "-" + initLeft + "px"; //获取不到translateX的值改用 left

        //target.style = 'transform: translateX(-'+ initLeft +'px)';
      }, 16);
    },
  },
  mounted() {
    //实例挂载完毕前
    //调用滚动代码
    this.scroll();
  },
};
</script>

css代码:

<style scoped lang="css">
ul,
li,
em {
  margin: 0;
  padding: 0;
}
.cont {
  height: 40px;
  line-height: 40px;
  background-color: #000;
  color: #fff;
  overflow: hidden;
  position: relative;
}
.contlist {
  position: absolute;
  white-space: nowrap;
  display: flex;
  flex-direction: row;
}
.contlist ul {
  display: flex;
  flex-direction: row;
}
.contlist ul li {
  font-size: 12px;
  margin-right: 25px;
  height: 40px;
  line-height: 40px;
  display: flex;
  flex-direction: row;
}
.contlist ul li em {
  color: #f80;
  font-size: 12px;
  padding-right: 10px;
}
</style>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,根据你的要求,我给你提供一个简单的代码示例: ```html <template> <div class="notice-wrapper"> <div class="notice-content" v-for="(notice, index) in notices" :key="index" ref="notice"> <span>{{ notice }}</span> </div> </div> </template> <script> export default { data() { return { notices: ['公告1', '公告2', '公告3'], // 公告列表 intervalId: null, // 定器ID }; }, mounted() { this.startScroll(); // 组件挂载开始滚动 }, methods: { // 开始滚动 startScroll() { this.intervalId = setInterval(() => { const firstNotice = this.$refs.notice[0]; const height = firstNotice.offsetHeight; this.$refs.notice[0].style.marginTop = `-${height}px`; this.notices.push(this.notices.shift()); }, 2000); }, // 停止滚动 stopScroll() { clearInterval(this.intervalId); }, }, // 监听鼠标进入和离开事件 created() { this.$parent.$on('mouse-enter', this.stopScroll); this.$parent.$on('mouse-leave', this.startScroll); }, // 解绑事件 beforeDestroy() { this.$parent.$off('mouse-enter', this.stopScroll); this.$parent.$off('mouse-leave', this.startScroll); }, }; </script> <style> .notice-wrapper { height: 50px; overflow: hidden; } .notice-content { line-height: 50px; height: 50px; transition: margin-top 0.5s linear; } </style> ``` 在这个示例中,我们使用v-for指令遍历公告列表,使用CSS实现滚动效果。在组件挂载,调用startScroll方法开始滚动,使用setInterval实现滚动。在鼠标进入和离开事件中,分别调用stopScroll和startScroll方法,实现鼠标移出停止滚动的效果。注意,在监听事件和解绑事件,我们使用了$parent来访问父组件,因为鼠标事件是在父组件中触发的。 希望这个示例可以帮到你,如果还有什么不明白的地方,可以继续问我哦~

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李泽举

如对你有帮助,那我就没白写

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值