vue 通知 走馬燈效果


封裝一個子組件:

<template>
  <div class="container">
      <div class="wrap"> 
    <div id="box">
      <div id="marquee">{{text}}</div> 
      <div id="copy"></div> 
    </div>
    <div id="node">{{text}}</div>
  </div>
  </div>
</template>
<script>
export default {
  name: 'Marquee',
  props: ['text'],  // 字符串格式
  data () {
    return {
    }
  },
  methods: {
    move () {
// 獲取文字text 的計算后寬度  (由於overflow的存在,直接獲取不到,需要獨立的node計算)
      let width = document.getElementById('node').getBoundingClientRect().width 
      let box = document.getElementById('box')
      let copy = document.getElementById('copy')
      copy.innerText = this.text // 文字副本填充
      let distance = 0 // 位移距離
//設置位移
      setInterval(function () { 
        distance = distance - 1
 // 如果位移超過文字寬度,則回到起點
        if (-distance >= width) {
          distance = 16
        }
        box.style.transform = 'translateX(' + distance + 'px)'
      }, 40) 
    }
  },
// 把父組件傳入的arr轉化成字符串
  mounted () {
  },
// 更新的時候運動
  updated: function () {
    this.move()
  }
}
</script>
<style lang="less" scoped>
  @import '../assets/less/common.less';
// 限制外框寬度,隱藏多余的部分
.container{
    font-size: 26/@size;
    background: url(../assets/img/purchasedMembers/ic_laba.png) white no-repeat 20/@size 50%;   //通知的一個icon圖標
    background-size: 32/@size 28/@size;
    padding-left:64/@size;
    margin-top: 0.266rem;
    /*border-bottom: 1px solid #F1F1F1;*/
    height: 1rem;
}
.wrap {
   overflow: hidden;
    height: 1rem;
}
// 移動框寬度設置
#box {
  width: 8000%;
}
// 文字一行顯示
#box div {
  float: left;
}
// 設置前后間隔
#marquee {
   margin-right:16/@size;
   line-height: 1rem;
}
// 獲取寬度的節點,隱藏掉
#node {
  position: absolute;
  z-index: -999;
  top: -999999px;
}
</style>

 

父組件引用並傳參:

      

<news :text="text"></news>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM