微信小程序 左右滑动块,自定义的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()
        },


  }
相关推荐
wcy_10115 分钟前
七大软件设计原则
java·设计规范
Mapmost6 分钟前
【高斯泼溅】如何将“歪头”的3DGS模型精准“钉”在地图上,杜绝后续误差?
前端
invicinble6 分钟前
jar包在执行的时候需要关注的细节(提供一个解构jvm问题的视角)
java·jvm·jar
JellyDDD7 分钟前
h5上传大文件可能会导致手机浏览器卡死,重新刷新的问题
javascript·上传文件
麦芽糖02198 分钟前
SSE介绍及使用(Server-Send Events)
java
alan072112 分钟前
【Java + Elasticsearch全量 & 增量同步实战】
java·elasticsearch·jenkins
hashiqimiya25 分钟前
后端springboot的接收前端发来的数据反序列化原理
java
废春啊38 分钟前
前端工程化
运维·服务器·前端
爱上妖精的尾巴39 分钟前
6-9 WPS JS宏Map、 set、get、delete、clear()映射的添加、修改、删除
前端·wps·js宏·jsa
爱分享的鱼鱼1 小时前
对比理解 Vue 响应式 API:data(), ref、reactive、computed 与 watch 详解
前端·vue.js