当前位置: 首页 > news >正文

uni-app 中 swiper 轮播图高度自适应

方法一

1、首先 swiper 标签的宽度是 width: 100%

2、swiper 标签存在默认高度是 height: 150px ;高度无法实现由内容撑开,在默认情况下,图片的高度显示总是 150px

swiper 宽度 / swiper 高度 = 原图宽度 / 原图高度

swiper 高度 = swiper 宽度 * 原图高度 / 原图宽度

<swiper class="swiper-box" indicator-dots autoplay circular><swiper-item v-for="(item, i) in imgList" :key="i"><image style="width: 100%" :src="item" mode="widthFix" /></swiper-item>
</swiper>
.swiper-box {width: 100%;height: calc(100vw * 9 / 16);
}

方法二

1、在每次滑动切换的时候,动态地获取 swiper-item 内部的 DOM 的元素的高度

2、将获取的高度动态设置给 swiper 元素

<swiper:current="currIndex"@change="changeSwiper":style="{ height: swiperHeight + 'px' }"indicator-dotsautoplaycircular:duration="1000"
><swiper-item v-for="(item, i) in imgList" :key="i"><image :id="'wrap' + i" style="width: 100%" :src="item" mode="widthFix" /></swiper-item>
</swiper>
currIndex: 0, // 当前索引
swiperHeight: 0, // 滑块的高度(单位px)
onLoad(e) {this.$nextTick(() => {this.setSwiperHeight(); // 动态设置 swiper 的高度});
},
/* 切换 swiper 滑块 */
changeSwiper(e) {this.currIndex = e.detail.current;this.$nextTick(() => {this.setSwiperHeight(); // 动态设置 swiper 的高度});
},
/* 动态设置 swiper 的高度 */
setSwiperHeight() {const element = "#wrap" + this.currIndex;const query = uni.createSelectorQuery().in(this);query.select(element).boundingClientRect();query.exec(res => {if (res && res[0]) this.swiperHeight = res[0].height;});
},
http://www.lryc.cn/news/153960.html

相关文章:

  • 开源风雷CFD软件多物理场耦合接口开发路线分享!!!
  • 浅谈Mysql读写分离的坑以及应对的方案 | 京东云技术团队
  • 最近在对接电商供应链,说说开放平台API接口
  • 【FusionInsight 迁移】HBase从C50迁移到6.5.1(02)C50上准备FTP Server
  • Java操作符学习笔记
  • 【STM32】学习笔记-PWR(Power Control)电源控制
  • 安卓 MeasureCache优化了什么?
  • docker save docker export 区别
  • 音频基础知识
  • TensorFlow(R与Python系列第四篇)
  • 华为数通方向HCIP-DataCom H12-821题库(单选题:261-280)
  • 论文《基于概率标签估计的半监督日志缺陷检测》翻译
  • ajax day2
  • 互联网摸鱼日报(2023-09-04)
  • UG\NX CAM二次开发 遍历组中的工序 UF_NCGROUP_ask_member_list
  • 适配器、装饰器模式
  • Netty服务端启动的整体流程-基于源码4.1.96Final分析
  • 预训练Bert添加new token的问题
  • 非常典型和高效的枚举类写法
  • kafka-- kafka集群环境搭建
  • 3.flask-sqlalchemy ORM库
  • mac 安装 homebrew
  • R语言应用interactionR包进行亚组相加交互作用分析
  • mysql 数据库面试题整理
  • LeetCode-435-无重叠区间
  • 记录深度学习常用指令(一)
  • Shell脚本练习——系统应用相关
  • 同创永益入选首批“金融数字韧性与混沌工程实践试点机构”
  • Hive 表注释乱码解决
  • 【搭建私人图床】使用LightPicture开源搭建图片管理系统并远程访问