注册登录

使用mpvue开发小程序原生的swiper,实现酷炫banner

2018-09-18
导读:使用mpvue开发小程序原生的swiper,实现酷炫banner 也是一种很实用的技能,下面呢,就是具体的实现方法以及效果了:...

使用mpvue开发小程序原生的swiper,实现酷炫banner 也是一种很实用的技能,下面呢,就是具体的实现方法以及效果了:

mpvue的该组件也是基于小程序原生的swiper组件实现的,具体的属性我就不再挨个介绍了,毕竟官方文档里写的很清楚了~这里就主要说下我们要实现上图中的banner要依赖的最重要的两个属性previous-margin和next-margin,前者主要作用是「露出前一项的一小部分」,后者主要作用是「露出后一项的一小部分」,好了,我们先把mpvue-swiper组件介绍中的代码copy 过来:

<template>
  <div class="page">
    <view class="page__hd">
      <view class="page__title">Swiper</view>
      <view class="page__desc">滑块视图容器,这里采用小程序原生 swiper 组件实现。</view>
    </view>
    <div class="page__bd page__bd_spacing">
      <swiper :indicator-dots="indicatorDots" 
        :autoplay="autoplay" 
        :interval="interval" 
        :duration="duration" 
        :circular="circular" 
        @change="swiperChange" 
        @animationfinish="animationfinish">
        <div v-for="item in imgUrls" :key="index">
          <swiper-item>
            <image :src="item" class="slide-image" />
          </swiper-item>
        </div>
      </swiper>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 900,
      circular: true,
      imgUrls: [
        'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
        'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
      ]
    }
  },
  methods: {
    swiperChange(e) {
      console.log('第' + e.mp.detail.current + '张轮播图发生了滑动');
    },
    animationfinish(e) {
      console.log('第' + e.mp.detail.current + '张轮播图滑动结束');
    }
  }
}
</script>
<style>
.slide-image {
  width: 100%;
  height: 100%;
}
</style>
复制代码

粘完这些代码,你能实现一个很常规的banner了,然后我们加上刚刚我们提到的那两个属性:

<swiper :indicator-dots="indicatorDots" 
        :autoplay="autoplay" 
        :interval="interval" 
        :duration="duration" 
        :circular="circular" 
        :previous-margin="'60rpx'"
        :next-margin="'60rpx'"
        @change="swiperChange" 
        @animationfinish="animationfinish">
        <div v-for="item in imgUrls" :key="index">
          <swiper-item>
            <div class="img-wrapper">
                <image :src="item" class="slide-image" />
            </div>
          </swiper-item>
        </div>
    </swiper>
复制代码

这时候你就实现了一个能将前一项和后一项各露出60rpx的banner了,只不过此时各项的图片大小都是相同的,那怎么实现主项的图片大小的放大呢,当然是使用css的transform给图片标签加各放大的样式,且往下看代码:

<swiper :indicator-dots="indicatorDots" 
        :autoplay="autoplay" 
        :interval="interval" 
        :duration="duration" 
        :circular="circular" 
        :previous-margin="'60rpx'"
        :next-margin="'60rpx'"
        @change="swiperChange" 
        @animationfinish="animationfinish">
        <div v-for="item in imgUrls" :key="index">
          <swiper-item>
            <div class="img-wrapper"
                :style="{
                    boxSizing: 'border-box', 
                    width: '100%', 
                    height: '100%', 
                    display: 'flex', 
                    justifyContent: 动态值,需要根据设计图以及banner图片的个数以及位置进行计算得出, 
                    padding: 动态值,需要根据设计图以及banner图片的个数以及位置进行计算得出
                }">
                <image :src="item" 
                    class="slide-image" 
                    :style="{
                        transform: currentIndex===bannerIndex?'scale(' + scaleX + ',' + scaleY + ')':'scale(1,1)',
                        transitionDuration: '.3s',
                        transitionTimingFunction: 'ease'
                    }"/>
            </div>
          </swiper-item>
        </div>
    </swiper>
复制代码

其中几个出现的参数:

currentIndex:即当前展现的banner项的索引

bannerIndex:即banner项在整个图片列表中的索引

scaleX以及scaleY:即你希望的主项的放大的倍数,此项的值可能需要我们根据屏幕宽度以及设计稿的展示来进行计算

这几个样式就是:将当前展示的图片放大一定的倍数

到了这里,我们需要的结构以及style上的代码基本上都有了,下面主要是script里对一些关键的参数进行控制,这里有个比较重要的函数@change

<script>
data () {
    return {
        autoplay: false,
        interval: 3000,
        duration: 300,
        circular: true,
        currentIndex: 0,
        scaleX: (634 / 550).toFixed(4),
        scaleY: (378 / 328).toFixed(4)
    }
},
methods: {
    // 控制currentIndex以及动画执行索引descIndex的值
    swiperChange (e) {
        const that = this
        this.currentIndex = e.mp.detail.current
        this.scaleX = (634 / 550).toFixed(4)
        this.scaleY = (378 / 328).toFixed(4)
    }
}
</script>
复制代码
3.注意点

至此呢,主图中的banner的主要效果基本已经实现了,看下来其实并不是很难,主要是一些细节需要特别注意:

3.1:previous-margin和next-margin的值

它们的值并不是随便写的,需要你根据设计图去进行细微的计算

3.2:swiper-item内,image标签外的class名为img-wrapper的div容器的样式

其中我没有写出具体值的两项属性:justifyContent与padding,他们的具体值同样需要你去进行计算,此时的计算不止会涉及到设计稿,他们的值还会根据当前展示出来的三张图片在整个imgList(至少三项)中的顺序的不同而不同,在我的实现中我使用了超长的三目运算符来保证每个图片的具体的属性值。。。

重磅推荐:小程序开店目录

第一部分:小商店是什么

第二部分:如何开通一个小商店

第三部分:如何登录小商店

第四部分:开店任务常见问题

第五部分:小商店可以卖什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收货/物流

第九部分:小程序怎么结算

第十部分:小程序客服

第十一部分:电商创业

第十二部分:小程序游戏开发

电话咨询 微信咨询 预约演示 0元开店