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

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

相关推荐
QT.qtqtqtqtqt9 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
CHU72903511 小时前
直播商城APP前端功能全景解析:打造沉浸式互动购物新体验
java·前端·小程序
黑客老李20 小时前
web渗透实战 | js.map文件泄露导致的通杀漏洞
安全·web安全·小程序·黑客入门·渗透测试实战
游戏开发爱好者81 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息1 天前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”1 天前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
换日线°2 天前
NFC标签打开微信小程序
前端·微信小程序
光影少年2 天前
AIGC + Taro / 小程序
小程序·aigc·taro