• Vue3——区域内无限滚动


    1. 新建组件RegionRoll.vue

    <template>
      <div :style="noticeStyle" ref="noticeDivRef">
        <span ref="noticeRef" class="notice-span" :style="noticeSpanStyle">{{ props.text }}</span>
      </div>
    </template>
    <script setup lang="ts">
    import { computed, defineProps, onMounted, ref, nextTick, watch } from 'vue'
    
    const props = defineProps({
      /* 区域宽度(默认200px) */
      w: {
        type: String,
        default: '200px'
      },
      /* 区域高度(默认20px) */
      h: {
        type: String,
        default: '20px'
      },
      /* 区域文本 */
      text: {
        type: String
      },
      /* 过渡速度(默认30,值越大速度越快) */
      speed: {
        type: Number,
        default: 30
      },
      /* 是否默认滚动(默认否) */
      isDefaultRoll: {
        type: Boolean,
        default: false
      }
    })
    const noticeSpanStyle = ref({})
    const noticeRef = ref<HTMLElement>()
    const noticeDivRef = ref<HTMLElement>()
    
    const noticeStyle = computed<{}>(() => {
      return {
        width: props.w,
        'white-space': 'nowrap',
        overflow: 'hidden',
        position: 'relative',
        height: props.h
      }
    })
    
    onMounted(() => {
      noticeSpanStyle.value = {
        left: `100%`,
        transition: '0s'
      }
    })
    
    const setAni = (width) => {
      const delay = width / props.speed
      noticeSpanStyle.value = {
        left: `-${width}px`,
        transition: `${delay}s linear`
      }
      setTimeout(() => {
        noticeSpanStyle.value = {
          left: `100%`,
          transition: `0s linear`
        }
        nextTick(() => {
          setAni(noticeRef.value?.offsetWidth)
        })
      }, delay * 1000)
    }
    
    watch(
      () => props.text,
      () => {
        nextTick(() => {
          let spanW = noticeRef.value?.offsetWidth || 0
          let divW = noticeDivRef.value?.offsetWidth || 0
          if (spanW <= divW && !props.isDefaultRoll) {
            noticeSpanStyle.value = {
              left: `0px`,
              transition: '0s'
            }
            return
          }
          setAni(spanW)
        })
      }
    )
    </script>
    
    <style scoped>
    .notice-span {
      position: absolute;
    }
    </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

    2. 使用

    <script setup lang="ts">
    import { ref } from 'vue'
    import RegionRoll from '@/components/RegionRoll.vue'
    const noticeText = ref('我是文本-我是文本-我是文本-我是文本-我是文本-我是文本-我是文本')
    </script>
    
    <template>
      <RegionRoll w="100%" :text="noticeText" />
    </template>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    Git上传项目到Github
    SpringBoot SpringBoot 原理篇 3 核心原理 3.2 启动流程【1】
    飞桨平台搭建PP-YOLOE模型
    MyBatis-plus:删除操作、逻辑删除、性能分析插件(狂)
    令人疑惑的Promise相关问题
    算法排序6——快速排序(分治思想)
    小程序直接生成鸿蒙App的方法
    Python画小仓鼠
    真的很难理解?RecyclerView 缓存机制到底是几级缓存?
    一次学习引发我对于 synchronized 的再理解
  • 原文地址:https://blog.csdn.net/qq812457115/article/details/127612867