Swiper滑块视图容器的用法

Swiper滑块视图容器

滑块容器视图(Swiper)和滚动容器视图(Scroll-View)的区别,滚动容器仅有一个内容区域,可自由滚动选择要显示的内容;滑块容器有多个区域,Swiper下的每个swiper-item是一个滑块切换区域,只能一屏一屏的切换,不能停留在2个滑块区域之间。

滑块视图容器(Swiper)

Swiper是父元素,内包含多个子元素Swiper-item,先写上一个Swiper,并设置样式,代码如下:

<template>
	<view>
		<swiper>
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
	}
</style>

效果如下:
在这里插入图片描述
在这里,可以加上CSS选择器:swiper-item:nth-child(2n),选择2倍数页,并为其设置珊瑚色背景,代码如下:

<template>
	<view>
		<swiper>
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

显示面板指示点(indicator-dots)

有了指示点,可以很清楚的知道区域的总数,也可以直接点击指示点快速切换到不同区域,添加指示点属性,代码如下:

<template>
	<view>
		<swiper indicator-dots>
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

指示点颜色(indicator-color)、当前选中指示点颜色(indicator-active-color)

这里的指示点,默认是灰色,被选中后为黑色,继续添加属性,尝试改变指示点的颜色,代码如下:

<template>
	<view>
		<swiper indicator-dots indicator-color ="#126bae" indicator-active-color = "#ee3f4d">
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

衔接滑动(circular)

即滑动到末尾后是否返回到开头,默认是不能返回到开头的,加入衔接滑动属性,代码如下:

<template>
	<view>
		<swiper indicator-dots indicator-color ="#126bae" indicator-active-color = "#ee3f4d" circular>
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

自动切换(autoplay)、自动切换时间间隔(interval)

默认自动切换时间为5000毫秒,有点慢,直接设置2000毫秒试试,代码如下:

<template>
	<view>
		<swiper indicator-dots indicator-color ="#126bae" indicator-active-color = "#ee3f4d" circular autoplay interval = "2000">
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

纵向滑动(vertical)

代码如下:

<template>
	<view>
		<swiper indicator-dots indicator-color ="#126bae" indicator-active-color = "#ee3f4d" circular autoplay interval = "2000" vertical>
			<swiper-item>我是第一区域</swiper-item>
			<swiper-item >我是第二区域</swiper-item>
			<swiper-item>我是第三区域</swiper-item>
			<swiper-item> 我是第四区域</swiper-item>
		</swiper>
	</view>
</template>

<script	setup>
	
</script>

<style lang="scss">
	swiper{
		width:100vw ;
		height: 200px;
		border: 1px solid green;
		swiper-item{
			width: 100%;
			height:100%;
			background: pink;
		}
		swiper-item:nth-child(2n){
			background: coral;
		}
	}
</style>

效果如下:
在这里插入图片描述

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值