微信小程序 左右滑动块,自定义的switch组件,带文字状态的开关

复制代码
<template>
  <view class="pageMain">
    <view class="mp-switch" style="--mp-switch-width: {{width}}px;--mp-switch-height: {{height}}px;--mp-switch-text: '{{checked?trueText:falseText}}';">
	<view class="wx-switch-input {{checked?'wx-switch-input-checked':''}}" bind:tap="onChange"></view>
</view>
 </view>
</template>

<style lang="less">
.mp-switch .wx-switch-input {
	-webkit-appearance: none;
	appearance: none;
	position: relative;
	width: var(--mp-switch-width);
	height: var(--mp-switch-height);
	border: 1px solid #DFDFDF;
	outline: 0;
	border-radius: calc(var(--mp-switch-height) / 2);
	box-sizing: border-box;
	background-color: #e5e5e5;
	transition: background-color 0.1s, border 0.1s;
}

.mp-switch .wx-switch-input::before {
	content: var(--mp-switch-text);
	position: absolute;
	top: 0;
	left: 0;
	width: calc(var(--mp-switch-width) - 2px);
	height: calc(var(--mp-switch-height) - 2px);
	line-height: calc(var(--mp-switch-height) - 2px);
	font-size: calc(var(--mp-switch-height) / 2.2);
	padding-left: calc(var(--mp-switch-height));
	padding-right: 6px;
	box-sizing: border-box;
	overflow: hidden;
	color: #999;
	text-align: center;
	background-color: transparent;
}

.mp-switch .wx-switch-input::after {
	content: " ";
	position: absolute;
	top: 0;
	left: 0;
	width: calc(var(--mp-switch-height) - 2px);
	height: calc(var(--mp-switch-height) - 2px);
	border-radius: 50%;
	background-color: #FFFFFF;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
	transition: -webkit-transform 0.3s;
	transition: transform 0.3s;
}

.mp-switch .wx-switch-input.wx-switch-input-checked {
	border-color: #07C160;
	background-color: #07C160;
}

.mp-switch .wx-switch-input.wx-switch-input-checked::before {
	color: #fff;
	padding-right: calc(var(--mp-switch-height));
	padding-left: 6px;
}

.mp-switch .wx-switch-input.wx-switch-input-checked::after {
	-webkit-transform: translateX(calc(var(--mp-switch-width) - var(--mp-switch-height)));
	transform: translateX(calc(var(--mp-switch-width) - var(--mp-switch-height)));
}
</style>

  data = {
 
    trueText: '开',
    falseText: '关',
    checked:false,
      width:72,
      height: 32,
  }

methods = {
     onChange() {
      this.checked= !this.checked
     this.$apply()
        },


  }
相关推荐
curdcv_po1 分钟前
前端CSS高频面试题详解
前端
珹洺4 分钟前
数据库系统概论(十七)超详细讲解数据库规范化与五大范式(从函数依赖到多值依赖,再到五大范式,附带例题,表格,知识图谱对比带你一步步掌握)
java·数据库·sql·安全·oracle
Danta4 分钟前
从0开始学习three.js(1)😁
前端·javascript·three.js
我的心巴5 分钟前
vue-print-nb 打印相关问题
前端·vue.js·elementui
用户79117724235836 分钟前
黑马点评【基于redis实现共享session登录】
java·redis
网安INF10 分钟前
CVE-2023-25194源码分析与漏洞复现(Kafka JNDI注入)
java·web安全·网络安全·kafka·漏洞·jndi注入
程序员岳焱16 分钟前
Java 集合高级操作:反射实现多属性去重与分组
java·后端·编程语言
Dcs19 分钟前
CMS、G1、ZGC、Shenandoah 的全面对比
java
coderYYY24 分钟前
element树结构el-tree,默认选中当前setCurrentKey无效
前端·javascript·vue.js
GISer_Jing34 分钟前
Three.js中AR实现详解并详细介绍基于图像标记模式AR生成的详细步骤
开发语言·javascript·ar