swiper实现点击后元素始终在中间位置

介绍

Vue中引入swiper,实现多个元素平铺排列时,点击后元素始终会滚到中间位置,并且可以拖拽,

引入swiper

import Swiper from ‘swiper’

dom示例

    <div class="swiper"  ref="myswiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide "
             v-for="(item,index) in detail.ques"
             :key="item.id">{{index+1}}</div>
      </div>
    </div>

swiper初始化

  mounted () {
    this.swiper = new Swiper('.swiper', {
      slidesPerView: 'auto',
      spaceBetween: 26,
      centeredSlides: true,
      slideToClickedSlide: true,
      observer: true, // 修改swiper自己或子元素时,自动初始化swiper
      observeParents: true, // 修改swiper的父元素时,自动初始化swiper
      touchEventsTarget: 'container' // 接受touch事件的目标
    })
    this.swiper.on('slideChange', (e) => {
      const { activeIndex } = e
      this.tabChange(activeIndex)
    })
    this.swiper.slideTo(this.index, 1000, false)
    console.log('this.swiper', this.swiper)
  },

样式示例

  .swiper{
    margin-left: auto;
    margin-right: auto;
    position: relative;
    overflow: hidden;
    list-style: none;
    padding: 0;
    z-index: 1;
    touch-action: pan-y;
    .swiper-wrapper{
      position: relative;
      width: 100%;
      height: 100%;
      z-index: 1;
      display: flex;
      transition-property: transform;
      box-sizing: content-box;
      padding: 24px 0;
      .swiper-slide{
        position: relative;
        width: 90px;
        min-width:90px;
        height: 90px;
        font-size: 36px;
        text-align: center;
        line-height: 90px;
        border-radius: 50%;
        border: 1px solid $theme-stu-error;
        transition-property: transform;
      }
    }
    .swiper-slide-active{
      background:$theme-stu-error;
      color:#fff;
      &::after {
        content: "";
        position: absolute;
        bottom:-24px;
        left: calc(45px - 18px);
        background-color: #f64e5e;
        width: 36px;
        height: 4px;
      }
    }
    .swiper-scrollbar {
      border-radius: 10px;
      position: relative;
      -ms-touch-action: none;
      background: rgba(0,0,0,.1);
      position: absolute;
      left: 1%;
      bottom: 3px;
      z-index: 50;
      height: 5px;
      width: 98%;
    }
  }

效果图

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值