欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)跑馬燈樣式文字橫向滾動

 更新時間:2021年11月23日 17:29:59   作者:Best_北詩  
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)跑馬燈樣式文字橫向滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Vue實現(xiàn)跑馬燈樣式文字橫向滾動的具體代碼,供大家參考,具體內(nèi)容如下

需求:

在Vue項目的頂部,來實現(xiàn)文字左右滾動

步驟:

1、可以自己封裝一個組件,也可以自己寫,也可以復(fù)制以下代碼
2、封裝完成以后要在所需的組件中引入,注冊,并使用

代碼:

封裝一個專門用來實現(xiàn)跑馬燈效果的組件marquee組件

<template>
<!-- 跑馬燈組件 -->
  <div class="marquee-wrap" ref="marquee-wrap">
    <div class="scroll" ref="scroll">
      <p class="marquee">{{text}}</p>
      <p class="copy" ref="copy"></p>
    </div>
    <p class="getWidth" ref="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['marquee-wrap'].clientWidth
    let width = this.$refs['getWidth'].scrollWidth
      if (width <= maxWidth) return
    let scroll = this.$refs['scroll']
    let copy = this.$refs['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: 0.16rem;
  }
  p {
    word-break:keep-all;
    white-space: nowrap;
    font-size: 0.28rem;
  }
  .scroll {
    display: flex;
  }
  .getWidth {
    word-break:keep-all;
    white-space:nowrap;
    position: absolute;
    opacity: 0;
    top: 0;
  }
</style>

在哪個組件中使用,就引入

// 引入跑馬燈組件
import  marquee  from "@/components/marquee/marquee.vue";

引用并注冊

export default {
  components: {
  // 注冊跑馬燈組件
    marquee,
  },
 }

注冊完成以后接下來就是Html樣式了,在template模板中使用這個組件

<Marquee class="realData">
          <ul class="fa-scroll-cont">
            <li v-for="item in realData" :key="item.name">
              <span class="roll-text">{{ item.city }}</span>
            </li>
          </ul>
</Marquee>

接下來就是效果圖:

為了效果看的更明顯多截了幾張

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論