uniapp 简易滑块切换,简单易改

html 复制代码
<template>
	<view class="my-tab w-100 flex-center">
		<view class="tab-without">
			<view class="switch-tab w-100" :style="{height:height,borderRadius:borderRadius,border:borderBox,width:width,backgroundColor:mainBg}">
				<block v-for="(item,index) in tabList" :key="index">
					<view class="h-100 one flex-center font-weight-550" @click="tabClick(index)">{{item}}
					</view>
				</block>
			</view>
			<view class="zg-view flex-center"
				:style="{left:tabLeft,borderRadius:radius,backgroundColor:bgColor,color:textColor,height:height,width:themeWidth}">
				{{tabList[tabIndex]}}
			</view>
		</view>
	</view>
</template>
javascript 复制代码
<script>
	export default {
		props: {
			/* 主体的所需数据的宽度*/
			width: {
				type: String,
				default: "572rpx"
			},
			/* 主体的所需数据的高度*/
			height: {
				type: String,
				default: "52rpx"
			},
			/* 主体的所需数据的圆角*/
			borderRadius: {
				type: String,
				default: "26rpx"
			},
			/* 主体的所需数据的边框*/
			borderBox: {
				type: String,
				default: '2rpx solid #333333'
			},
			/* 主体的所需数据的背景颜色*/
			mainBg: {
				type: String,
				default: '#fff'
			},
			/* 切换的数据 */
			tabList: {
				type: Array,
				default: () => {
					return ['本月数据', '季度数据', '半年数据', '本年数据']
				}
			},
			/* 滑块的背景色 */
			bgColor: {
				type: String,
				default: "#333"
			},
			/* 滑块的文字颜色*/
			textColor: {
				type: String,
				default: "#fff"
			},
			
			
		},
		computed: {
			/* 计算滑块的宽度占比 */
			themeWidth() {
				return 100 / this.tabList.length + '%'
			},
			/* 滑块移动的距离 */
			tabLeft() {
				return (100 / this.tabList.length) * this.tabIndex + '%'
			},
			/* 滑块的圆角 */
			radius() {
				let text = ''
				if (this.tabIndex == 0 && this.borderRadius != '') {
					text = `${this.borderRadius} 0 0 ${this.borderRadius}`
				}
				if (this.tabList.length - 1 == this.tabIndex && this.borderRadius != '') {
					text = `0 ${this.borderRadius}  ${this.borderRadius} 0`
				}
				return text
			}
		},
		data() {
			return {
				tabIndex: 0
			}
		},
		methods: {
			/* 切换方法,同时向外暴露当前点击的按钮 */
			tabClick(index) {
				this.tabIndex = index
				this.$emit('tabClick', index)
			}
		},
	}
</script>
css 复制代码
<style lang="scss" scoped>
	.flex-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.h-100 {
		height: 100%;
	}

	.one {
		flex: 1;
	}

	.w-100 {
		width: 100%;
	}

	/* 字体权重 */
	.font-weight-500 {
		font-weight: 500;
	}

	.font-weight-550 {
		font-weight: 550;
	}

	.font-weight-600 {
		font-weight: 600;
	}

	.font-weight-700 {
		font-weight: 700;
	}

	.font-weight-bold {
		font-weight: bold;
	}

	.my-tab {
		font-size: 24rpx;

		.tab-without {
			position: relative;

			.switch-tab {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.zg-view {
				position: absolute;
				top: 0;
				left: 0;
				transition: all .3s;
			}
		}

	}
</style>
相关推荐
liusheng几秒前
Capacitor + React 的 iOS 侧滑返回手势
前端·ios
CUYG12 分钟前
v-model封装组件(定义 model 属性)
前端·vue.js
子洋14 分钟前
基于远程开发的大型前端项目实践
运维·前端·后端
用户350201588474818 分钟前
基于react-routet v7 的配置式 + 约定式路由系统 第一步:引入react-routerv7
前端
用户350201588474820 分钟前
基于react-routet v7 的配置式 + 约定式路由系统 第二步:一个简单的约定式路由系统
前端
攀登的牵牛花24 分钟前
前端向架构突围系列 - 框架设计(七):反应式编程框架Flower的设计
前端·架构
佛系打工仔26 分钟前
K线绘制前言
前端
2501_915918411 小时前
除了 Perfdog,如何在 Windows 环境中完成 iOS App 的性能测试工作
android·ios·小程序·https·uni-app·iphone·webview
遇见~未来1 小时前
JavaScript数组全解析:从本质到高级技巧
开发语言·前端·javascript
石像鬼₧魂石1 小时前
80 端口(Web 服务)渗透测试完整总结(含踩坑 + 绕过 + 实战流程)
linux·运维·服务器·前端·网络·阿里云