小程序

小程序踩坑记2md—图片轮播高度自适应组件

2018-09-12  本文已影响178人  玲儿珑

需求背景:

实现图片轮播功能且高度要自适应。

技术实现思路:

使用小程序自带组件swiper

关键点:

就是要计算出当前图片的高度并赋值给swiper高度。需要计算是由于swiper必须指定高度不能像div一样自动撑开。

难点:
  1. 高度自适应失效
  1. 前后台切换初始高度不符
  1. 同页面切换初始高度不符
PS:

在设计和解决这些难点时,均遵循着组件的高内聚、低耦合原则,使得更具复用性、稳定性、无依赖。

具体实现:

模版template:
<template>
  <view class="com_imagesSwiper">
    <swiper class="com_img_auto_swiper" indicator-dots="true" autoplay="{{false}}" circular="true" bindchange="bindchange" style="height:{{swiperHeight}}px;">
      <block wx:for="{{imgUrls}}" wx:key="{{index}}">
        <swiper-item>
          <image src="{{item}}" class="com_swiper_image" mode="widthFix" data-id='{{index}}' bindload="imageLoad"/>
        </swiper-item>
      </block>
    </swiper>
  </view>
</template>
脚本script:
<script>
import wepy from 'wepy'
export default class ImgSwiper extends wepy.component{
  props = {
    imgUrls: Array
  }
  data = {
    // imgUrls : [
    //   // '/m/static/img/weixin/act_hongbao.png'
    // ],
    imgheights: [], //所有图片的高度  (必须)
    imgwidth: 750,  //图片宽度 兼容
    current: 0, //默认  (必须)
    swiperHeight: 150,
    currentPage: 1
  };
  watch = {
    imgUrls(newValue, oldValue) {
      if (newValue) {
        for (let i = 0; i < newValue.length; i++) {
          newValue[i] = newValue[i] + '?' + new Date().getTime()
        }
      }
    }, 
    currentPage(newValue, oldValue) {
      this.current = JSON.parse(wepy.getStorageSync('currents'))[this.getCurrentPages().length] - 0
      this.$apply()
    },
    current(newValue, oldValue) {
      let key = this.getCurrentPages().length+''
      let obj = JSON.parse(wepy.getStorageSync('currents'))
      obj[key] = this.current
      wepy.setStorageSync('currents', JSON.stringify(obj))
    }
  };
  methods = {
    imageLoad: function (e) {//获取图片真实宽度  
      this.currentPage = this.getCurrentPages().length
      let imgwidth = e.detail.width,
          imgheight = e.detail.height,
          ratio = imgwidth / imgheight;
      let viewHeight = this.imgwidth / ratio;
      this.imgheights[e.target.dataset.id] = viewHeight;
      this.swiperHeight = this.imgheights[this.current]
      this.$apply()
    },
    bindchange: function (e) {
      this.current = e.detail.current
      this.swiperHeight = this.imgheights[e.detail.current]
      this.$apply()
    }
  }
  onLoad() {
    this.imgwidth = wx.getSystemInfoSync().screenWidth
    this.$apply()
    if (!wepy.getStorageSync('currents')) {
      wepy.setStorageSync('currents', JSON.stringify({1 : 0}))
    } else {
      let obj = JSON.parse(wepy.getStorageSync('currents'))
      obj[this.getCurrentPages().length] = 0
      wepy.setStorageSync('currents', JSON.stringify(obj))
    }
  }
}
</script>
样式style:
<style lang="less">
//高度自适应图片轮播组件
.com_imagesSwiper { 
  .com_img_auto_swiper { 
    transition: height 0.5s ease;
    .com_swiper_image {
      width: 100%;
      height: 100%;
    }
    .wx-swiper-dot {
      width: 20rpx;
      display: inline-flex;
      height: 2rpx;
      justify-content: space-between;
    }
    .wx-swiper-dot::before {
      content: '';
      flex-grow: 1;
      background: rgba(255, 255, 255, 0.6);
      border-radius: 8rpx
    }
    .wx-swiper-dot-active::before {
      background: rgba(255, 255, 255, 1);
    }
  }
}
</style>

Notes:

很多时候开始发现是未解的,待解决之后发现原来并没什么,😄,希望我们在发现问题解决问题的路上结伴而行孜孜不倦~ 有写的不到之处望能不吝赐教,欢迎随时交流,共勉~ 😊

上一篇 下一篇

猜你喜欢

热点阅读