tab栏转换可滑动功能,解决swiper高度问题,使swiper高度自适应。

出现的问题:swiper高度默认150px,swiper-item高度是100%,起初想让swiper-item中的内容吧swiper-item和其父元素高度撑开,理应将height设置成auto但是swiper的高度auto可以设置成功,但是swiper-item高度设置了auto始终是不成功(开发者工具中styles中显示出了但是就是不生效),

项目组件库使用的是u-view2.0,因为删除了u-view1.0中的tabswiper所以采取以下做法来实现

 

<view>
	<swiper style="height: {{anchor.deviceHeight + 'px'}}" bindchange="swiperChange">
		<swiper-item>
			<view class="anchor-top"></view>
			<!-- 你的内容 -->
			<view class="anchor-bottom"></view>
		</swiper-item>
		<swiper-item>
			<view class="anchor-top"></view>
			<!-- 你的内容 -->
			<view class="anchor-bottom"></view>
		</swiper-item>
	</swiper>
	<view class="anchor-screen-bottom"></view>
</view>
data(){
returm{
    anchor: {//处理swiper高度问题
					deviceHeight: 0,
					anchorTop: 0,
					anchorBottom: 0,
					anchorScreenBottom: 0
				}
}
}


method:{
computeSwiperHeight(pageIndex) {
				let getSwiperHeight = () => {
					let min = this.anchor.anchorScreenBottom - this.anchor.anchorTop;
					let value = this.anchor.anchorBottom - this.anchor.anchorTop
					return Math.max(min, value)
				}
				wx.createSelectorQuery()
					.select('.anchor-screen-bottom')
					.boundingClientRect()
					.selectViewport()
					.scrollOffset()
					.exec(res => {
						this.anchor.anchorScreenBottom = res[0].bottom
					})
				wx.createSelectorQuery()
					.selectAll('.anchor-top')
					.boundingClientRect()
					.selectViewport()
					.scrollOffset()
					.exec(res => {
						this.anchor.anchorTop = res[0][pageIndex].top
						this.anchor.deviceHeight = getSwiperHeight()
					})
				wx.createSelectorQuery()
					.selectAll('.anchor-bottom')
					.boundingClientRect()
					.selectViewport()
					.scrollOffset()
					.exec(res => {
						this.anchor.anchorBottom = res[0][pageIndex].bottom
						this.anchor.deviceHeight = getSwiperHeight()
					})
			},

transition(e) {
				this.currentNum = e.detail.current
				console.log(e.detail.current, '-------')
				this.computeSwiperHeight(e.detail.current)
			},
    
}
	.anchor-top {
		width: 0;
		height: 0;
	}

	.anchor-bottom {
		width: 0;
		height: 0;
	}

	.anchor-screen-bottom {
		position: absolute;
		bottom: 0;
		width: 0;
		height: 0;
	}

原理:给 swiper-item 内部添加三个锚点,最上面一个,最下面一个,还有一个锚点始终位于屏幕最底下。根据这三个锚点计算出内容高度和内容显示区高度。 PS:锚点,宽高为 0 的不可见的 view,用于获取定位

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柑橘乌云_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值