javascript - How to make the news list scroll up and down in vue
淡淡烟草味
淡淡烟草味 2017-05-19 10:43:20
0
3
834

Loop playback

This is the data part

data() {
    return {
      prizeList: [
        { name: 0 },
        { name: 1 },
        { name: 2 },
        { name: 3 },
        { name: 4 }
      ]
    }
  }

This is my current code, it can only switch in cycles and has no animation effect

setInterval(async () => {
    let first = this.prizeList.splice(0, 1)
    this.prizeList.push(...first)
}
淡淡烟草味
淡淡烟草味

reply all(3)
世界只因有你

You can change your mind and use transition to achieve it

<p class="scroll-wrap">
    <ul class="scroll-content" :style="{ top }">
        <li v-for="item in prizeList">{{item.name}}</li >  
    </ul>
</p>
export default {
  data () {
    return {
      prizeList: [
        { name: 0 },
        { name: 1 },
        { name: 2 },
        { name: 3 },
        { name: 4 }
      ],
      activeIndex: 0
    }
  },

  computed: {
    top() {
      return - this.activeIndex * 50 + 'px';
    }
  },

  mounted() {
    setInterval(_ => {
      if(this.activeIndex < this.prizeList.length) {
        this.activeIndex += 1;
      } else {
        this.activeIndex = 0;
      }
    }, 1000);
  }
};
.scroll-wrap{
  width: 200px;
  height: 50px;
  border: 1px solid blue;
  overflow: hidden;
}

.scroll-content{
  position: relative;
  transition: top 0.5s;

  li{
    line-height: 50px;
    text-align: center;
  }
}

Effect

巴扎黑

Can be implemented using the transition component

刘奇
  • vue official documentation

  • Animation effects library

Very easy to use:

import 'animate'

<transition
  name="custom-classes-transition"
  enter-active-class="animated bounceIn" //animate 提供的动画效果
  leave-active-class="animated bounceOutRight"
>
... //要使用动画效果的内容
</transition>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!