微信小程序轮播图高度自适应优化

html 复制代码
  <view class="swiper-wrap">
    <swiper indicator-dots circular class="swiper" style="height: {{swiperHeight}}">
      <swiper-item wx:for="{{imgList}}" wx:key="this">
        <view class="img-box">
          <image src="{{item}}" mode="widthFix" class="swiper-img" />
        </view>
      </swiper-item>
    </swiper>
  </view>
javascript 复制代码
const app = getApp()

Page({
  data: {
    imgList : [
      'http://siyuds.cc/uploads/attach/2025/12/20251212/de6e6a90ec0e47b7a249e6dd097ae3db.jpg',
      'http://siyuds.cc/uploads/attach/2025/12/20251212/7d4d78ce86f485abf4ea09ab4c105f6b.png',
      'http://siyuds.cc/uploads/attach/2025/12/20251212/f55161f8af944c62c78d2ef2d184e70b.png'
    ],
    swiperHeight:230
  },
  onLoad() {
    console.log('代码片段是一种迷你、可分享的小程序或小游戏项目,可用于分享小程序和小游戏的开发经验、展示组件和 API 的使用、复现开发问题和 Bug 等。可点击以下链接查看代码片段的详细文档:')
 
    this.getElementTop();
  },
  

    getElementTop() {
      let that = this;
      setTimeout(() => {
        wx.createSelectorQuery()
          .in(this)
          .select(".swiper-img")
          .boundingClientRect((data) => {
            console.log(data)
            if (data) {
              // that.swiperHeight = data.height + 30;
              this.setData({ swiperHeight: data.height + 'px' });
            }
          })
          .exec();
     }, 0);
  },
  
})
css 复制代码
.swiper-wrap {
  width: 100%;
  box-sizing: border-box;
  height:200px
}
.swiper {
  width: 100%;
  height:auto;
}
.img-box {
  width: 100%;
  margin: 0;
  padding: 0;
}
.swiper-img {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}

css这里主要是设置swiper的高度自动,覆盖默认的150px

相关推荐
宁夏雨科网2 小时前
文具办公用品小程序商城,开发一个难吗
小程序·商城小程序·文具小程序·文具商城
说私域9 小时前
开源链动2+1模式商城小程序在深度分销数字化转型中的应用研究
人工智能·小程序·开源·流量运营·私域运营
咖啡の猫10 小时前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
咖啡の猫10 小时前
微信小程序全局数据共享
微信小程序·小程序
桐溪漂流11 小时前
微信小程序cli脚本预览上传
微信小程序·小程序
咖啡の猫11 小时前
微信小程序使用 npm 包
微信小程序·小程序·npm
说私域11 小时前
开源链动2+1模式商城小程序的营销技术与私域运营策略研究
人工智能·小程序·开源·流量运营·私域运营
小小王app小程序开发1 天前
淘宝扭蛋机小程序核心玩法拆解与技术运营分析
大数据·小程序
说私域1 天前
AI智能名片商城小程序数据清洗的持续运营策略与实践研究
大数据·人工智能·小程序·流量运营·私域运营
东东5161 天前
xxx食堂移动预约点餐系统 (springboot+微信小程序)
spring boot·微信小程序·小程序·毕业设计·个人开发·毕设