uniapp中的swiper高度自适应,图片显示问题

uniapp中的swiper高度自适应,图片显示问题

swiper部分

<template>
	<view style="width:100%;">
		<swiper :style="{height:windowHeight+'px'}" :indicator-dots="true" :autoplay="true" :interval="2000" :duration="500"
		 :circular="true" :current="tabIndex" @click="changeTab">
			<swiper-item style="width: 100%;" v-for="item in imgs" :key="item.id">
				<image :src="item.url" mode="widthFix" class="img"></image>
			</swiper-item>
		</swiper>
	</view>
</template>

js求出可使用高度

onLoad(option) {
			this.systemInfo = uni.getSystemInfoSync()
			this.windowHeight = this.systemInfo.windowHeight;
			console.log(this.windowHeight);
		},

css中的图片要设置成块,可以填充100%

	.img {
		width: 100%;
		height: auto;
	}

完整代码

<template>
	<view style="width:100%;">
		<swiper :style="{height:windowHeight+'px'}" :indicator-dots="true" :autoplay="true" :interval="2000" :duration="500"
		 :circular="true" :current="tabIndex" @click="changeTab">
			<swiper-item style="width: 100%;" v-for="item in imgs" :key="item.id">
				<image :src="item.url" mode="widthFix" class="img"></image>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgs: [{
						url: '../../static/init-bg.png',
						id: 1
					}, {
						url: '../../static/init-bg.png',
						id: 2
					},
					{
						url: '../../static/init-bg.png',
						id: 3
					}, {
						url: '../../static/init-bg.png',
						id: 4
					},
					{
						url: '../../static/init-bg.png',
						id: 5
					}
				],
				height: '',
				tabIndex:0,
				systemInfo: {},
				swiperHeight: '',
				windowHeight: '',
			}
		},
		onLoad(option) {
			this.systemInfo = uni.getSystemInfoSync()
			this.windowHeight = this.systemInfo.windowHeight;
			console.log(this.windowHeight);
		},
		methods: {
			changeTab() {
				this.tabIndex++;
				if (this.tabIndex > this.imgs.length - 1) {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
		}
	}
</script>

<style>
	.img {
		width: 100%;
		height: auto;
	}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值