1. 程式人生 > >Vue 消息無縫滾動

Vue 消息無縫滾動

過渡動畫 sel tno ted ear code EDA 數組 css3

技術分享圖片
vue實現消息向上無縫滾動效果

<ul class="new-list" :class="{anim:animate}" @mouseenter="Stop()" @mouseleave="Up()">
  <li v-for="item in noticeList">
    ...
  </li>
</ul>
<script>
  export default {
    data() {
      return {
        noticeList: [],
        animate:false,
        intNum: undefined,
      }
    },
    created: function () {
      this.getNoticeData();
    },
    methods: {
      getNoticeData() {
        this.$http.get(‘/news/allList‘, {
          params: {
            ‘pageNumber‘: 10,
            ‘currentPage‘: 1
          }
        }).then(res => {
          this.noticeList = res.data.items;
          this.ScrollUp();
        });
      },
      ScrollUp() {
        this.intNum = setInterval(() => {
          this.animate=true;// 向上滾動的時候需要添加css3過渡動畫
          setTimeout(()=>{
            this.noticeList.push(this.noticeList[0]);// 將數組的第一個元素添加到數組的
            this.noticeList.shift(); //刪除數組的第一個元素
            this.animate=false;
          },500)
        }, 10000);
      },
      //鼠標移上去停止
      Stop() {
        clearInterval(this.intNum);
      },
      Up() {
        this.ScrollUp();
      },
    }
  }
</script>

樣式

.new-list{
    line-height: 28px;
    transition: top 0.5s;
}
.anim{
    transition: all 0.5s;
    margin-top: -28px;//高度等於行高
}

Vue 消息無縫滾動