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

需求背景:

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

技术实现思路:

使用小程序自带组件swiper

关键点:

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

难点:
  1. 高度自适应失效
  • 描述:切换页面返回 由onhide—>onshow时,出现所有的高度
    会保持在最后计算出的那个值,导致高度自适应效果失效。
  • 原因:是由于此时imageLoad不再监听。
  • 解决办法:watch图片列表,给url加参数(时间戳),使其每次都重新加载,使imageLoad监听。
  1. 前后台切换初始高度不符
  • 描述:切换到后台再返回到前台时,初始高度会保持出现在第一张图片的高度,若切换时非第一张图片,就会导致给当前图片高度不正确,被遮挡或者有大片空白。
  • 原因:给swiper赋值的是 图片列表里第一张的高度。
  • 解决办法:后台切回前台时,appdata是保持不变的,而当前图片排位已被保存变量,所以取当前图片的高度赋值给swiper高度。
  1. 同页面切换初始高度不符
  • 描述:此组件所在页面,下面有跳转到当前页的业务需要,只是渲染数据不同。当返回前一个页面时,初始高度还保留着返回前最后一次的高度,与当前页当前图片高度不符。
  • 原因:同页面切换时,appdata没有重新赋值的话就不会变化,最后当前图片变量取值了最后出现的那个页面的当前图片。
  • 解决办法:每切换到一个页面时,在图片组件里,缓存以页面数:当前图片为键值的currents对象。返回到某个页面时,通过当前页面数取得当前图片,从而获得当前初始高度。
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:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 需求背景: 实现图片轮播功能且高度要自适应。 技术实现思路: 使用小程序自带组件swiper。 关键点:就是要...
    玲儿珑阅读 4,933评论 2 7
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,101评论 3 119
  • 时间管理具体可以参考@warfalcon 老师(read01)和@秋叶 老师(PPT100),都是超级善于管理自己...
    莉莉俺的路西阅读 1,729评论 0 0
  • 自从听到了墨玉桓的表白后,琉璃每天的心情都无比的美好,她知道自己无法改变现状,也就心安的呆在采血玉里面。 虽然被困...
    记搏阅读 2,972评论 1 6
  • 我们站在高山上 蓝天白云画中央 老人驾鹤西游去 不恋繁尘俗世扰
    狠狠爱_ff阅读 2,622评论 0 0

友情链接更多精彩内容