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

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

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

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

<script>

 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>

樣式設置

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



html

<template>
 
<div id="box">
  <ul id="con1" ref="con1" :class="{anim:animate==true}">
    <li v-for='item in items'>{{item.name}}</li>
  </ul>
</div>
</template>

以上就是這篇文章的全部內容,希望對大家有幫助,也請多多指教,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章