使用uniapp开发h5网页 适配手机 和 PC端

uni-app是以移动为先的理念诞生的,所以我们先开发出适配手机浏览器的网页,再来适配PC端。

第一,目前我先开发移动端官方网站

第二,由于Pc端布局 和 移动的布局结构不同,我直接根据uniapp提供的match-media组件,动态根据媒体查询条件显示对应组件(该情况适用于html结构不同)。

	<!-- 主体内容 -->
	<match-media :min-width="990">
		<PcMainContentVue />
	</match-media>

	<match-media :min-width="0" :max-width="990">
		<PhoneMainContentVue />
	</match-media>

第三,细微调整样式,我是根据uni.createMediaQueryObserver,监测是否符合条件,符合的一个样式,不符合的一个样式(该情况适用于html结构一样࿰

uniapp中,实现swiper自适应内容高度的方法与在普通网页中类似。不过需要注意的是,uniapp中的Swiper组件是基于第三方Swiper库封装的,需要使用相应的API来操作。 以下是实现swiper自适应内容高度的具体步骤: 1. 在Swiper初始化的时候,通过uni.createSelectorQuery()方法获取Swiper容器所有的slide元素。 ```javascript onLoad() { uni.createSelectorQuery().in(this).select('.swiper-container').fields({ size: true }, this.onContainerSizeReady).exec(); }, onContainerSizeReady(res) { var swiperContainerWidth = res.width; uni.createSelectorQuery().in(this).selectAll('.swiper-slide').boundingClientRect(this.onSlidesSizeReady).exec(); }, ``` 2. 遍历所有的slide元素,计算出最高的slide高度。 ```javascript onSlidesSizeReady(res) { var slidesHeight = res.map(function (item) { return item.height; }); var maxHeight = Math.max.apply(null, slidesHeight); this.swiperHeight = maxHeight; }, ``` 3. 在Swiper组件中,使用swiperHeight属性设置Swiper容器的高度为最高slide的高度。 ```html <swiper class="swiper-container" :style="'height:' + swiperHeight + 'px;'"> <swiper-item class="swiper-slide" v-for="(item, index) in items" :key="index">{{ item.content }}</swiper-item> </swiper> ``` 完整的实现代码如下: ```javascript export default { data() { return { items: [ { content: 'Slide 1' }, { content: 'Slide 2' }, { content: 'Slide 3' }, { content: 'Slide 4' }, { content: 'Slide 5' }, ], swiperHeight: 0 } }, onLoad() { uni.createSelectorQuery().in(this).select('.swiper-container').fields({ size: true }, this.onContainerSizeReady).exec(); }, onContainerSizeReady(res) { var swiperContainerWidth = res.width; uni.createSelectorQuery().in(this).selectAll('.swiper-slide').boundingClientRect(this.onSlidesSizeReady).exec(); }, onSlidesSizeReady(res) { var slidesHeight = res.map(function (item) { return item.height; }); var maxHeight = Math.max.apply(null, slidesHeight); this.swiperHeight = maxHeight; } } ``` 需要注意的是,uniapp中的Swiper组件还提供了其他的API,如autoplay、loop、pagination等,可以根据需要进行设置。同时,uniapp中的Swiper组件也支持嵌套使用,可以实现更为复杂的滑动效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@穷且益坚,不坠青云之志

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

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

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

打赏作者

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

抵扣说明:

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

余额充值