小程序swiper限高高度自适应解决方案

方案一:满屏
QQ截图20181212112423.png
/*wxml*/
 <swiper style='width:100%;height:100%;' current='{{current}}' bindchange='bindChange'>

切记是行内样式

/*wxss*/
swiper-item {
  overflow: scroll;
}

ok完美解决!

方案二:半屏

这个才是重点,小二,上酸菜!!


QQ截图20181212113929.png

这个就不能行内设置height:100%;了,要根据元素节点动态设置高,红框中的是我的循环元素,要获得每个swiper-item的高

//js
get_wxml: function (className, callback){
    wx.createSelectorQuery().selectAll(className).boundingClientRect(callback).exec()
  },
wx.request({
      url: baseUrl + 'm=wttapi&a=getcategories',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        that.setData({
          scrollNav: res.data.data[1],
          scrollContent: res.data.data[2],
          indexTit: res.data.data[1][0].name
        })
        let change_id = that.data.scrollNav[that.data.idx].cid//动态获取每个swiper-item元素节点的id
        that.setData({
          swiper_length: that.data.scrollNav.length-1//获取节点数量
        })
        //获取节点信息
        that.get_wxml(`#cont${change_id}`,(rects)=>{
          that.setData({
            height:rects[0].height
          })
        })
      }
    })
/*wxml*/
<swiper style='width:100%;height:{{height}}px;background:#F5F5F5;' current='{{current}}' bindchange='moveSwiper'>
    <swiper-item wx:for='{{scrollNav}}' wx:for-item='item' wx:key='index' class='swiper_content' item-id='{{item.cid}}'>
      <!-- <view class='loading'>
        <image src='/images/loading.gif'></image>
        <text>努力加载中</text>
      </view> -->
      <view id='cont{{item.cid}}'>
        <view wx:for='{{scrollContent}}' wx:key='index' id='cont{{item[0].pcid}}'>
          <view class='content-title'>
            <image src='/images/lefticon@2x.png'></image>
            <text style='display:inline-block;margin:0 10rpx;'>{{item[0].pcname}}</text>
            <image src='/images/righticon@2x.png'></image>
          </view>
          <view class='content-box'>
            <view wx:for='{{scrollContent[index]}}' wx:key='index' class='content-box-list' data-id='{{item.ccid}}' data-name='{{item.name}}' bindtap='intoList'>
              <image src='{{item.image}}' style='max-width:160rpx;max-height:160rpx;'></image>
              <view class='content-box-price'>
                <view>
                  <text class='price-text1 fontweight'>{{item.name}}</text>
                  <text class='price-text2'>今日最值</text>
                </view>
                <text class='price-text3'>{{item.price}}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </swiper-item>
  </swiper>

ok,小白一枚,以上两种方案是小程序初步踩坑经验

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

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,697评论 1 92
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,917评论 0 17
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,415评论 1 45
  • 考研分数出来了,嗯,没有一战的时候多,都怪自己太感性以及幼稚。下面说些失败经验,能让一个人少走弯路都是最大的欣慰。...
    加油冲哇阅读 3,770评论 0 0
  • 借我明媚如朝阳 借我追风如少年 借我疾驰以骏马 驰骋万里数中原 借我羽纱千尺卷 素裹款款至桃园 借我酒情执飞天 敢...
    蓼城明月阅读 2,865评论 15 5

友情链接更多精彩内容