前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue文字滚动插件marquee-components优化

Vue文字滚动插件marquee-components优化

作者头像
tianyawhl
发布2021-12-06 14:29:26
3.7K0
发布2021-12-06 14:29:26
举报
文章被收录于专栏:前端之攻略

实现单行文字水平滚动,在网上看了一个vue插件 marquee-components

安装 npm i marquee-components

使用 在main.js中引入

代码语言:javascript
复制
import marquee from 'marquee-components'
Vue.use(marquee );

在页面中使用

代码语言:javascript
复制
<template>
  <div id="app">
       <marquee :val="msg"></marquee>
  </div>
</template>
<script>
export default {
  name: 'app',
  data () {
    return {
      msg: 'vuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevuevue'
    }
  }
}
</script>

如果直接引入组件

代码语言:javascript
复制
<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 {
    width: 100%;
    overflow: hidden;
    position: relative;
  }
  .marquee{
    margin-right: 16px;
  }
  p {
    word-break:keep-all;
    white-space: nowrap;
    font-size: 16px;
    font-family: "微软雅黑 Light";
  }
  .scroll {
    display: flex;
  }
  .getWidth {
    word-break:keep-all;
    white-space:nowrap;
    position: absolute;
    opacity: 0;
    top: 0;
  }
</style>

其他页面引用

代码语言:javascript
复制
<template>
  <div class="container">
        <marquee :val="title"></marquee>
  </div>
</template>
<script>
import marquee from './marquee'
 name: 'index',
 components: {
    marquee
 },
  data () {
    return {
		title: ''
    }
  },
</script>

但是如果一个页面有几个需要滚动的文字,需要修改代码,主要把原代码的通过选择器获取元素的方式 修改为通过通过ref获取元素

代码语言:javascript
复制
<template>
  <div>
     <div class="marquee-wrap" ref="marqueeWrap">
       <div class="scroll" ref="scroll">
         <p class="marquee" ref="marquee">{{text}}</p>
         <p class="copy" ref="copy" v-if="showCopy">{{text}}</p>
       </div>
       <p class="getWidth" ref="getWidth">{{text}}</p>
     </div>
 
  </div>
</template>
<script>
import { clearTimeout } from 'highcharts';
export default {
  name:"marquee",
  props:['val'],

  data() {
    return {
      timer:null,
      creatTimer:null,
      watchTimer:null,
      text:'',
      showCopy:true
    };
  },
  created(){
    this.creatTimer = setTimeout(() => {
      this.move()
      clearTimeout(this.creatTimer)
    }, 1000);
  },
  mounted(){
    console.log("hi")
    console.log(this.val)
    for(let item of this.val){
      this.text+=' '+item
    }
    console.log(this.text)
  },
  methods: {
    move(){
       this.$nextTick(()=>{
          console.log("move")
          let maxWidth = this.$refs.marqueeWrap.clientWidth
          let width = this.$refs.getWidth.scrollWidth
          let scroll = this.$refs.scroll
          if(width<maxWidth){
            this.showCopy = false
            scroll.style.transform = 'translateX(0px)'
            clearInterval(this.timer)
            return
          }else{
            this.showCopy = true
          }
          // let copy = this.$refs.copy
          console.log(this.text)
          // copy.innerText = this.text
          
          let distance = 0
          clearInterval(this.timer)
          this.timer = setInterval(() => {
            distance-=1
            if(-distance >= width){
              distance = 16
            }
            scroll.style.transform = 'translateX(' + distance+'px)'
          }, 20);
          })
    }  
  },
  watch:{
    val(newValue){
      clearTimeout(this.watchTimer)
      this.watchTimer = setTimeout(() => {
       let text=""
       this.text = ""
       for(let item of newValue){
          text+=' '+item
       }
       this.text = text
       console.log(this.text)
      //  clearInterval(this.timer)
       this.move()
       }, 2000);            
    }
  },
  beforeDestroy(){
    clearInterval(this.timer)
    clearInterval(this.creatTimer)
  }
};
</script>
 
<style scoped>
.marquee-wrap{
  width: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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档