1. 程式人生 > >vue實現文字上下滾動

vue實現文字上下滾動

動畫 con vertica pos mar int str borde size

實現文字的上下滾動使用positon的relative的top屬性,通過動態改變top來實現相關內容的更換,通過transion來實現相關的動畫效果,

相關的dom內容

<template>
    <div class="index">
      <div class="scroll">
        <ul :style="{top}" :class="{transition:index!=0}">
          <li v-for="(item,index) in list">
            {{"第"+item+"條數據"}}
          </li>
          <li>
            {{"第"+list[0]+"條數據"}}
          </li>
        </ul>
      </div>
      <router-link to="/">hello</router-link>
    </div>
  </template>

相關css內容

img{width: 30px;height: 30px;border-radius: 50%;vertical-align: middle;margin-right: 20px}
ul{position: relative;}
li{overflow: hidden;white-space: nowrap;  text-overflow:ellipsis;width: 80%;height: 60px;line-height: 60px;text-align: left;margin: 0;font-size: 14px}
.scroll{height:60px;overflow: hidden;font-size: 0px;  position: relative;}
.transition{transition: top 0.5s}

相關script內容

<script>
    export default {
      name: ‘HelloWorld‘,
      data() {
        return {
          list: [
            "一","二","三","四","五","六","七","八","九","十"
          ],
          top: "",
          index: 0,
          p:""
        }
      },
      mounted() {
        this.goScroll()
      },
      methods: {
        goScroll() {
          var _this = this;
          this.p = setInterval(() => {
            console.log(22)
            _this.top = -60 * _this.index + ‘px‘;
            if (_this.index >= this.list.length + 1) {
              _this.index = 0;
              _this.top = -0 + ‘px‘;
              clearInterval(_this.p);
              _this.continueScroll()
            } else {
              _this.index++;
            }
          }, 1000)
        }, 
        continueScroll(){
          var _this=this;
        setTimeout(() => {
          _this.index=1;
        _this.top = -60 * this.index+ ‘px‘;
        _this.index++
         this.goScroll()
        },0);
      }
      },
      destroyed(){
        clearInterval( this.p );
      }
    }
  
  </script>
  

vue實現文字上下滾動