当前位置: 首页 > article >正文

小程序开关组件

前提是vant组件库不太好改,干脆就手写了一个

<template>
    <view class="switch-container" @click="toggleOrder">
        <view :class="['switch-text', 'left-text', { 'left-textChoose': isReverseOrder }]"
            >{{ leftText }}</view
        >
        <view class="switch-slider" :style="sliderStyle"></view>
        <view :class="['switch-text', 'right-text', { 'right-textChoose': !isReverseOrder }]"
            >{{ rightText }}</view
        >
    </view>
</template>

<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
    leftText: {
        type: String,
        default: ''
    },
    rightText: {
        type: String,
        default: ''
    }
});
const isReverseOrder = ref(true);
const emit = defineEmits(['getValue']);
// 计算滑块的位置
const sliderStyle = computed(() => {
    // 滑块完全在左侧时偏移量为0%,完全在右侧时偏移量为100%
    return {
        transform: `translateX(${isReverseOrder.value ? '100%' : '0%'})`
    };
});

// 切换开关状态
function toggleOrder() {
    isReverseOrder.value = !isReverseOrder.value;
    console.log(isReverseOrder.value);
    emit('getValue', isReverseOrder.value);
}
</script>

<style scoped>
.switch-container {
    border: 2px solid #eeeeee;
    display: flex;
    align-items: center;
    width: 72px; 
    height: 28px; 
    background-color: #eeeeee; 
    border-radius: 20px; 
    overflow: hidden;
    position: relative; // 滑块绝对定位
}

.switch-text {
    flex: 1; //使文本元素占据剩余空间的一半
    display: flex;
    justify-content: center; 
    align-items: center;
    user-select: none; 
    position: relative; //为了可能的z-index调整
    z-index: 2; // 确保文本在滑块之上
}

.left-text {
    font-family: PingFangSC-Medium;
    font-weight: 500;
    font-size: 10px;
    color: #25292f;
}
.left-textChoose {
    font-family: PingFang SC;
    font-weight: 400;
    font-size: 10px;
    color: #25292f;
}

.right-text {
    font-family: PingFangSC-Medium;
    font-weight: 500;
    font-size: 10px;
    color: #25292f;
}
.right-textChoose {
    font-family: PingFang SC;
    font-weight: 400;
    font-size: 10px;
    color: #25292f;
}
.switch-slider {
    width: 50%; // 滑块宽度为容器宽度的一半
    height: 24px;
    background-color: #fff; 
    transition: transform 0.3s ease-in-out; // 过渡效果 
    border-radius: 20px; 
    position: absolute; // 绝对定位以覆盖整个容器 
    left: 0; //初始位置
    top: 0;
    bottom: 0;
    z-index: 1; // 确保滑块在文本之下
}
</style>

效果如图
在这里插入图片描述


http://www.kler.cn/news/308909.html

相关文章:

  • ArrayList的扩容机制
  • Spring 源码解读:实现@Scope与自定义作用域
  • 前端开发第三节课
  • 解决使用阿里云DataV Geo在线地图路径访问403问题
  • 深入解析JSON:数据交换的通用语言
  • Spring Boot-国际化(I18N)问题
  • 嵌入式Linux笔试题目
  • 【JavaWeb】利用IDEA2024+tomcat10配置web6.0版本搭建JavaWeb开发项目
  • Encountered error while trying to install package.> lxml
  • es6中set和map的区别
  • C++速通LeetCode简单第17题-爬楼梯
  • PostgreSQL维护——解决索引膨胀和数据死行
  • 运维的基本概念:服务器和网络基础知识
  • 瑞星微RK芯片的Buildroot构建系统镜像
  • 【Gateway】Gateway Filter Factories
  • Visual Studio 2019/2022 IntelliCode(AI辅助IntelliSense)功能介绍
  • 【SpringBoot】调度和执行定时任务--Spring Task(超详细)
  • 数据结构 - 树与二叉树
  • [强化你的LangChain工具创建技能:从基础到进阶]
  • C语言 | Leetcode C语言题解之第413题等差数列划分
  • c语言题目猜凶手问题
  • Vue2中父子组件通信双向绑定
  • 【Java】【力扣】83.删除排序链表中的重复元素
  • TensorRT-LLM——优化大型语言模型推理以实现最大性能的综合指南
  • react18基础教程系列-- 框架基础理论知识mvc/jsx/createRoot
  • 预训练蛋白质语言模型ESM-2保姆级使用教程
  • C++设计模式(更新中)
  • 数据结构:(OJ141)环形列表
  • 李宏毅2023机器学习HW15-Few-shot Classification
  • 部分动态铜皮的孤岛无法删除。报错