Swiper实现保持卡片悬停位置居中

前言

Swiper现在其实还是有一定问题的,但是可以说已经风靡小程序,在小程序中随处可见这种设计效果,吐槽归吐槽,还是要用起来

实现方式一

看代码
  • wxml代码
<view class='college-container'>
  <swiper indicator-dots="{{false}}" autoplay="{{false}}" previous-margin='80rpx' next-margin='80rpx'>
    <block wx:for="{{imgUrls}}" wx:for-item='item' wx:key=''>
      <swiper-item>
        <view class="shuffing-item">
          <image src="{{item.url}}"></image>
        </view>
      </swiper-item>
    </block>
  </swiper>
</view>
  • wxss代码
.college-container{
  display: flex;
  flex-direction: column;
}

swiper{
  height:500rpx;
  margin:20rpx 0rpx;
}

.shuffing-item{
  position: absolute;
  width:90%;
  height:500rpx;
  left: 50%;
  right:50%;
  transform: translateX(-50%) translateY(-50%);
  transition: all 0.3s;
}
.shuffing-item-next{
  width:85%;
  height:85%;
  transform:translateX(-100%) translateY(-50%);
  transition: all 0.3s;
}
.shuffing-item-preo{
  width:85%;
  height:85%;
  transform:translateX(40%) translateY(-50%);
  transition: all 0.3s;
}
.shuffing-item>image{
  width:100%;
  height:100%;
}
  • 数据源
data: {
    imgUrls: [
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1091628847,41930541&fm=26&gp=0.jpg',
        isChange: 1,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=597216490,672508208&fm=26&gp=0.jpg',
        isChange: 2,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3173577287,1130280921&fm=26&gp=0.jpg',
        isChange: 3,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=858959453,1988929777&fm=26&gp=0.jpg',
        isChange: 4,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3797481993,1929347741&fm=26&gp=0.jpg',
        isChange: 5,
      }
    ]
  }
实现效果附图一张(忽略上方黑边)
image.png

实现方式二

看代码
  • wxml代码
<view class='college-container'>
  <swiper class="swiper-block" autoplay="{{true}}" circular='{{true}}' bindchange="swiperChange" previous-margin="80rpx" next-margin="80rpx" current="0">
    <block wx:for="{{imgUrls}}" wx:index="{{index}}" wx:key='index'>
      <swiper-item class="swiper-item">
        <image mode="aspectFill" src="{{item.url}}" class="slide-image {{currentIndex == index ? 'active' : ''}}" />
      </swiper-item>
    </block>
  </swiper>
</view>
  • wxss代码
.college-container {
  display: flex;
  flex-direction: column;
}

.swiper-block {
  height: 480rpx;
  width: 100%;
}

swiper-item{
  height: 90% !important;
}

.swiper-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  overflow: unset;
}

.slide-image {
  height: 320rpx;
  width: 520rpx;
  border-radius: 9rpx;
  box-shadow: 0px 0px 30rpx rgba(0, 0, 0, 0.2);
  margin: 0rpx 30rpx;
  z-index: 1;
}

.active {
  transform: scale(1.14);
  transition: all 0.2s ease-in 0s;
  z-index: 20;
}
  • page的js代码
data: {
    currentIndex:0,
    imgUrls: [
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1091628847,41930541&fm=26&gp=0.jpg',
        isChange: 1,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=597216490,672508208&fm=26&gp=0.jpg',
        isChange: 2,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3173577287,1130280921&fm=26&gp=0.jpg',
        isChange: 3,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=858959453,1988929777&fm=26&gp=0.jpg',
        isChange: 4,
      },
      {
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3797481993,1929347741&fm=26&gp=0.jpg',
        isChange: 5,
      }
    ]
  },

  swiperChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
实现效果附图一张(忽略上方黑边)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飘阅读 5,489评论 0 6
  • 转载链接 注:本文转载知乎上的回答 作者:初雪 链接:https://www.zhihu.com/question...
    pengshuangta阅读 28,984评论 9 295
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,344评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,472评论 2 59
  • 这是朋友的话题,我也来写一写。 如果有来生,我依然想要这样的父母。 父母的婚姻是经过别人的介绍认识,听妈说:以前嫁...
    惠芝阅读 3,480评论 2 1