vue原生方式实现swiper效果

原生方式实现swiper效果

在vue项目下 以组件形式导入使用即可

支持点击切换、滑动切换

思路

1、 写轮播牌子,数量若干,限定在一个盒子内 横向排列
2、写两组样式 一个是放大的 一个是缩小的
3、js监听鼠标落下和抬起事件
落下时记录offeset位置
抬起时计算位移距离
判断是否满足切换条件
将放大样式置于满足条件的元素
4、补齐css过渡动画

效果

在这里插入图片描述

<template>
  <div>
    <div
      class="swip"
      ref="swip"
      @mousedown="inDown"
      @mousemove="inMove"
      @mouseup="inUp"
      @mouseleave="inLeave"
    >
      <div class="btn leftBtn" @click="toLeft">《-</div>
      <div class="btn rightBtn" @click="toRight">-》</div>
      <div
        class="swipInner"
        ref="swipInner"
        :style="{ width: total * 305 + 'px' }"
      >
        <div class="swips" v-for="i in total" :key="i">
          <div
            style="border-radius: 7px"
            :class="current == i ? 'selected' : 'notSelected'"
          ></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "swipInner",
  components: {},
  watch: {
    current(val) {
      this.$refs["swipInner"].style.left = 0 - (val - 2) * 305 + "px";
    },
  },
  data() {
    return {
      total: 12,
      current: 2,
      promit: false,
      initNumb: false,
      perNumb: false,
    };
  },
  mounted() {},
  methods: {
    inDown(e) {
      this.promit = true;
      this.initNumb = e.clientX;
      this.$refs["swipInner"].style.transitionDuration = "unset";
    },
    inMove(e) {
      if (this.promit) {
        this.$refs["swipInner"].style.left =
          Number(this.$refs["swipInner"].style.left.slice(0, -2)) +
          (e.clientX - this.initNumb - this.perNumb || 0) +
          "px";
        this.perNumb = e.clientX - this.initNumb;
      }
    },
    inUp() {
      this.promit = false;
      this.perNumb = false;
      this.$refs["swipInner"].style.transitionDuration = "0.55s";
      this.current = Math.floor(
        0 - Number(this.$refs["swipInner"].style.left.slice(0, -2)) / 305 + 2
      );
    },
    inLeave() {
      this.promit = false;
      this.perNumb = false;
      this.$refs["swipInner"].style.transitionDuration = "0.55s";
      this.current = Math.floor(
        0 - Number(this.$refs["swipInner"].style.left.slice(0, -2)) / 305 + 2
      );
    },
    toLeft() {
      if (this.current > 1) {
        this.current -= 1;
      } else {
        this.current = this.total;
      }
    },
    toRight() {
      if (this.current < this.total) {
        this.current += 1;
      } else {
        this.current = 1;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.swip {
  height: 280px;
  width: 920px;
  background-color: rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
  .swipInner {
    position: absolute;
    left: 0;
    top: 0;
    background-color: rgba(255, 255, 40, 0.4);
    transition-duration: 0.55s;
    height: 280px;
  }
  .swips {
    height: 280px;
    width: 300px;
    display: inline-block;
    margin-left: 5px;
  }
  .selected {
    height: 280px;
    width: 300px;
    background-color: aqua;
    transition-duration: 0.25s;
  }
  .notSelected {
    height: 240px;
    margin-top: 20px;
    width: 250px;
    margin-left: 25px;
    background-color: aqua;
    transition-duration: 0.25s;
  }
  .btn {
    height: 22px;
    width: 22px;
    background-color: rgba(255, 255, 255, 0.7);
    position: absolute;
    border-radius: 50%;
    cursor: pointer;
    top: calc(50% - 10px);
    z-index: 10;
  }
  .leftBtn {
    left: 10px;
  }
  .rightBtn {
    right: 10px;
  }
}
</style>

其他需求 自行优化修改

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
vue-awesome-swiper是一个基于Vue.js的插件,用于实现H5滑动翻页效果。它是在swiper的基础上进行了封装,使得在Vue项目中使用swiper变得更加方便和灵活。 要在vue中使用vue-awesome-swiper,首先需要在main.js中引入VueAwesomeSwiper插件,并导入swiper的样式文件。具体的代码如下: import VueAwesomeSwiper from 'vue-awesome-swiper' import 'swiper/dist/css/swiper.css' Vue.use(VueAwesomeSwiper) 这样就可以在Vue组件中使用vue-awesome-swiper实现H5滑动翻页效果了。你可以在需要使用swiper的组件中引入并注册vue-awesome-swiper的组件,然后使用组件的方式来配置和控制swiper的各种参数和功能。通过配置不同的选项,你可以实现各种不同的滑动翻页效果,满足你的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue案例 - vue-awesome-swiper实现h5滑动翻页效果](https://blog.csdn.net/weixin_33881050/article/details/93267999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue移动端使用swiper+vue-awesome-swiper实现滑动选择](https://blog.csdn.net/weixin_45842078/article/details/119141775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值