vue實現消息的無縫滾動效果(完善版)

在昨天發佈完文章以後又整理一下,發現有幾處須要改進的地方,今天就及時更新一下,也算是激勵本身要保持這種積極的好習慣css

項目環境vue-cli ,請自行配置好相應的,環境及路由,這裏主要介紹實現的方法vue

第一步在模板中 使用v-for方法循環出消息列表css3

<script>es6

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

}
</script>vue-cli

樣式設置數組

<style>函數

*{
    margin: 0 ;
    padding: 0;
}
#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;
}

</style>動畫

以上就是這篇文章的所有內容,但願對你們有幫助,也請多多指教,謝謝!this

相關文章
相關標籤/搜索