iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >uniapp组件之tab选项卡滑动切换功能实现
  • 293
分享到

uniapp组件之tab选项卡滑动切换功能实现

摘要

目录uniapp组件之tab选项卡滑动切换补充:uniapp实现tabs切换(可滑动)uniapp组件之tab选项卡滑动切换   效果如下:   代码如下:&n

uniapp组件之tab选项卡滑动切换

  效果如下:

  代码如下: 

<template>
	<view class="content">
		<view class="nav">
			<!-- 选项卡水平方向滑动,scroll-with-animation是滑动到下一个选项时,有一个延时效果 -->
			<scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
				<view class="tab-scroll_box">
					<!-- 选项卡类别列表 -->
					<view class="tab-scroll_item" v-for=" (item,index) in cateGory" :key="index"  :class="{'active':isActive==index}" @click="chenked(index)">
							{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 选项卡内容轮播滑动显示,current为当前第几个swiper子项 -->
		<swiper @change="change" :current="isActive" class="swiper-content" :style="fullHeight">
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡1页面
					</view>
				</scroll-view>	
			</swiper-item>
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡2页面
					</view>
				</scroll-view>	
			</swiper-item>
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡3页面
					</view>
				</scroll-view>
			</swiper-item>
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡4页面
					</view>
				</scroll-view>	
			</swiper-item>
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡5页面
				    </view>
				</scroll-view>	
			</swiper-item>
			<swiper-item class="swiperitem-content">
				<scroll-view scroll-y style="height: 100%;">
					<view class="nav_item" >
						选项卡6页面
				    </view>
				</scroll-view>	
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	export default {
		watch:{
			// swiper与上面选项卡联动
			currentindex(newval){
				this.isActive = newval;
				this.scrollLeft = 0;
				// 滑动swiper后,每个选项距离其父元素最左侧的距离
				for (let i = 0; i < newval - 1; i++) {
					this.scrollLeft += this.category[i].width
				};
			},
		},
		data() {	
			return {
				isActive: 0,
				index: 0,
				currentindex:0,
				category:[
					{
						id:1,
						name:'选项卡一',
					},
					{
						id:2,
						name:'选项卡二',
					},
					{
						id:3,
						name:'选项卡三',
					},
					{
						id:4,
						name:'选项卡四',
					},
					{
						id:5,
						name:'选项卡五',
					},
					{
						id:6,
						name:'选项卡六',
					},
				],
				contentScrollW: 0, // 导航区宽度
				scrollLeft: 0, // 横向滚动条位置
				fullHeight:"",	
			}
		},
		mounted() {
			var that = this;
			 //获取手机屏幕的高度,让其等于swiper的高度,从而使屏幕充满
			uni.getSystemInfo({
			      success: function (res) {
				         that.fullHeight ="height:" + res.windowHeight + "px";
				  }
				});
			// 获取标题区域宽度,和每个子元素节点的宽度
			this.getScrollW()
		},
		methods: {
			// 获取标题区域宽度,和每个子元素节点的宽度以及元素距离左边栏的距离
			getScrollW() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.tab-scroll').boundinGClientRect(data => {
					  // 拿到 scroll-view 组件宽度
					  this.contentScrollW = data.width
				 }).exec();
				 query.selectAll('.tab-scroll_item').boundingClientRect(data => {
					 let dataLen = data.length;
					  for (let i = 0; i < dataLen; i++) {
						  //  scroll-view 子元素组件距离左边栏的距离
						  this.category[i].left = data[i].left;
						 //  scroll-view 子元素组件宽度
						 this.category[i].width = data[i].width
					}
				 }).exec()
			},
			// 当前点击子元素靠左留一个选项展示,子元素宽度不相同也可实现
			chenked(index) {
				this.isActive = index;
				this.scrollLeft = 0;
				for (let i = 0; i < index - 1; i++) {
					this.scrollLeft += this.category[i].width
				};
			},
			// swiper滑动时,获取其索引,也就是第几个
			change(e){
				const {current} = e.detail;
				this.currentindex = current;
			},	
		}
	}
</script>
<style lang="sCSS">
	page{
		height: 100%;
		display: flex;
		background-color: #FFFFFF;
	}
	.content{
		display: flex;
		flex-direction: column;
		width: 100%;
		flex: 1;
		.nav{
			border-top: 1rpx solid #f2f2f2;
			background-color: #fceeee;	
			position: fixed;
			z-index: 99;
			width: 100%;
			align-items: center;
			height: 100rpx;
			.tab-scroll{
				flex: 1;
				overflow: hidden;
				box-sizing: border-box;
				padding-left: 30rpx;
				padding-right: 30rpx;
				.tab-scroll_box{
					display: flex;
					align-items: center;
					flex-wrap: nowrap;
					box-sizing: border-box;
					.tab-scroll_item{
						line-height: 60rpx;
						margin-right: 35rpx;
						flex-shrink: 0;
						padding-bottom:10px;
						display: flex;
						justify-content: center;
						font-size: 16px;
						padding-top: 10px;
					}
				}
			}
		}
		.swiper-content{
			   padding-top: 120rpx;
			   flex: 1;
			.swiperitem-content{
			    background-color: #ffffff;
				.nav_item{	
					background-color: #FFFFFF;
					padding:20rpx 40rpx 0rpx 40rpx ;
				}
			}
		}	
	}
	.active {
		position: relative;
		color: #ff0000;
		font-weight: 600;
	}
	.active::after {
		content: "";
		position: absolute;
		width: 130rpx;
		height: 4rpx;
		background-color: #ff0000;
		left: 0px;
		right: 0px;
		bottom: 0px;
		margin: auto;
	}
	
	/deep/.uni-scroll-view::-WEBkit-scrollbar {
		display: none
	}
</style>

注意:css当中需要加上以下,为了隐藏滚动条,否则会出现下图效果


 
/deep/.uni-scroll-view::-webkit-scrollbar {
 
display: none
 
}

补充:uniapp实现tabs切换(可滑动)

uniapp实现tabs切换(可滑动)

在这里插入图片描述

<template>
	<view class="body-view">
		<!-- 使用scroll-view实现tabs滑动切换 -->
		<scroll-view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
			<view class="menu-topic-view" v-for="item in tabs" :id="'tabNum'+item.id" :key="(item.id - 1)" @click="swichMenu(item.id - 1)">
				<view :class="currentTab==(item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
					<text class="menu-topic-text">{{item.name}}</text>
					<view class="menu-topic-bottom">
						<view class="menu-topic-bottom-color"></view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 内容 -->
		<swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
			<swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
				<view class="swiper-item">
					{{item.content}}
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [{
						id: 1,
						name: '推荐'
					},
					{
						id: 2,
						name: '交通交通'
					},
					{
						id: 3,
						name: '住房'
					},
					{
						id: 4,
						name: '社会保障'
					},
					{
						id: 5,
						name: '民生热点'
					},
					{
						id: 6,
						name: '即日头条'
					},
					{
						id: 7,
						name: '新闻联播'
					},
				],
				currentTab: 0,
				tabCurrent: 'tabNum1',
				// Tab切换内容
				swiperDateList: [{
						id: 1,
						content: '推荐'
					},
					{
						id: 2,
						content: '交通交通'
					},
					{
						id: 3,
						content: '住房'
					},
					{
						id: 4,
						content: '社会保障'
					},
					{
						id: 5,
						content: '民生热点'
					},
					{
						id: 6,
						content: '即日头条'
					},
					{
						id: 7,
						content: '新闻联播'
					},
				],
			}
		},
		onLoad() {

		},
		methods: {
			swichMenu(id) {
				this.currentTab = id
				console.log(11,id)
				this.tabCurrent = 'tabNum'+ id
			},
			swiperChange(e) {
				console.log(22,e.detail.current)
				let index = e.detail.current
				this.swichMenu(index)
			}
		}
	}
</script>

<style scoped lang="scss">
	.body-view {
		height: 100vh;
		width: 100%;
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		align-items: flex-start;
		justify-content: center;
	}

	.top-menu-view {
		display: flex;
		position: fixed;
		top: 100rpx;
		left: 0;
		white-space: nowrap;
		width: 100%;
		background-color: #FFFFFF;
		height: 86rpx;
		line-height: 86rpx;
		border-top: 1rpx solid #d8dbe6;

		.menu-topic-view {
			display: inline-block;
			white-space: nowrap;
			height: 86rpx;
			position: relative;

			.menu-topic-text {
				font-size: 30rpx;
				color: #303133;
				padding: 10rpx 40rpx;
			}

			// .menu-topic-act {
			// 	margin-left: 30upx;
			// 	margin-right: 10upx;
			// 	position: relative;
			// 	height: 100%;
			// 	display: flex;
			// 	align-items: center;
			// 	justify-content: center;
			// }

			.menu-topic-bottom {
				position: absolute;
				bottom: 0;
				width: 100%;

				.menu-topic-bottom-color {
					width: 40rpx;
					height: 4rpx;
				}
			}

			.menu-topic-act .menu-topic-bottom {
				display: flex;
				justify-content: center;
			}

			.menu-topic-act .menu-topic-bottom-color {
				background: #3D7eff;
			}



		}


	}

	.swiper-box-list {
		width: 100%;
		padding-top: 200rpx;
		flex:1;
		background-color: #FFFFFF;
		.swiper-topic-list {
		     width: 100%;
		 }
	}
</style>

继续加油呀~

到此这篇关于uniapp组件之tab选项卡滑动切换的文章就介绍到这了,更多相关uniapp tab选项卡滑动切换内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: uniapp组件之tab选项卡滑动切换功能实现

本文链接: https://www.lsjlt.com/news/193767.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • uniapp组件之tab选项卡滑动切换功能实现
    目录uniapp组件之tab选项卡滑动切换补充:uniapp实现tabs切换(可滑动)uniapp组件之tab选项卡滑动切换   效果如下:   代码如下:&n...
    99+
    2023-01-31
    uniapp tab选项卡滑动切换 uniapp滑动切换 uniapp tab切换 uniapp tab选项卡
  • CSS3中怎么实现tab选项卡切换功能
    本篇文章给大家分享的是有关CSS3中怎么实现tab选项卡切换功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。CSS3伪类target利用ta...
    99+
    2024-04-02
  • jQuery实现滑动tab选项卡
    本文实例为大家分享了jQuery实现滑动tab选项卡的具体代码,供大家参考,具体内容如下 先上最终效果: 需求分析: 1.选项卡菜单数量不固定,菜单内容不固定,导致了单个菜单和整体...
    99+
    2024-04-02
  • Vue实现Tab选项卡切换
    本文实例为大家分享了Vue实现Tab选项卡切换的具体代码,供大家参考,具体内容如下 点击不同的标题显示出相应的图片 代码如下 <!DOCTYPE html> <...
    99+
    2024-04-02
  • vue中如何实现选项卡点击切换且能滑动切换功能
    这篇文章主要介绍vue中如何实现选项卡点击切换且能滑动切换功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体代码如下所述: <div>  &nbs...
    99+
    2024-04-02
  • AngularJS如何实现标签页tab选项卡切换功能
    这篇文章将为大家详细讲解有关AngularJS如何实现标签页tab选项卡切换功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体如下:选项卡一:JavaScript+h...
    99+
    2024-04-02
  • Vue实现选项卡tab切换制作
    本文实例为大家分享了Vue实现选项卡tab切换制作代码,供大家参考,具体内容如下 1.主要的实现功能如下:(点击进行切换,这里我不做太多的样式处理了,主要看功能) 2.话不多说:主...
    99+
    2024-04-02
  • Css怎么实现tab选项卡切换
    这篇文章主要介绍了Css怎么实现tab选项卡切换,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Css实现tab选项卡切换的方法:利用target的特性,可以实现纯css的ta...
    99+
    2023-06-14
  • Vue.js中tab怎么实现选项卡切换
    这篇文章主要介绍Vue.js中tab怎么实现选项卡切换,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下<!DOCTYPE html> <html...
    99+
    2024-04-02
  • vue动态组件如何实现选项卡切换效果
    这篇文章主要介绍了vue动态组件如何实现选项卡切换效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下导航按钮:<div&n...
    99+
    2024-04-02
  • 如何使用CSS3实现选项卡切换功能
    这篇文章主要讲解了“如何使用CSS3实现选项卡切换功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用CSS3实现选项卡切换功能”吧!:target是...
    99+
    2024-04-02
  • 小程序怎么实现tab卡片切换功能
    这篇文章主要介绍小程序怎么实现tab卡片切换功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、UI与交互首先我们来看看要实现的ui模样和交互效果吧,下图是我们的一个入口,以下的每...
    99+
    2024-04-02
  • jquery+swiper组件实现时间轴滑动年份tab切换效果
    实现效果: 实现代码:需要配合swiper组件使用 Swiper基础演示地址: https://www.swiper.com.cn/demo/index.html HTML: ...
    99+
    2024-04-02
  • 怎么用vue动态组件实现选项卡切换效果
    这篇文章主要讲解了“怎么用vue动态组件实现选项卡切换效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用vue动态组件实现选项卡切换效果”吧!导航按钮:<div cla...
    99+
    2023-07-04
  • vue怎么使用动态组件实现选项卡切换效果
    本文小编为大家详细介绍“vue怎么使用动态组件实现选项卡切换效果”,内容详细,步骤清晰,细节处理妥当,希望这篇“vue怎么使用动态组件实现选项卡切换效果”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。导航按钮:&l...
    99+
    2023-07-04
  • jquery+swiper组件如何实现时间轴滑动年份tab切换效果
    这篇文章主要讲解了“jquery+swiper组件如何实现时间轴滑动年份tab切换效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“jquery+swiper组件如何实现时间轴滑动年份tab...
    99+
    2023-06-21
  • vue中如何使用动态组件实现选项卡切换效果
    这篇文章主要为大家展示了“vue中如何使用动态组件实现选项卡切换效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue中如何使用动态组件实现选项卡切换效果”这...
    99+
    2024-04-02
  • 微信小程序实现tab组件切换动画
    目录前言如何实现页面tab1.使用内置组件scroll-view2.实现点击时出现的背景样式3.scroll-into-view前言 本次主要内容是介绍页面tab的开发,如何实现ta...
    99+
    2022-11-13
    小程序tab组件切换 微信小程序tab组件 小程序tab切换
  • vue使用动态组件实现TAB切换效果
    目录问题描述 什么是vue的动态组件 应用场景描述 实现步骤 第一步(新建组件并引入注册) 第二步(布局,上面放tab点击的标签,下面放组件呈现对应内容)第三步(写好上面的tab点击...
    99+
    2024-04-02
  • 微信小程序如何自定义可滑动顶部TabBar选项卡实现页面切换功能
    这篇文章主要介绍微信小程序如何自定义可滑动顶部TabBar选项卡实现页面切换功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体如下:顶部滚动选项卡话不多说,直接上代码pages/...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作