vue實現消息的無縫滾動效果


export default {
data() {
  return {
      animate:false,
      items:[
          {name:"馬雲"},
          {name:"雷軍"},
          {name:"王勤"}
      ]
  }
},
created(){
    setInterval(this.scroll,1000)
},
methods: {
    scroll(){
       this.animate=true;    // 因為在消息向上滾動的時候需要添加css3過渡動畫,所以這里需要設置true
       setTimeout(()=>{      //  這里直接使用了es6的箭頭函數,省去了處理this指向偏移問題,代碼也比之前簡化了很多
               this.items.push(this.items[0]);  // 將數組的第一個元素添加到數組的
               this.items.shift();               //刪除數組的第一個元素
               this.animate=false;  // margin-top 為0 的時候取消過渡動畫,實現無縫滾動
       },500)
    }
}
#box{
    width: 300px;
    height: 32px;
    overflow: hidden;
    padding-left: 30px;
    border: 1px solid black;
}
.anim{
    transition: all 0.5s;
    margin-top: -30px;
}
#con1 li{
    list-style: none;
    line-height: 30px;
    height: 30px;
}
<div id="box">
            <ul id="con1" ref="con1" :class="{anim:animate==true}">
                <li v-for='item in items'>{{item.name}}</li>
            </ul>
        </div>

 


免責聲明!

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



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