vue制作单向轮播组件

<template>
  <div
    :class="{'swiper': true} ">
    <div
      class="swiper-frame"
      @touchstart= "start"
      @touchmove= "move"
      @touchend= "end"
      @mousedown="dragStart"
      @mousemove="dragMove"
      @mouseup="dragUp"
      @mouseleave="dragUp"
      :style="{transform: 'translateX('+ (canTranslateX ? (pathDistance+'px') : currentIndex+'00%)'),
      transition: 'all' + (canTranslateX ? ' 0s ' : ' 0.65s ') + 'ease 0s'}"
    >
      <div
        v-for="(item, index) in slideShowLists"
        class="swiper-frame-item"
        :key="index"
        :style="{backgroundImage: 'url('+ item.background+')'}"
      >
        <component :is="item.el"></component>
      </div>
    </div>
    <ul class="swiper-dots">
      <li
        :class="{'swiper-dots-li': true ,'swiper-dots-li-active': currentIndex === -index }"
        v-for="(item, index) in slideShowLists" :key="index"
        @click="switchTo(index)"
      ></li>
    </ul>
  </div>
</template>
<script lang="js">

export default {
  name: 'Swiper',
  components: [],
  data () {
    return {
      slideShowLists: this.swiperLists,
      startAbscissa: 0,
      pathDistance: 0,
      endAbscissa: 0,
      canTranslateX: false,
      currentIndex: 0,
      mouse: false,
      shake: this.canShake || false
    }
  },
  props: ['swiperLists', 'canShake'],
  methods: {
    start (event) {
      this.startAbscissa = event.touches[0].clientX
    },
    move (event) {
      this.canTranslateX = true
      const l = event.changedTouches[0].clientX - this.startAbscissa
      if ((this.currentIndex === 0 && l > 0) || (this.swiperLists.length === (-this.currentIndex + 1) && l < 0)) {
        this.canTranslateX = this.shake
      }
      if (this.currentIndex === this.swiperLists.length && l < 0) {
        this.canTranslateX = true
        this.currentIndex++
      } else {
        this.pathDistance = l + (event.target.offsetWidth * this.currentIndex)
      }
    },
    end (event) {
      this.canTranslateX = false
      const l = event.changedTouches[0].clientX - this.startAbscissa
      if (l <= -100 && -this.currentIndex !== this.swiperLists.length - 1) {
        this.currentIndex--
      }
      if (l > 100 && this.currentIndex !== 0) {
        this.currentIndex++
      }
    },
    switchTo (index) {
      this.canTranslateX = false
      this.currentIndex = -index
    },
    dragStart (event) {
      event.preventDefault()
      if (event) {
        this.mouse = true
        this.startAbscissa = event.clientX
      }
    },
    dragMove (event) {
      event.preventDefault()
      if (!this.mouse || !this.shake) {
        return
      }
      const l = event.clientX - this.startAbscissa
      this.canTranslateX = true
      if (this.mouse) {
        this.pathDistance = l + (event.target.offsetWidth * this.currentIndex)
      }
    },
    dragUp (event) {
      if (!this.mouse) {
        return
      }
      this.mouse = false
      this.canTranslateX = false
      const l = event.clientX - this.startAbscissa
      if (l <= -100 && this.currentIndex !== -this.swiperLists.length + 1) {
        this.currentIndex--
      }
      if (l > 100 && this.currentIndex !== 0) {
        this.currentIndex++
      }
    }
  },
  setup (props) {
    console.log(props)
  }
}
</script>
<style scoped lang="less">
.swiper{
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  height: 100%;
  &-bg {
    background-size: cover;
    background-repeat: no-repeat;
  }
  &-frame{
    display: flex;
    flex-direction: row;
    //overflow: hidden;
    height: 100%;
    box-sizing: border-box;
     &-item{
      width: 100%;
      flex-shrink: 0;
      height: 100%;
      box-sizing: border-box;
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }
  }
  &-dots {
    position: absolute;
    bottom: 10%;
    display: flex;
    align-items: center;
    justify-content: center;
    list-style: none;
    width: 100%;
    &-li {
      opacity: 1;
      border-radius: 50%;
      cursor: pointer;
      width: 12PX;
      height: 12PX;
      margin: 0 6PX !important;
      background: #ffaf00;
      box-sizing: border-box;
      &-active {
        border: 2px solid #f8f8f8;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百度一下吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值