zoukankan      html  css  js  c++  java
  • 文字超出滚动效果封装为组件

    组件在页面中使用

    引用
    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>
    
  • 相关阅读:
    2019年1月18日23:20:02 夜盘
    2019年1月16日22:50:28 白糖SR1905
    2018/12/20 20:52:42 螺纹钢PTA豆粕
    2018/12/19 20:55:58 螺纹钢豆粕PTA
    2018-12-18 豆粕
    2018-12-18 19:53 螺纹钢
    记录一下自己的跑步数据
    Xamarin.Lottie---UWP运行出错时的注意事项
    Hitting refresh on my career(译)----重新定义我的事业
    ChatKit for Xamarin.Android 绑定
  • 原文地址:https://www.cnblogs.com/enhengenhengNymph/p/15475939.html
Copyright © 2011-2022 走看看