#swiper的自适应高度问题

众所周知,swiper组件的元素swiper-item是设置了绝对定位的,所以里面的内容是无法撑开swiper的,并且给swiper盒子设置overflow:visible也是没有用的,有几种解决方法,根据不同的需求使用。

  1. 给swiper-item里的内容加scaoll-view包装;
  2. 通过uniapp api,直接选取有实际内容的DOM,并获取到他的高度,动态设置swiper的高度

1.方法一

html部分

<view class="nav">...</view>
<swiper class="swiper">
<swiper-item>
<scroll-view scroll-y="true" class="scroll">
<!-- 这里是内容 -->
</scroll-view>
</swiper-item>
</swiper>

css部分

.nav{
height:400px;
}
.swiper{
height: calc(100vh - 400rpx);
}
.scroll{
height: 100%;
}

2.方法二

html部分

<swiper class="swiper" :style="{height:swiperHeight + 'px'}" :current="current" @change="changeCurrent">
<swiper-item class="swiper1">
<!-- 这里是内容 -->
</swiper-item>>
<swiper-item class="swiper2">
<!-- 这里是内容 -->
</swiper-item>>
<swiper-item class="swiper3">
<!-- 这里是内容 -->
</swiper-item>>
</swiper>

js部分

export default {
data() {
return {
swiperHeight:0, //外部的高度
current:0
}
},
onLoad() {
this.getElementHeight('.swiper' + this.current)
},
methods:{
//点击tab切换
changeCurrent(index) {
this.current = index;
this.getElementHeight('.swiper' + this.current)
},
//动态获取高度
getElementHeight(element) {
//一定要 this.$nextTick 完成之后在获取dom节点高度
this.$nextTick(()=>{
let query = uni.createSelectorQuery().in(this);
query.select(element).boundingClientRect(data => {
console.log(data.height);
this.swiperHeight = data.height;
}).exec()
})
}
}
}

扩展:

html部分

<view class="main">
<!-- ...... --> <!-- 其他属性省略,动态绑定width -->
<u-charts :width="width" />
</view>

css部分

.main{
margin: 28rpx 14rpx 0 14rpx;
}

js部分

export default {
data() {
return {
width:0
}
},
onLoad() {
this.getElementWidth('.main')
},
methods:{
//动态获取高度
getElementWidth(element) {
//一定要 this.$nextTick 完成之后在获取dom节点高度
this.$nextTick(()=>{
let query = uni.createSelectorQuery().in(this);
query.select(element).boundingClientRect(data => {
console.log(data.width);
this.width = data.width;
}).exec()
})
}
}
}

最新文章

  1. ios label上显示特殊字符 % &quot;
  2. WordPress博客系统搜索引擎优化seo全攻略方法
  3. sencha touch之store
  4. javascript 学习笔记(权威指南)
  5. 具体解释VMware 9.0.1安装MAC OS X 10.8(历时近3日感想篇)
  6. 搭建php环境的几种方法
  7. UITabBarController及三种控制器的…
  8. .NET Core使用微软官方类库实现汉字转拼音
  9. mongodb 数据导出
  10. 【分享】【原创开源应用第4期】给ili9488,RA8875类显示屏的emWin底层增加DMA加速方案
  11. 测序数据质控-FastQC
  12. P5205 【模板】多项式开根
  13. 题解——Codeforces Round #507 (based on Olympiad of Metropolises) T2(模拟)
  14. 《DSP using MATLAB》Problem 2.9
  15. 动态规划:双重DP
  16. Wide-range regulator delivers 12V, 3A output from 16 to 100V source
  17. private、protected、public和internal的区别
  18. Docker运行GUI软件的方法
  19. MySQL常用命令(三)---最值的搜索
  20. 【ZJOI2017 Round1练习】D2T3 counter(线段树)

热门文章

  1. 温故知新-java的I/O模型-BIO&NIO&AIO
  2. go mod 与单元测试
  3. Centos 文件系统基础命令
  4. Spring WebFlux 学习笔记 - (一) 前传:学习Java 8 Stream Api (2) - Stream的中间操作
  5. SpringBoot 中注解方式的拦截过滤
  6. Bash知识点记录
  7. (五)POI-设置单元格的对齐方式
  8. WebDriverWait
  9. C#数据结构与算法系列(五):常见单链表笔试
  10. 研华advantech-凌华ADLINK板卡运动控制卡