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

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

相关推荐
StarChainTech3 小时前
电动车租赁行业的核心需求:智能中控设备的选择与技术方案
物联网·微信小程序·小程序·软件需求·共享经济
计算机毕设指导65 小时前
基于微信小程序的积分制零食自选平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
云起SAAS5 小时前
老年美文文章图文短视频资讯阅读抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·老年美文文章图文短视频资讯阅读
qq_12498707535 小时前
基于微信小程序的民宿预订系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·毕业设计
2501_915106325 小时前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者86 小时前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_916008897 小时前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
说私域7 小时前
人口红利消退与疫情冲击下电商行业的转型路径探索——以开源链动2+1模式S2B2C商城小程序为例
小程序·开源
一室易安7 小时前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app