首頁  >  文章  >  web前端  >  在vue中如何實現訊息的無縫滾動效果

在vue中如何實現訊息的無縫滾動效果

亚连
亚连原創
2018-06-22 17:10:293107瀏覽

本篇文章主要介紹了vue實現訊息的無縫滾動效果的範例程式碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

朋友的專案裡要實現一個訊息無縫滾動的效果,中途遇到了一點小bug,每組訊息滾動完再次循環時會出現停留兩倍的時間間隔問題,我研究了一天終於解決了這個1S的小問題

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

第一步在範本中使用v-for方法循環出訊息清單

<template>

<p id="box">
  <ul id="con1" ref="con1" :class="{anim:animate==true}">
    <li v-for=&#39;item in items&#39;>{{item.name}}</li>
  </ul>
</p>
</template>

第二步在3f1c4e4b6b16bbbd69b2ee476dc4f83a標籤中放置訊息數組和具體的method 方法。

<script>

 export default {
data() {
 return {
   animate:false,
   items:[  //消息列表对应的数组
     {name:"马云"},
     {name:"雷军"},
     {name:"王勤"}
   ]
 }
},
created(){
  setInterval(this.scroll,1000) // 在钩子函数中调用我在method 里面写的scroll()方法,注意此处不要忘记加this,我在这个位置掉了好几次坑,都是因为忘记写this。
},
methods: {
  scroll(){
    let con1 = this.$refs.con1;
    con1.style.marginTop=&#39;-30px&#39;;
    this.animate=!this.animate;
    var that = this; // 在异步函数中会出现this的偏移问题,此处一定要先保存好this的指向
    setTimeout(function(){
        that.items.push(that.items[0]);
        that.items.shift();
        con1.style.marginTop=&#39;0px&#39;;
        that.animate=!that.animate;  // 这个地方如果不把animate 取反会出现消息回滚的现象,此时把ul 元素的过渡属性取消掉就可以完美实现无缝滚动的效果了
    },500)
  }
}
}
</script>

<style>

*{
  margin: 0 ;
  padding: 0;
}
#box{
  width: 300px;
  height: 32px;
  line-height: 30px;
  overflow: hidden;
  padding-left: 30px;
  border: 1px solid black;
  transition: all 0.5s;
}
.anim{
  transition: all 0.5s;
}
#con1 li{
  list-style: none;
  line-height: 30px;
  height: 30px;
}
</style>

上面是我整理給大家的,希望未來會對大家有幫助。

相關文章:

如何把Angular專案部署到nginx上

使用ES6如何實作單例模式

使用node.js如何建立子程序(詳細教學)

#如何使用slider設定資料值

###########################################################################################################################

以上是在vue中如何實現訊息的無縫滾動效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn