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


  }
相关推荐
研☆香8 分钟前
js中使用的正则表达式
开发语言·javascript·正则表达式
szarron9 分钟前
国产手持式频谱分析仪选型攻略:TFN RC系列 vs HTOOL SA8T频谱分析仪 专业参数对比(军工/路测/调试全覆盖)
开发语言·前端·状态模式
开开心心就好16 分钟前
免费桌签打印工具,支持批量导入名字
前端·javascript·人工智能·docker·jupyter·智能手机·语音识别
涛涛ing17 分钟前
2026 年 9 月,整个 npm 生态的「心脏」都被 Rust 换了
前端
Rain的Java大神之路18 分钟前
手写一个Spring容器
java·后端·mysql·spring·面试
小七在进步20 分钟前
C++入门(1)
java·jvm·c++
IT_陈寒22 分钟前
明明设了默认值,为什么我的JavaScript函数参数还是undefined?
前端·人工智能·后端
CappuccinoRose22 分钟前
Web Components 基础
前端·javascript·交互·web component·shadow dom
晴天1624 分钟前
前端 Monorepo 入门分享:从概念到实践
前端
Wang's Blog27 分钟前
Java框架快速入门: Spring Security+OAuth2之定制登录页
java·开发语言·spring