Vue实现一个文字滚动

688 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue文字轮播 </title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
    <style>
      div, ul, li, span, img {
        margin: 0;
        padding: 0;
        display: flex;
        box-sizing: border-box;
      }
      .marquee {
        width: 100%;
        height: 150px;
        align-items: center;
        color: #3A3A3A;
        background-color: #b3effe;
        display: flex;
        box-sizing: border-box;
      }
      .marquee_title {
        padding: 0 20px;
        height: 30px;
        font-size: 14px;
        border-right: 1px solid #d8d8d8;
        align-items: center;
      }
      .marquee_box {
        display: block;
        position: relative;
        width: 60%;
        height: 90%;
        overflow: hidden;
      }
      .marquee_list {
        display: block;
        position: absolute;
        top: 0;
        left: 0;
      }
      .marquee_top {
        transition: all 1s;
        margin-top: -30px
      }
      .marquee_list li {
        height: 30px;
        line-height: 30px;
        font-size: 14px;
        padding-left: 20px;
      }
      .marquee_list li span {
        padding: 0 2px;
      }
      .red {
        color: #FF0101;
      }

    </style>

</head>
<body>

<div class="vueBox">
    <div class="marquee">
        <div class="marquee_title">
            <span>滚动列表</span>
        </div>
        <div class="marquee_box">
            <ul class="marquee_list" :class="{marquee_top:animate}">
                <li v-for="(item, index) in marqueeList" :key="index">
                    <span>{{item.name}}</span>

                </li>
            </ul>
        </div>
    </div>
</div>

<script type="text/javascript">
  const vm = new Vue({
    el: '.vueBox',
    data: {
      animate: false,
      marqueeList: [
        {
          name: '1',

        },
        {
          name: '2',

        },
        {
          name: '3',

        },
        {
          name: '4'

        }
      ]
    },
    created: function () {
      setInterval(this.showMarquee, 1000)
    },
    methods: {
      showMarquee: function () {
        this.animate = true
        setTimeout(() => {
          this.marqueeList.push(this.marqueeList[0])
          this.marqueeList.shift()
          this.animate = false
        }, 500)

      }
    }
  })
</script>
</body>
</html>