当前位置:   article > 正文

vue 轮播图实现_uview轮播图 轮播div

uview轮播图 轮播div
<template>
  <div>
    <div class="box">
      <div 
        v-for="item,index in list" 
        :key="index" 
        class="item" 
        :class="{
          'prev': index === 0, 
          'prev-to-right': index === 0 && action === 'toNext', 
          'current': index === 1,
          'current-to-right': index === 1 && action === 'toNext', 
          'current-to-left': index === 1 && action === 'toPrev',
          'next': index === 2,
          'next-to-left': index === 2 && action === 'toPrev'
        }"
      >
        {{item}}
      </div>
      <div class="prev-btn page-btn" @click="toPrevPage">prev</div>
      <div class="next-btn page-btn" @click="toNextPage">next</div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        list: [1,2,3],
        action: '',
        running: false
      }
    },
    methods: {
      // 切换到上一个
      toPrevPage() {
        if (this.running) return
        this.running = true
        this.action = 'toPrev'
        let timer = setTimeout(() => {
          this.list.shift()
          this.list.push(Math.random() * 10)
          this.action = ''
          clearTimeout(timer)
          this.running = false
        }, 1000)
      },
      // 切换到下一个
      toNextPage() {
        if (this.running) return
        this.running = true
        this.action = 'toNext'
        let timer = setTimeout(() => {
          this.list.pop()
          this.list.unshift(Math.random() * 10)
          this.action = ''
          clearTimeout(timer)
          this.running = false
        }, 1000)
      }
    }
  }
</script>

<style scoped lang="scss">
  .box {
    width: 400px;
    height: 200px;
    overflow: hidden;
    position: relative;
    margin: 100px auto;
    .item {
      width: 100%;
      height: 100%;
      background-color: #ccc;
      text-align: center;
      line-height: 200px;
      position: absolute;
      top: 0;
      left: 0;
      &.prev {
        transform: translateX(-100%);
      }
      &.next {
        transform: translateX(100%);
      }
    }
    .prev-to-right {
      animation: prevFadeToRight 1s .2s ease both;
    }
    .current-to-right {
      animation: currentFadeToRight 1s .2s ease both;
    }
    .current-to-left {
      animation: currentFadeToLeft 1s .2s ease both;
    }
    .next-to-left {
      animation: nextFadeToLeft 1s .2s ease both;
    }

    @keyframes prevFadeToRight {
      0% { transform:translateX(-100%); }
      100% { transform:translateX(0%); }
    }
    @keyframes currentFadeToRight {
      0% { transform:translateX(0%); }
      100% { transform:translateX(100%); }
    }
    @keyframes currentFadeToLeft {
      0% { transform:translateX(0%); }
      100% { transform:translateX(-100%); }
    }
    @keyframes nextFadeToLeft {
      0% { transform:translateX(100%); }
      100% { transform:translateX(0%); }
    }

    .page-btn {
      position: absolute;
      top: 20%;
      transform: translateY(-50%);
      cursor: pointer;
      &.prev-btn {
        left: 20px;
      }
      &.next-btn {
        right: 20px;
      }
    }
  }
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/251070
推荐阅读
相关标签
  

闽ICP备14008679号