微信小程序swiper控件高度自适应

在小程序开发文档中,swiper控件默认高度为150,高度必须设置具体的值,所以为了适应不同分辨率的设备,通过动态的获取屏幕空白部分高度在设置swiper控件的高度

主要用到两个API:

1、getSystemInfo(获取当前设备的屏幕信息)

2、createSelectorQuery(这里的作用是获取其他控件的高度)

在wxml文件中用一个view控件将表头包裹起来,然后设置class为all_view

在js中的data中添加两个字段

    // 页面总高度将会放在这里
    windowHeight: 0,
    // scroll-view的高度
    swiperViewHeight: 0

在js中新建一个方法:setSwiperHeight,用来动态设置swiper控件的高度

setSwiperHeight: function () {
    let that = this

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight,
        });
      }
    });
    // 根据文档中的介绍,先创建一个SelectorQuery对象实例
    let query = wx.createSelectorQuery().in(this);

    query.select('.all_view').boundingClientRect();

    // 执行上面所指定的请求,结果会按照顺序存放于一个数组中,在callback的第一个参数中返回
    query.exec((res) => {
      //取出表头高度
      let headerHeight = res[0].height;
      // 计算出去除表头剩余的高度
      let swiperViewHeight = this.data.windowHeight - headerHeight;

      this.setData({
        swiperViewHeight: swiperViewHeight
      });
    });

  },

然后在wxml中将数据与控件绑定即可

    <swiper current="{{TabCur}}" duration="300" easing-function="easeOutCubic" style="height:{{swiperViewHeight}}px">
        <swiper-item>
            <view class="swiper_item_0">
            testestsetsetsetsettsette
        </view>

        </swiper-item>
        <swiper-item><view>test</view></swiper-item>
    </swiper>

完成后效果如下

微信小程序swiper组件默认是根据图片的高度来设置swiper高度的,所以如果要实现swiper高度自适应的效果,可以采用以下几种方式: 1. 使用image组件获取图片的高度:在swiper-item中使用image组件来展示图片,并设置mode为aspectFill,然后通过image组件的bindload事件获取到图片加载后的高度,并将该高度动态赋值给swiper组件的高度属性。例如: ```html <swiper style="height:{{swiperHeight}}px;" current="{{current}}"> <block wx:for="{{imageList}}" wx:key="*this"> <swiper-item> <image src="{{item}}" mode="aspectFill" bindload="getImageHeight"></image> </swiper-item> </block> </swiper> ``` ```javascript Page({ data: { swiperHeight: 0, current: 0, imageList: ['image1.png', 'image2.png', 'image3.png'], }, getImageHeight: function(e) { const { index, height } = e.currentTarget.dataset; this.setData({ swiperHeight: height }); } }) ``` 2. 使用wx.createSelectorQuery获取图片高度:在onLoad生命周期函数中使用wx.createSelectorQuery来获取图片的高度,并将该高度动态赋值给swiper组件的高度属性。例如: ```javascript Page({ data: { swiperHeight: 0, current: 0, imageList: ['image1.png', 'image2.png', 'image3.png'], }, onLoad: function() { const query = wx.createSelectorQuery(); query.select('.swiper-item-image').boundingClientRect(res => { this.setData({ swiperHeight: res.height }); }).exec(); } }) ``` ```html <swiper style="height:{{swiperHeight}}px;" current="{{current}}"> <block wx:for="{{imageList}}" wx:key="*this"> <swiper-item> <image class="swiper-item-image" src="{{item}}" mode="aspectFill"></image> </swiper-item> </block> </swiper> ``` 以上两种方式都可以实现swiper高度自适应的效果,你可以根据自己的实际需求选择其中一种方法来实现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值