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

三开关VUE组件

一、使用效果

请添加图片描述请添加图片描述

请添加图片描述

<template><QqThreeSwitch v-model="value" /><!-- <SqThreeSwitch v-model="value" :options="['test1', 'test2', 'test3']"><template #left-action><div style="display: flex"><IconMoon /></div></template><template #middle-action><div style="display: flex"><IconSunny /></div></template><template #right-action><div style="display: flex"><IconSystem /></div></template></SqThreeSwitch> -->
</template><script setup>
import SqThreeSwitch from './components/SqThreeSwitch.vue'
import { ref } from 'vue'const value = ref(0)
</script>

二、SqThreeSwitch.vue源码

<template><div class="sq-three-switch"><button class="focus-btn" :style="focusBtnStyle" @click="handleBtnClick">按下空格切换主题, 当前选择:{{ selectedOption }}</button><div v-show="isMouseEnter" class="tooltip" tabindex="-1" :style="tooltipStyle"><div class="tip-text">{{ tooltipText }}</div><svg class="tip-arrow" width="16px" height="8px" :style="tipArrowStyle"><polygon points="0,-1 8,7 16,-1" /></svg></div><div ref="selectedOptionRef" class="selected-option"><span>{{ selectedOption }}</span></div><divref="controlRef"class="control plane-border"@click="handleClick"@mouseenter="handleMouseEnter"@mouseleave="handleMouseLeave"@mousemove="debouncedHandleMouseMove"></div><div class="plane"></div><div class="badge-dots"><divv-for="(dot, index) in [0, 1, 2]":key="index"class="dot":class="{ 'dot-animate': dotAnimateFlag }"@animationend="handleAnimationEnd"></div></div><div class="handle" :style="handleStyle"><slot v-if="modelValue === 0" name="left-action"></slot><slot v-if="modelValue === 1" name="middle-action"></slot><slot v-if="modelValue === 2" name="right-action"></slot></div></div>
</template><script setup>
import { ref, watch, computed, nextTick, onMounted, onBeforeUnmount } from 'vue'
import { useDebounceFn } from '@vueuse/core'const props = defineProps({modelValue: {type: Number,default: 0},options: {type: Array,default: () => ['选项A', '选项B', '选项C']}
})
const emit = defineEmits(['update:modelValue'])const selectedOptionRef = ref(null)const focusBtnStyle = ref({})
nextTick(() => {focusBtnStyle.value = {width: `${selectedOptionRef.value.getBoundingClientRect().width + 50}px`}
})
watch(() => props.modelValue,() => {nextTick(() => {focusBtnStyle.value = {width: `${selectedOptionRef.value.getBoundingClientRect().width + 50}px`}})}
)const controlRef = ref(null)
const haveTooltipSpace = ref(false)
const tipArrowStyle = computed(() => {return {transform: haveTooltipSpace.value ? '' : 'translateY(-26px) rotate(180deg)'}
})
function checkTooltipSpace(deadline) {if (deadline.timeRemaining() > 0) {const rect = controlRef.value?.getBoundingClientRect()if (rect) {haveTooltipSpace.value = rect.top >= 20}}
}
const debouncedCheckTooltipSpace = useDebounceFn(() => requestIdleCallback(checkTooltipSpace, { timeout: 200 }),200
)
let intervalId
onMounted(() => {debouncedCheckTooltipSpace()window.addEventListener('scroll', debouncedCheckTooltipSpace)window.addEventListener('resize', debouncedCheckTooltipSpace)intervalId = setInterval(debouncedCheckTooltipSpace, 2000)console.log('作者主页: https://blog.csdn.net/qq_39124701')
})
onBeforeUnmount(() => {window.removeEventListener('scroll', debouncedCheckTooltipSpace)window.removeEventListener('resize', debouncedCheckTooltipSpace)if (intervalId !== null) {clearInterval(intervalId)}
})const isMouseEnter = ref(false)
const tooltipText = ref(props.options[props.modelValue])
const tooltipStyle = ref({left: props.modelValue === 0 ? '0px' : props.modelValue === 1 ? '20px' : '40px',top: haveTooltipSpace.value ? '0px' : '54px'
})const selectedOption = computed(() => {return props.options[props.modelValue]
})const dotAnimateFlag = ref(false)const handleStyle = ref({left:props.modelValue === 0? '2px': props.modelValue === 1? 'calc(50% - 9px)': 'calc(100% - 19px)'
})
watch(() => props.modelValue,(newValue) => {handleStyle.value = {left: newValue === 0 ? '2px' : newValue === 1 ? 'calc(50% - 9px)' : 'calc(100% - 19px)'}}
)function handleClick(/** @type { MouseEvent } */ event) {/** @type { Element } */const eventTarget = event.targetconst rect = eventTarget.getBoundingClientRect()const clickX = event.clientX - rect.leftconst oneThirdWidth = rect.width / 3if (clickX < oneThirdWidth) {if (props.modelValue === 0) {dotAnimateFlag.value = true}emit('update:modelValue', 0)} else if (clickX > oneThirdWidth * 2) {if (props.modelValue === 2) {dotAnimateFlag.value = true}emit('update:modelValue', 2)} else {if (props.modelValue === 1) {dotAnimateFlag.value = true}emit('update:modelValue', 1)}
}
function handleBtnClick() {if (props.modelValue === 0) {emit('update:modelValue', 1)} else if (props.modelValue === 1) {emit('update:modelValue', 2)} else if (props.modelValue === 2) {emit('update:modelValue', 0)}
}
function handleMouseEnter() {isMouseEnter.value = true
}
function handleMouseLeave() {isMouseEnter.value = false
}
const debouncedHandleMouseMove = useDebounceFn(handleMouseMove, 40)
function handleMouseMove(event) {if (!isMouseEnter.value) {return}const rect = event.target.getBoundingClientRect()const clickX = event.clientX - rect.leftconst oneThirdWidth = rect.width / 3if (clickX < oneThirdWidth) {tooltipText.value = props.options[0]tooltipStyle.value = { left: '0px', top: haveTooltipSpace.value ? '0px' : '54px' }} else if (clickX > oneThirdWidth * 2) {tooltipText.value = props.options[2]tooltipStyle.value = { left: 'calc(100% - 21px)', top: haveTooltipSpace.value ? '0px' : '54px' }} else {tooltipText.value = props.options[1]tooltipStyle.value = { left: 'calc(50% - 11px)', top: haveTooltipSpace.value ? '0px' : '54px' }}
}function handleAnimationEnd() {dotAnimateFlag.value = false
}
</script><style scoped>
.sq-three-switch {position: relative;width: 60px;height: 20px;/* scale: 5;transform-origin: 0% 0%;z-index: 1; */
}
.sq-three-switch > * {position: absolute;
}
.sq-three-switch > .plane,
.sq-three-switch > .badge-dots,
.sq-three-switch > .handle {pointer-events: none;
}
.sq-three-switch > .focus-btn {height: 100%;border-radius: 10px;border: 0;outline-offset: 1px;font-size: 0;
}
.sq-three-switch > .focus-btn:focus {outline: 2px solid #409eff;
}
.sq-three-switch > .tooltip {z-index: 1;transform: translateY(-27px);white-space: nowrap;background-color: #e6e6e6;border: 1px solid gray;border-radius: 4px;padding: 1px 11px;transition: left 0.2s;
}
.sq-three-switch > .tooltip > .tip-text {font-size: 12px;color: black;
}
.sq-three-switch > .tooltip > .tip-arrow {position: absolute;top: 18px;left: 1px;
}
.sq-three-switch > .tooltip > .tip-arrow polygon {fill: #e6e6e6;stroke: gray;stroke-width: 1;
}
.sq-three-switch > .selected-option {height: 100%;background: linear-gradient(to right, #a8d4ff, #409eff 16px);border-radius: 10px;border-top-left-radius: 0;border-bottom-left-radius: 0;transform: translateX(50px);display: flex;justify-content: center;font-size: 14px;color: white;white-space: nowrap;
}
.sq-three-switch > .selected-option > span {padding-left: 16px;padding-right: 10px;user-select: none;
}
.sq-three-switch > .control {width: 100%;height: 20px;border-radius: 10px;background: #409eff;cursor: pointer;
}
.sq-three-switch > .plane {top: 1px;left: 1px;width: calc(100% - 2px);height: 18px;border-radius: 10px;background: #409eff;
}
.sq-three-switch > .badge-dots > .dot {position: absolute;top: 8px;left: 8px;width: 4px;height: 4px;border-radius: 100%;transition: all 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);background-color: white;
}
.sq-three-switch > .badge-dots > .dot:nth-child(2) {left: 27px;
}
.sq-three-switch > .badge-dots > .dot:nth-child(3) {left: 47px;
}
.dot-animate {animation: dotAnimation 0.3s;
}
@keyframes dotAnimation {0% {background-color: white;}25% {background-color: black;}50% {background-color: white;}75% {background-color: black;}100% {background-color: white;}
}
.sq-three-switch > .handle {top: 2px;left: 2px;width: 16px;height: 16px;border-radius: 100%;transition: all 0.3s cubic-bezier(0.22, 0.61, 0.36, 1);background-color: white;
}html.dark .sq-three-switch > .tooltip {background-color: #303133;
}
html.dark .sq-three-switch > .tooltip > .tip-arrow polygon {fill: #303133;
}
html.dark .sq-three-switch > .tooltip > .tip-text {color: white;
}
</style>

相关文章:

三开关VUE组件

一、使用效果 <template><QqThreeSwitch v-model"value" /><!-- <SqThreeSwitch v-model"value" :options"[test1, test2, test3]"><template #left-action><div style"display: flex"><IconMoon…...

SpringCloud+SpringCloudAlibaba学习笔记

SpringCloud 服务注册中心 eureka ap 高可用 分布式容错 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency><groupId…...

牛客小白月赛105(A~E)

文章目录 A lz的吃饭问题思路code B lz的数字问题思路code C lz的蛋挞问题思路code D lz的染色问题思路code E lz的括号问题思路code 总结 牛客小白月赛105 A lz的吃饭问题 思路 签到题&#xff0c;比较大小即可 code void solve(){int a,b,c,d;cin >> a >> b…...

OSPF协议整理

OSPF&#xff08;Open Shortest Path First&#xff09;即开放式最短路径优先协议&#xff0c;是一种广泛应用于大型网络中的链路状态路由协议。 OSPF 的基本概念 OSPF 是基于链路状态算法的内部网关协议&#xff08;IGP&#xff09;&#xff0c;用于在一个自治系统&#xff…...

Java中的多线程

文章目录 Java中的多线程一、引言二、多线程的创建和启动1、继承Thread类2、实现Runnable接口 三、线程的常用方法1、currentThread()和getName()2、sleep()和yield()3、join() 四、线程优先级五、使用示例六、总结 Java中的多线程 一、引言 在Java中&#xff0c;多线程编程是…...

什么是聚簇索引、非聚簇索引、回表查询

其实聚集索引也叫聚簇索引&#xff0c;二级索引也叫非聚簇索引&#xff0c;大家不要认为这是不同的两个知识点。 定义 先看一下数据库的索引介绍。 聚簇索引 1. 如果存在主键&#xff08;一般都存在&#xff09;&#xff0c;主键索引就是聚簇索引。 2. 如果不存在&#xff0c;…...

探索 Spring 框架核心组件:构建强大 Java 应用的基石

Spring框架作为Java企业级开发的首选框架之一&#xff0c;其强大的功能和灵活的架构深受开发者喜爱。Spring框架的核心组件共同构建了一个高效、可扩展的应用程序开发平台。本文将深入探讨Spring框架的核心组件&#xff0c;揭示它们如何在Spring框架中发挥关键作用。 一、Bean…...

Android 13 Aosp 默认允许应用动态权限

图库 frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java 修改 public void grantDefaultPermissions(int userId) {DelayingPackageManagerCache pm new DelayingPackageManagerCache();grantPermissionsToSysCompon…...

【C++知识总结1】c++第一篇,简单了解一下命名空间是什么

一、C的由来 C语言是一种结构化和模块化的编程语言&#xff0c;它对于处理较小规模的程序非常适用。然而&#xff0c;当面临需要高度抽象和建模的复杂问题&#xff0c;以及规模较大的程序时&#xff0c;C语言就显得不那么合适了。为了应对这种挑战&#xff0c;并在解决软件危机…...

从0开始深度学习(32)——循环神经网络的从零开始实现

本章将从零开始&#xff0c;基于循环神经网络实现字符级语言模型&#xff08;不是单词级&#xff09; 首先我们把从0开始深度学习&#xff08;30&#xff09;——语言模型和数据集中的load_corpus_time_machine()函数进行引用&#xff0c;用于导入数据&#xff1a; train_iter…...

GitLab使用操作v1.0

1.前置条件 Gitlab 项目地址&#xff1a;http://******/req Gitlab账户信息&#xff1a;例如 001/******自己的分支名称&#xff1a;例如 001-master&#xff08;注&#xff1a;master只有项目创建者有权限更新&#xff0c;我们只能更新自己分支&#xff0c;然后创建合并请求&…...

cuda conda yolov11 环境搭建

优雅的 yolo v11 标注工具 AutoLabel Conda环境直接识别训练 nvidia-smi 检查CUDA版本 下载nvidia cudnn对应的版本 将cuDNN压缩包内对应的文件复制到本地bin、include、lib的文件夹中 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6 miniConda快速开始-安装 执行…...

解决SpringBoot连接Websocket报:请求路径 404 No static resource websocket.

问题发现 最近在工作中用到了WebSocket进行前后端的消息通信&#xff0c;后端代码编写完后&#xff0c;测试一下是否连接成功&#xff0c;发现报No static resource websocket.&#xff0c;看这个错貌似将接口变成了静态资源来访问了&#xff0c;第一时间觉得是端点没有注册成…...

element-plus的组件数据配置化封装 - table

目录 一、封装的table、table-column组件以及相关ts类型的定义 1、ATable组件的封装 - index.ts 2、ATableColumn组件的封装 - ATableColumn.ts 3、ATable、ATableColumn类型 - interface.ts 二、ATable、ATableColumn组件的使用 三、相关属性、方法的使用以及相关说明 1. C…...

【二维动态规划:交错字符串】

介绍 编程语言&#xff1a;Java 本篇介绍一道比较经典的二维动态规划题。 交错字符串 主要说明几点&#xff1a; 为什么双指针解不了&#xff1f;为什么是二维动态规划&#xff1f;根据题意分析处转移方程。严格位置依赖和空间压缩优化。 题目介绍 题意有点抽象&#xff0c…...

goframe开发一个企业网站 MongoDB 完整工具包18

1. MongoDB 工具包完整实现 (mongodb.go) package mongodbimport ("context""fmt""time""github.com/gogf/gf/v2/frame/g""go.mongodb.org/mongo-driver/mongo""go.mongodb.org/mongo-driver/mongo/options" )va…...

在vue中,根据后端接口返回的文件流实现word文件弹窗预览

需求 弹窗预览word文件&#xff0c;因浏览器无法直接根据blob路径直接预览word文件&#xff0c;所以需要利用插件实现。 解决方案 利用docx-preview实现word文件弹窗预览&#xff0c;以node版本16.21.3和docx-preview版本0.1.8为例 具体实现步骤 1、安装docx-preview插件 …...

动态规划之背包问题

0/1背包问题 1.二维数组解法 题目描述&#xff1a;有一个容量为m的背包&#xff0c;还有n个物品&#xff0c;他们的重量分别为w1、w2、w3.....wn&#xff0c;他们的价值分别为v1、v2、v3......vn。每个物品只能使用一次&#xff0c;求可以放进背包物品的最大价值。 输入样例…...

【Python】 深入理解Python的单元测试:用unittest和pytest进行测试驱动开发

《Python OpenCV从菜鸟到高手》带你进入图像处理与计算机视觉的大门! 单元测试是现代软件开发中的重要组成部分,通过验证代码的功能性、准确性和稳定性,提升代码质量和开发效率。本文章深入介绍Python中两种主流单元测试框架:unittest和pytest,并结合测试驱动开发(TDD)…...

Java集合1.0

1.什么是集合&#xff1f; 集合就是一个存放数据的容器&#xff0c;准确的说是放数据对象引用的容器。 集合和数组的区别 数组是固定长度&#xff0c;集合是可变长度。数组可以存储基本数据类型&#xff0c;也可以存储引用数据类型&#xff0c;集合只能存储引用数据类型&…...

深入浅出Asp.Net Core MVC应用开发系列-AspNetCore中的日志记录

ASP.NET Core 是一个跨平台的开源框架&#xff0c;用于在 Windows、macOS 或 Linux 上生成基于云的新式 Web 应用。 ASP.NET Core 中的日志记录 .NET 通过 ILogger API 支持高性能结构化日志记录&#xff0c;以帮助监视应用程序行为和诊断问题。 可以通过配置不同的记录提供程…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

visual studio 2022更改主题为深色

visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中&#xff0c;选择 环境 -> 常规 &#xff0c;将其中的颜色主题改成深色 点击确定&#xff0c;更改完成...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

今日科技热点速览

&#x1f525; 今日科技热点速览 &#x1f3ae; 任天堂Switch 2 正式发售 任天堂新一代游戏主机 Switch 2 今日正式上线发售&#xff0c;主打更强图形性能与沉浸式体验&#xff0c;支持多模态交互&#xff0c;受到全球玩家热捧 。 &#x1f916; 人工智能持续突破 DeepSeek-R1&…...

Spring数据访问模块设计

前面我们已经完成了IoC和web模块的设计&#xff0c;聪明的码友立马就知道了&#xff0c;该到数据访问模块了&#xff0c;要不就这俩玩个6啊&#xff0c;查库势在必行&#xff0c;至此&#xff0c;它来了。 一、核心设计理念 1、痛点在哪 应用离不开数据&#xff08;数据库、No…...

JVM 内存结构 详解

内存结构 运行时数据区&#xff1a; Java虚拟机在运行Java程序过程中管理的内存区域。 程序计数器&#xff1a; ​ 线程私有&#xff0c;程序控制流的指示器&#xff0c;分支、循环、跳转、异常处理、线程恢复等基础功能都依赖这个计数器完成。 ​ 每个线程都有一个程序计数…...

人机融合智能 | “人智交互”跨学科新领域

本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...

MySQL 知识小结(一)

一、my.cnf配置详解 我们知道安装MySQL有两种方式来安装咱们的MySQL数据库&#xff0c;分别是二进制安装编译数据库或者使用三方yum来进行安装,第三方yum的安装相对于二进制压缩包的安装更快捷&#xff0c;但是文件存放起来数据比较冗余&#xff0c;用二进制能够更好管理咱们M…...