uni——点击同一个按钮,赋予不同状态(来回切换)

案例演示:

代码:

事件源:

<view @click="open(showValue)">点击调起弹框</view>
<!-- 弹框 -->
<u-popup :show="isShow" @close="close" @open="open()" round="30rpx"></u-popup>
data() {
	return {
			isShow: false,
			// 0不显示 1显示
			showValue: 0
		}
},
method:{
	open(e) {
		if (e == 0) {
			// 如果当前不显示,那么让他显示,提前把值改掉  下一次再点的时候再进行判断
			this.isShow = true
			this.showValue = 1
		}
		if (e == 1) {
			this.isShow = false
			this.showValue = 0
		}
	},
	
	close() {
		this.isShow = false
		this.showValue = 0
	},
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过一个按钮切换两种全局样式,您可以使用uni-app提供的全局变量和计算属性来实现。下面是一个示例: 1. 在App.vue文件中定义全局变量`theme`,用来记录当前的主题,以及两种主题的样式: ```html <template> <div :class="theme"> <!-- 页面内容 --> <router-view></router-view> </div> </template> <script> export default { name: 'App', data() { return { theme: 'light' // 默认主题为light } }, computed: { themeStyles() { if (this.theme === 'light') { return { backgroundColor: '#fff', color: '#333' } } else { return { backgroundColor: '#333', color: '#fff' } } } } } </script> <style> /* 全局CSS样式 */ .light button { background-color: #ccc; color: #333; } .dark button { background-color: #333; color: #ccc; } </style> ``` 在上面的示例中,我们定义了一个`theme`变量,用来记录当前的主题。当`theme`为`light`时,应用`light`主题的样式,当`theme`为`dark`时,应用`dark`主题的样式。同时,我们还定义了一个计算属性`themeStyles`,用来返回当前主题的样式。 2. 在需要切换主题的页面中添加一个按钮,并通过点击事件来切换主题: ```html <template> <div> <!-- 页面内容 --> <button @click="toggleTheme">切换主题</button> </div> </template> <script> export default { name: 'Page', methods: { toggleTheme() { if (this.$root.theme === 'light') { this.$root.theme = 'dark' } else { this.$root.theme = 'light' } } } } </script> <style> /* 页面CSS样式 */ button { border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; } </style> ``` 在上面的示例中,我们添加了一个按钮,并通过点击事件来切换主题。在点击事件中,我们通过访问`this.$root.theme`来获取和修改全局变量`theme`的值。当`theme`的值为`light`时,切换为`dark`主题,当`theme`的值为`dark`时,切换为`light`主题。 通过以上步骤,您就可以通过一个按钮切换两种不同的全局样式了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值