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>
相关推荐
fanruitian1 分钟前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo4 分钟前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk8 分钟前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
2501_944525542 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
李白你好2 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
刘一说3 小时前
Vue 组件不必要的重新渲染问题解析:为什么子组件总在“无故”刷新?
前端·javascript·vue.js
徐同保4 小时前
React useRef 完全指南:在异步回调中访问最新的 props/state引言
前端·javascript·react.js
fanruitian4 小时前
uniapp 创建项目
javascript·vue.js·uni-app
刘一说5 小时前
Vue 导航守卫未生效问题解析:为什么路由守卫不执行或逻辑失效?
前端·javascript·vue.js
一周七喜h5 小时前
在Vue3和TypeScripts中使用pinia
前端·javascript·vue.js