• 文字超出滚动效果封装为组件


    组件在页面中使用

    引用
    import marquee from "@/components/marquee/index";
    注册
      components:{
        marquee
      },
    使用
    <marquee :val="TI_info"></marquee>
    

    组件代码

    <template>
     <div ref="marqueeWrap" class="marquee-wrap">
         <!-- 文本超出滚动显示 -->
      <div ref="scroll" class="scroll">
       <p class="marquee">{{text}}</p>
       <p ref="copy" class="copy"></p>
      </div>
      <p ref="getWidth" class="getWidth">{{text}}</p>
     </div>
    </template>
    
    <script>
    export default {
     name: 'marquee',
     props: ['val'],
     data () {
      return {
       timer: null,
       text: ''
      }
     },
     created () {
      let timer = setTimeout(() => {
       this.move()
       clearTimeout(timer)
      }, 1000)
     },
     mounted () {
      for (let item of this.val) {
       this.text += ' ' + item
      }
     },
     methods: {
      move () {
       let maxWidth = this.$refs.marqueeWrap.clientWidth
       
       let width = this.$refs.getWidth.scrollWidth
       if (width <= maxWidth) return
       this.$refs.copy.innerText = this.text
       let distance = 0 
       this.timer = setInterval(() => {
        distance -= 1
        if (-distance >= width) {
         distance = 16
        }
        this.$refs.scroll.style.transform = 'translateX(' + distance + 'px)'
       }, 20)
      }
     },
     beforeDestroy () {
      clearInterval(this.timer)
     }
    }
    </script>
    
    <style scoped>
     .marquee-wrap {
       100%;
      overflow: hidden;
      position: relative;
     }
     .marquee{
      margin-right: 16px;
     }
     p {
      word-break:keep-all;
      white-space: nowrap;
     }
     .scroll {
      display: flex;
     }
     .getWidth {
      word-break:keep-all;
      white-space:nowrap;
      position: absolute;
      opacity: 0;
      top: 0;
     }
    </style>
    
  • 相关阅读:
    日期时间類(DateTime)的应用
    C# 排版快捷鑑
    撷取指定网址中的资料Part1:WebClinet 的用法
    Chart in Web
    Android APK反编译得到Java源代码和资源文件
    iOS 6.0 GM 版全系列固件下载
    IOS判断设备是否已越狱(isJailbroken)
    批量离线下载迅雷快传资源
    Android如何防止apk程序被反编译
    Java接口学习
  • 原文地址:https://www.cnblogs.com/enhengenhengNymph/p/15475939.html
Copyright © 2020-2023  润新知