• vue自动滚动组件 可以支持鼠标滚轮操作


    vue自动滚动组件 可以支持鼠标滚轮操作

    
    <template>
      <div ref="scrollMain" class="auto-scroll-list-main" @click="autoScrollClick($event)"
           :key="keyValue" @mouseover="mEnter" @mouseleave="mLeave"
      >
          <div ref="scrollItemBox" class="seamless-warp-box" >
            <slot/>
          div>
          <div v-html="copyHtml" class="seamless-warp-box">div>
      div>
    template>
    
    <script>
    
    import LOG from '@/utils/self-log'
    
    export default {
      name: 'AutoScrollList',
      props: {
        listData: {
          type: Array
        },
        isPause: {
          type: Boolean,
          default: false
        },
        keyValue: { type: String, default: '' }
      },
      mounted() {
        // 如果列表数据是异步获取的,记得初始化在获取数据后再调用
        this.initScroll()
      },
      data() {
        return {
          scrollTop: 0, //列表滚动高度
          speed: 70, //滚动的速度
          copyHtml: null,
          scrollInterval: null,
        }
      },
      watch: {
        isPause(newValue, _) {
          LOG.info('isPause='+ newValue)
          if (newValue) {
            this.mEnter()
          } else {
            this.mLeave()
          }
        },
        listData(newValue, _) {
          this.mEnter()
          this.initScroll()
        },
      },
      methods: {
        initScroll() {
          this.$nextTick(() => {
            this.copyHtml = this.$refs.scrollItemBox.innerHTML
            this.startScroll()
          })
        },
        // 鼠标移入停止滚动
        mEnter() {
          if (this.scrollInterval) {
            clearInterval(this.scrollInterval)
            this.scrollInterval=null
          }
        },
        // 鼠标移出继续滚动
        mLeave() {
          this.startScroll()
        },
        // 开始滚动
        startScroll() {
          //在当前位置开始滚动
          this.scrollTop=this.$refs.scrollMain.scrollTop
          this.scrollInterval = setInterval(this.scroll, this.speed)
        },
        // 滚动处理方法
        scroll() {
          this.scrollTop=this.scrollTop+1
          this.$refs.scrollMain.scrollTop=this.scrollTop
          // 获取需要滚动的盒子的高度
          let scrollItemBox = this.$refs.scrollMain.scrollHeight/2
          // 当判断滚动的高度大于等于盒子高度时,从头开始滚动
          if (this.scrollTop >= scrollItemBox) {
            this.scrollTop = 0
          }
        },
        autoScrollClick(e) {
          let index = e.target.dataset.index
          if (index === undefined) {
            return
          }
          this.$emit('autoScrollClick', index)
        }
      },
      destroyed(){
        this.mEnter()
      }
    }
    script>
    
    <style lang="scss" scoped>
    .auto-scroll-list-main {
      overflow: auto;
      height: 100%;
    }
    
    .auto-scroll-list-main::-webkit-scrollbar {
      display: none
    }
    
    .seamless-warp-box {
      width: 100%;
    }
    style>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
  • 相关阅读:
    2.4 图解CIO工作指南(IT 架构模型) --- 构成IT 架构的技术要素
    HackTheBox Support 逆向工程获取LDAP凭证,票证伪造提权
    LVS负载均衡群集——LVS-NAT模式搭建和LVS-DR模式搭建
    java计算机毕业设计基于springboo+vue的学生毕业离校系统
    MySQL——函数和流程控制
    面向对象之旅:核心理念、设计方法与UML详解(软件设计师笔记)
    数据库和缓存如何保证一致性?
    601-体育馆的人流量
    文件路径中的/,\的区别和文件路径的常见用法
    一键分割视频并生成M3U8格式:高效管理视频内容,畅享流畅播放新体验
  • 原文地址:https://blog.csdn.net/TY_GYY/article/details/126101856