vue实现公告/消息无缝滚动效果

首先html部分

 <div class="top-div-top">系统公告</div>
          <div class="top-div-middle" id="wrapper">
            <ul id="con1" ref="con1">
              <li v-for="(item, index) in items" :key="index" @click="viewDetails(item)">
                <h4 style="display: flex; color: #000; margin: 0">
                  <div class="con1-div-top">○ {{ item.title }}</div>
                  <div style="flex: 3; text-align: right">{{ item.date }}</div>
                </h4>
                <div class="con1-div-bottom">&emsp;&emsp;{{ item.center }}</div>
              </li>
            </ul>

            <ul id="con2" v-show="isRollShow">
              <li v-for="(item, index) in items" :key="index" @click="viewDetails(item)">
                <h4 style="display: flex; color: #000; margin: 0">
                  <div class="con1-div-top">○ {{ item.title }}</div>
                  <div style="flex: 3; text-align: right">{{ item.date }}</div>
                </h4>
                <div class="con1-div-bottom">&emsp;&emsp;{{ item.center }}</div>
              </li>
            </ul>
          </div>

然后js部分

  mounted() {
    this.scroll()
  },


  methods: {

    isRollShow: false,//如果内容没有超出第二份设置隐藏

    scroll() {
      const speed = 100
      const wrapper = document.getElementById('wrapper')
      const ListOne = document.getElementById('con1')
      const Listtwo = document.getElementById('con2')
      console.log(con1.offsetHeight >= wrapper.offsetHeight)
      // 内容超出才执行滚动
      if (con1.offsetHeight >= wrapper.offsetHeight) {
        this.isRollShow = true  //显示内容
        function Marquee() {
          if (Listtwo.offsetHeight - wrapper.scrollTop <= 0) {
            wrapper.scrollTop -= ListOne.offsetHeight
          } else {
            wrapper.scrollTop += 1
          }
        }
        let MyMar = setInterval(Marquee, speed)
        wrapper.onmouseover = function () {
          clearInterval(MyMar)
        }
        wrapper.onmouseout = function () {
          MyMar = setInterval(Marquee, speed)
        }
      }
    },

}

有试过用 cloneNode(true) 来复制节点,但是事件没有复制过去,innerHTML也是一样事件复制不过去,就想到了渲染两份数据可以实现无缝滚动并且可以触发事件


如果有更好的办法可以指教

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值