• 文本超出滚动显示


    ref版本,可以在页面中多次使用

    <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>
    

    id版本,一个页面中只能使用一次

    <template>
     <div class="marquee-wrap">
         <!-- 文本超出滚动显示 -->
      <div class="scroll">
       <p class="marquee">{{text}}</p>
       <p class="copy"></p>
      </div>
      <p 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 = document.querySelector('.marquee-wrap').clientWidth
       let width = document.querySelector('.getWidth').scrollWidth
       if (width <= maxWidth) return
       let scroll = document.querySelector('.scroll')
       let copy = document.querySelector('.copy')
       copy.innerText = this.text
       let distance = 0 
       this.timer = setInterval(() => {
        distance -= 1
        if (-distance >= width) {
         distance = 16
        }
        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>
    
  • 相关阅读:
    10.01 简单的51代码
    1010Linux的文件操作函数以及所需头文件
    10.05 最初对Linux的了解,对Shell的认识
    1006 Linux的基本命令以及一些简单的通配符说明
    10.03 简单的51单片机程序
    1011Linux用户管理规则及用户管理函数
    vim命令以及gcc编译器的常用cmd
    10.02 一个简单的串口的初始化程序
    做销售的100个绝招
    一个女程序员的创业人生:胆识也是一种能力
  • 原文地址:https://www.cnblogs.com/enhengenhengNymph/p/15409442.html
Copyright © 2020-2023  润新知