uniapp点击切换,动态添加类名

<template>
	<view class="main">
		
		<view class="team_info session ">
			<view class="tit">
				选择预约场次
			</view>
			<view class="week">
				<u-row gutter="10">
					<u-col span="2" v-for="(item,index) in timeData" :key="index">
						<view class="flex_row flex_center item " :class="initTimeIndex==index?'checked':''"
							@click="checkDate(index)">
							<view class="we">
								{{dateConver(item)}}
							</view>
							<view class="date">
								{{item.slice(5)}}
							</view>
						</view>
					</u-col>

				</u-row>
			</view>
			</view>
	
		
	</view>
</template>

<script>
	export default {

		data() {
			return {
				selectedIndex: 2,
				tabbarList: '',
				numval: 1,
				type: 1, //1:团队,2:专业人员
				timeData: [],
				initTimeIndex: 0
			}
		},
		onLoad() {
			this.tabbarList = uni.getStorageSync('tabbarList')
			this.getAppointmentTime()
		},

		onShow() {
			uni.hideTabBar({
				success: () => {
					//⭐隐藏pages.json里配置的导航栏,使用封装的tabbar组件
					uni.hideTabBar({
						animation: false
					})
				},
				fail: () => {
					// 捕获报错,防止没有tabbar页面调用后控制台报错
				}
			});
		},
		methods: {
			getAppointmentTime() {
				this.$api.getAppointmentTime().then(res => {
					if (res.code == 0) {
						let data = res.data

						this.timeData = data
					}
					console.log(res, this.timeData, '可預約時間')
				})
			},
			// 日期格式转换 2023-12-25转为周一
			dateConver(day) {
				const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
				const date = new Date(day)
				this.dayOfWeek = weekdays[date.getDay()]
				return this.dayOfWeek
			},
			// 选中 日期
			checkDate(ind) {
				this.initTimeIndex = ind
			}
		}
	}
</script>

<style lang="scss">
	.main {
		height: 100%;
		width: 100%;
		background-color: #f6f6f6;
		padding: 30rpx;
		box-sizing: border-box;
		padding-bottom: calc(var(--window-bottom) + 50px);
	}

	

	.session {
		margin-top: 30rpx;
		padding: 30rpx 20rpx;

		.week,
		.time {

			padding-top: 70rpx;
			overflow: scroll;

			.item {
				background-color: #fff;
				border: 1px dashed #d2d2d2;
				color: #666;
				font-size: 24rpx;
				height: 90rpx;
				margin-bottom: 30rpx;

			}

			.checked {
				background: linear-gradient(90deg, #29B183 0%, #286EB3 100%);
				color: #fff;
			}

			.outweek {
				background-color: #ededed;
			}
		}

		.date_it {
			flex-wrap: wrap;
		}
	}

	.regulaion {
		margin-top: 30rpx;
		padding: 30rpx 20rpx;
		background-color: #fff;

		.re_tip {
			height: 100%;

			img {
				width: 36rpx;
				height: 36rpx;
			}
		}

		.regu_info {
			background-color: #fff;
			border: 1px dashed #FA6319FF;
			color: #FA6319FF;
			padding: 30rpx 30rpx;
			display: flex;

			.info {
				margin-left: 15rpx;
			}
		}


	}

	.submit {
		margin: 40rpx 0;
	}
</style>

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在uniapp中,你可以通过以下方法来实现点击添加样式和清空样式的功能: 1. 在data中定义一个变量,用于绑定需要添加样式的元素的class属性,例如: ``` data() { return { isActive: false } } ``` 2. 在需要添加样式的元素上绑定class属性,然后使用三元表达式来判断是否添加样式,例如: ``` <view class="item {{isActive ? 'active' : ''}}" @click="toggleActive"></view> ``` 3. 在toggleActive方法中,通过修改isActive变量的值来控制样式的添加和清空,例如: ``` methods: { toggleActive() { this.isActive = !this.isActive; } } ``` 这样,当你点击该元素时,它的class属性会动态添加或清空active样式,从而实现点击添加样式和清空样式的功能。 ### 回答2: 在uniapp中,我们可以通过点击事件来添加样式和清空样式。 1. 首先,在模板中定义一个点击事件,例如: ``` <view class="container" @click="addStyle">点击添加样式</view> <view class="container" @click="clearStyle">点击清空样式</view> ``` 2. 在data中定义样式的变量,例如: ``` data() { return { isAdded: false } }, ``` 3. 在methods中分别定义添加样式和清空样式的函数: ``` methods: { addStyle() { this.isAdded = true; }, clearStyle() { this.isAdded = false; } } ``` 4. 在样式中使用条件判断来添加或清空样式,例如: ``` <style> .container { width: 200px; height: 200px; background-color: #ccc; } .added { color: red; } .clear { color: black; } </style> ``` 5. 在模板中通过条件判断来动态添加或清空样式,例如: ``` <view class="container" :class="{'added': isAdded, 'clear': !isAdded}">点击添加/清空样式</view> ``` 通过以上步骤,当我们点击 "点击添加样式" 按钮时,会给该元素添加样式 'added',文字颜色将变为红色;当我们点击 "点击清空样式" 按钮时,会清空该元素的样式,文字颜色将恢复为黑色。 ### 回答3: 在UniApp中,可以通过点击事件来添加样式或清空样式。 首先,需要在模板中定义一个标志位,用于控制样式的添加和清空。这个标志位可以是一个布尔类型的变量,比如isClicked,初始值为false。 接下来,在点击事件的处理函数中,可以通过修改isClicked的值来切换样式的状态。比如,当isClicked为false时,表示未点击状态,点击修改isClicked为true,表示已点击状态。 在模板中,可以使用条件渲染来根据isClicked的值来添加或清空样式。比如,可以通过v-bind:class添加或移除一个特定的类名,用于控制样式。 示例代码如下: ``` <template> <view> <button @click="handleClick">点击按钮</button> <view :class="{'clicked-style': isClicked}"> 点击添加样式 </view> </view> </template> <script> export default { data() { return { isClicked: false } }, methods: { handleClick() { this.isClicked = true // 修改标志位为true,表示点击了按钮 } } } </script> <style> .clicked-style { background-color: red; // 添加样式 } </style> ``` 以上代码中,定义了一个按钮,在点击按钮后会触发handleClick方法,将isClicked的值修改为true。同时,也定义了一个view组件,在isClicked为true时,会添加clicked-style样式,显示为红色背景;而在isClicked为false时,不添加任何样式,显示为默认样式。 通过这种方式,可以在UniApp中通过点击事件来添加样式或清空样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值