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


  }
相关推荐
谦行1 分钟前
前端视角 Java Web 入门手册 5.10:真实世界 Web 开发—— 单元测试
java·spring boot·后端
成长ing121381 分钟前
cocos creator 3.x shader 流光
前端·cocos creator
Alo36510 分钟前
antd 组件部分API使用方法
前端
hhua012312 分钟前
理解“无界队列”与“有界队列”及其适用场景
java·队列
BillKu12 分钟前
Vue3数组去重方法总结
前端·javascript·vue.js
GDAL15 分钟前
Object.freeze() 深度解析:不可变性的实现与实战指南
javascript·freeze
LZQqqqqo16 分钟前
C# 接口(interface 定义接口的关键字)
java·开发语言·c#
寒水馨23 分钟前
Java 9 新特性解析
java·开发语言·新特性·java9·jdk9
SimonKing29 分钟前
甩掉手动赋值!MyBatis-Plus 自动填充实战秘籍
java·后端·程序员
江城开朗的豌豆34 分钟前
Vue+JSX真香现场:告别模板语法,解锁新姿势!
前端·javascript·vue.js