# 左右滑动

# 一、效果图

image

# 二、使用方式

直接引用本页面

# 三、代码

<template>
  <div class="mis-carousel" ref="mis-carousel">
    <div
      class="mis-carousel-group clear"
      :style="{width:`${(carouselList.length)*itemWidth}px`,transform: `translateX(${translateX}px)`}"
    >
      <div
        class="mis-carousel-item fl"
        :style="{width:`${itemWidth}px`}"
        v-for="(item,index) in carouselList"
        :key="index"
      >
        <div class="mis-carousel-cell">{{item}}</div>
      </div>
    </div>
    <div
      class="mis-carousel-tools mis-carousel-prev mis-icon"
      :class="{'active':isMoveRight}"
      @click="carouselPrev()"
    ></div>
    <div
      class="mis-carousel-tools mis-carousel-next mis-icon"
      :class="{'active': thisPower !=0}"
      @click="carouselNext()"
    ></div>
  </div>
</template>
<script>
export default {
  name: "mis-carousel",
  data() {
    return {
      /* 轮播图开始 */
      itemWidth: 320, // 宽度
      thisPower: 0, // 当前第几个
      translateX: 0, // 移动位置
      isMoveRight: true, // 是否可向右移动
      step: 2, // 每次滑动几个
      carouselList: [1, 2, 3, 4, 5, 6, 7, 8, 9],
      /* 轮播图结束 */
    };
  },
  methods: {
    /* 轮播开始 */
    carouselPrev() {
      // 上一页
      let thisWidth = this.itemWidth; // 宽度
      let carouselWidth = this.$refs["mis-carousel"].offsetWidth; // 页面宽度
      let carouselLength = this.carouselList.length; // 数据长度

      if (
        thisWidth * this.step * -this.thisPower + carouselWidth <
        thisWidth * (carouselLength - this.step)
      ) {
        // 没超出时候
        this.thisPower = this.thisPower - this.step; // 设置当前状态
        this.translateX = this.thisPower * (thisWidth + this.itemRight); // 滚动位置
        this.isMoveRight = true; // 是否可向右移动
      } else {
        // 超出显示最后一个 
        this.translateX = -((thisWidth + this.itemRight) * carouselLength - carouselWidth);
        this.isMoveRight = false; // 是否可向右移动
      }
    },
    carouselNext() {
      // 下一页
      if (this.translateX < 0) {
        this.thisPower = this.thisPower + this.step; // 设置当前状态
        this.translateX = this.thisPower * this.itemWidth; // 滚动位置
        this.isMoveRight = true; // 是否可向右移动
      }
    }
    /* 轮播结束 */
  }
};
</script>
<style lang="scss" scoped>
.mis-carousel {
  padding: 6px 0 6px 6px;
  margin-bottom: 10px;
  position: relative;
  overflow: hidden;
  .mis-carousel-group:not(:last-child) {
    transition: all 0.3s;
    .mis-carousel-item {
      height: 124px;
      padding-right: 20px;
      margin-bottom: 10px;
      .mis-carousel-cell {
        background-color: #ffffff;
        height: inherit;
        border-radius: 6px;
        position: relative;
        cursor: pointer;
        transition: all 0.3s;
        box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.3);
        &:hover {
          box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.7);
        }
      }
    }
  }

  .mis-carousel-tools {
    display: inline-block;
    width: 20px;
    height: 50px;
    position: absolute;
    cursor: pointer;
    transition: all 0.3s;
    background-color: rgba(#000000, 0.6);
    &:hover {
      background-color: rgba(#000000, 0.7);
    }
    &.active {
      background-color: rgba(#4170dc, 0.8);
      &:hover {
        background-color: rgba(#4170dc, 0.9);
      }
    }
    &.mis-carousel-prev {
      top: 6px;
      right: 0;
      background-position: -6px 5px;
    }
    &.mis-carousel-next {
      bottom: 6px;
      right: 0;
      background-position: -36px 5px;
    }
  }
}
</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
133
更新时间: 2020年8月24日下午2点50分