Vue3气泡卡片(Popover)

news/2024/4/27 5:51:18/文章来源:https://blog.csdn.net/Dandrose/article/details/137084676

效果如下图:在线预览

在这里插入图片描述

APIs

参数说明类型默认值必传
title卡片标题string | slot‘’false
content卡片内容string | slot‘’false
maxWidth卡片内容最大宽度string | number‘auto’false
trigger卡片触发方式‘hover’ | ‘click’‘hover’false
overlayStyle卡片样式CSSProperties{}false

Events

事件名称说明参数
openChange显示隐藏的回调(visible: boolean) => void

注:组件引用方法 import { rafTimeout, cancelRaf } from ‘…/index’ 请参考该博客

创建气泡卡片组件Popover.vue

<script setup lang="ts">
import { ref, computed, useSlots } from 'vue'
import type { Slot, CSSProperties } from 'vue'
import { rafTimeout, cancelRaf } from '../index'
interface Props {title?: string|Slot // 卡片标题content?: string|Slot // 卡片内容maxWidth?: string|number // 卡片内容最大宽度trigger?: 'hover'|'click' // 卡片触发方式overlayStyle?: CSSProperties // 卡片样式
}
const props = withDefaults(defineProps<Props>(), {title: '',content: '',maxWidth: 'auto',trigger: 'hover',overlayStyle: () => ({})
})
const popMaxWidth = computed(() => {if (typeof props.maxWidth === 'number') {return props.maxWidth + 'px'}return props.maxWidth
})
const visible = ref(false)
const top = ref(0) // 提示框top定位
const left = ref(0) // 提示框left定位
const defaultRef = ref() // 声明一个同名的模板引用
const popRef = ref() // 声明一个同名的模板引用
function getPosition () {const defaultWidth = defaultRef.value.offsetWidth // 展示文本宽度const popWidth = popRef.value.offsetWidth // 提示文本宽度const popHeight = popRef.value.offsetHeight // 提示文本高度top.value = popHeight + 4left.value = (popWidth - defaultWidth) / 2
}
const emit = defineEmits(['openChange'])
const hideTimer = ref()
function onShow () {getPosition()cancelRaf(hideTimer.value)visible.value = trueemit('openChange', visible.value)
}
function onHide (): void {hideTimer.value = rafTimeout(() => {visible.value = falseemit('openChange', visible.value)}, 100)
}
const activeBlur = ref(false) // 是否激活 blur 事件
function onOpen () {visible.value = !visible.valueif (visible.value) {getPosition()}emit('openChange', visible.value)
}
function onEnter () {activeBlur.value = false
}
function onLeave () {activeBlur.value = truepopRef.value.focus()
}
function onBlur () {visible.value = falseemit('openChange', false)
}
</script>
<template><divclass="m-popover"@mouseenter="trigger === 'hover' ? onShow() : () => false"@mouseleave="trigger === 'hover' ? onHide() : () => false"><divref="popRef"tabindex="1"class="m-pop-content":class="{'show-pop': visible}":style="`max-width: ${popMaxWidth}; top: ${-top}px; left: ${-left}px;`"@blur="trigger === 'click' && activeBlur ? onBlur() : () => false"@mouseenter="trigger === 'hover' ? onShow() : () => false"@mouseleave="trigger === 'hover' ? onHide() : () => false"><div class="m-pop" :style="overlayStyle"><div class="m-title"><slot name="title">{{ title }}</slot></div><div class="m-content"><slot name="content">{{ content }}</slot></div></div><div class="m-pop-arrow"><span class="u-pop-arrow"></span></div></div><divref="defaultRef"@click="trigger === 'click' ? onOpen() : () => false"@mouseenter="trigger === 'click' ? onEnter() : () => false"@mouseleave="trigger === 'click' ? onLeave() : () => false"><slot></slot></div></div>
</template>
<style lang="less" scoped>
.m-popover {position: relative;display: inline-block;.m-pop-content {position: absolute;z-index: 999;width: max-content;padding-bottom: 12px;outline: none;pointer-events: none;opacity: 0;transform-origin: 50% 75%;transform: scale(.8);transition: transform .25s, opacity .25s;.m-pop {min-width: 32px;min-height: 32px;padding: 12px;font-size: 14px;color: rgba(0, 0, 0, .88);line-height: 1.5714285714285714;text-align: start;text-decoration: none;word-break: break-all;cursor: auto;user-select: text;background-color: #FFF;border-radius: 8px;box-shadow: 0 6px 16px 0 rgba(0, 0, 0, .08), 0 3px 6px -4px rgba(0, 0, 0, .12), 0 9px 28px 8px rgba(0, 0, 0, .05);.m-title {min-width: 176px;margin-bottom: 8px;color: rgba(0, 0, 0, .88);font-weight: 600;}.m-content {color: rgba(0, 0, 0, .88);}}.m-pop-arrow {position: absolute;z-index: 9;left: 50%;bottom: 12px;transform: translateX(-50%) translateY(100%) rotate(180deg);display: block;pointer-events: none;width: 16px;height: 16px;overflow: hidden;&::before {position: absolute;bottom: 0;inset-inline-start: 0;width: 16px;height: 8px;background-color: #FFF;clip-path: path('M 0 8 A 4 4 0 0 0 2.82842712474619 6.82842712474619 L 6.585786437626905 3.0710678118654755 A 2 2 0 0 1 9.414213562373096 3.0710678118654755 L 13.17157287525381 6.82842712474619 A 4 4 0 0 0 16 8 Z');content: "";}&::after {position: absolute;width: 8.970562748477143px;height: 8.970562748477143px;bottom: 0;inset-inline: 0;margin: auto;border-radius: 0 0 2px 0;transform: translateY(50%) rotate(-135deg);box-shadow: 3px 3px 7px rgba(0, 0, 0, 0.1);z-index: 0;background: transparent;content: "";}}}.show-pop {pointer-events: auto;opacity: 1;transform: scale(1);}
}
</style>

在要使用的页面引入

<script setup lang="ts">
function openChange (visible: boolean) {console.log('visible:', visible)
}
</script>
<template><div class="ml60"><h1>{{ $route.name }} {{ $route.meta.title }}</h1><h2 class="mt30 mb10">基本使用</h2><Popover title="Title" @open-change="openChange"><template #content><p>Content</p><p>Content</p></template><Button type="primary">Hover me</Button></Popover><h2 class="mt30 mb10">不同的触发方式</h2><Popover title="Title" trigger="click"><template #content><p>Content</p><p>Content</p></template><Button type="primary">Click me</Button></Popover><h2 class="mt30 mb10">自定义样式</h2><Popovertitle="TitleTitleTitleTitleTitleTitleTitleTitleTitle":max-width="240":overlayStyle="{ padding: '12px 18px', borderRadius: '12px' }"><template #content><p>Content</p><p>Content</p></template><Button type="primary">Hover me</Button></Popover></div>
</template>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_1026679.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

不可变集合及Stream流

若希望某个数据是不可修改的&#xff0c;就可以考虑使用不可变集合&#xff0c;以提高安全性&#xff1b;&#xff08;JKD9之后才有&#xff09; List不可变集合&#xff1a; public static void main(String[] args) {/*创建不可变的List集合"张三", "李四&q…

蓝桥杯练习06给网页化个妆

给页面化个妆 介绍 各个网站都拥有登录页面&#xff0c;设计一个界面美观的登录页面&#xff0c;会给用户带来视觉上的享受。本题中我们要完成一个登录页面的布局。 准备 开始答题前&#xff0c;需要先打开本题的项目代码文件夹&#xff0c;目录结构如下&#xff1a; 其中&…

蓝桥杯2019年第十届省赛真题-组队

一、题目 组队 题目描述 作为篮球队教练&#xff0c;你需要从以下名单中选出 1 号位至 5 号位各一名球员&#xff0c; 组成球队的首发阵容。每位球员担任 1 号位至 5 号位时的评分如下表所示。请你计算首发阵容 1 号位至 5 号位的评分之和最大可能是多少&#xff1f; &#xff…

ubuntu - 编译 linphone-sdk

业务需求需要定制sdk&#xff0c;首先声明我们需要的是在Android4.4上跑的sdk&#xff0c;因此本次编译的sdk最低支持为19&#xff08;不同版本需要的环境不一致&#xff09;&#xff0c;编译过程较容易&#xff0c;难点在于环境配置 环境准备 Ubuntu 18.04.6 android-sdk_r24.…

面试题:Java虚拟机JVM的组成

1. 基础概念 JVM是什么 Java Virtual Machine Java程序的运行环境&#xff08;java二进制字节码的运行环境&#xff09; 好处&#xff1a; 一次编写&#xff0c;到处运行 自动内存管理&#xff0c;垃圾回收机制 JVM由哪些部分组成&#xff0c;运行流程是什么&#xff1f; …

每日一题 --- 移除链表元素[力扣][Go]

移除链表元素 题目&#xff1a;203. 移除链表元素 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xf…

ViTAR: Vision Transformer with Any Resolution

ViTAR: Vision Transformer with Any Resolution 相关链接&#xff1a;arxiv 关键字&#xff1a;Vision Transformer、Resolution Adaptability、Adaptive Token Merger、Fuzzy Positional Encoding、High-Resolution Image Processing 摘要 本文解决了视觉Transformer&#x…

【ORB-SLAM3】在 Ubuntu20.04 上编译 ORM-SLAM3 并使用 D435i、EuRoC 和 TUM-VI 运行测试

【ORB-SLAM3】在 Ubuntu20.04 上编译 ORM-SLAM3 并使用 D435i、EuRoC 和 TUM-VI 运行测试 1 Prerequisites1.1 C11 or C0x Compiler1.2 Pangolin1.3 OpenCV1.4 Eigen3 2 安装 Intel RealSense™ SDK 2.02.1 测试设备2.2 编译源码安装 (Recommend)2.3 预编译包安装 3 编译 ORB-S…

[密码学] 密码学基础

目录 一 为什么要加密? 二 常见的密码算法 三 密钥 四 密码学常识 五 密码信息威胁 六 凯撒密码 一 为什么要加密? 在互联网的通信中&#xff0c;数据是通过很多计算机或者通信设备相互转发&#xff0c;才能够到达目的地,所以在这个转发的过程中&#xff0c;如果通信包…

常见的三种办公室租赁方式各自优缺点

商业办公的租赁市场。找商业办公地点&#xff0c;跟找住宅租房有点像&#xff0c;但目的和要求不同。主要也是三种方式&#xff1a;直接找房东租、接手别人的转租&#xff0c;或者找中介帮忙。每种方式都有它的小窍门和注意事项。 直租 直租商业办公&#xff0c;就是直接和办公…

GPT提示词分享 —— 代码释义者

提示词&#x1f447; 我希望你能充当代码解释者&#xff0c;阐明代码的语法和语义。 3.5版本&#x1f447; free2gpt 4.0版本&#x1f447; gpt4

互联网医院APP开发攻略:搭建智能医疗平台

互联网医院APP为患者提供了便捷的就医途径&#xff0c;还为医生和医院提供了更加高效的服务和管理手段。接下来&#xff0c;小编将我们本文将就互联网医院APP的开发攻略&#xff0c;以及如何搭建智能医疗平台进行探讨。 1.确定需求和目标 这包括确定服务对象&#xff08;患者、…

鸿蒙HarmonyOS应用开发之C/C++标准库机制概述

OpenHarmony NDK提供业界标准库 libc标准库、 C标准库 &#xff0c;本文用于介绍C/C标准库在OpenHarmony中的机制&#xff0c;开发者了解这些机制有助于在NDK开发过程中避免相关问题。 1. C兼容性 在OpenHarmony系统中&#xff0c;系统库与应用Native库都在使用C标准库&#…

基于springboot实现房屋租赁管理系统项目【项目源码+论文说明】

基于springboot实现房屋租赁系统演示 摘要 房屋是人类生活栖息的重要场所&#xff0c;随着城市中的流动人口的增多&#xff0c;人们对房屋租赁需求越来越高&#xff0c;为满足用户查询房屋、预约看房、房屋租赁的需求&#xff0c;特开发了本基于Spring Boot的房屋租赁系统。 …

Sublime for Mac 使用插件Terminus

1. 快捷键打开命令面板 commandshiftp2. 选择 Package Control: Install Package&#xff0c;然后会出现安装包的列表 3. 在安装终端插件前&#xff0c;我们先装个汉化包&#xff0c;ChineseLocallization&#xff0c;安装完重启 4. 输入 terminus&#xff0c;选择第一个&am…

瑞吉外卖实战学习--登录功能的开发

登录功能的开发 前端1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类1.1、字段名称对应上&#xff0c;有下划线的使用驼峰对应&#xff0c;因为在配置文件中进行了配置1.2、employee 文件 2、创建Controller、Service、Mapper2.1、Mapper文件2.2、定…

利用机器学习打造反电信诈骗系统

利用机器学习打造反电信诈骗系统 技术与功能数据集与模型可视化分析与词云结语 随着互联网的普及&#xff0c;电信诈骗日益猖獗&#xff0c;给人们的生活和财产安全带来了巨大的威胁。为了有效应对这一挑战&#xff0c;我们开发了一款基于机器学习的反电信诈骗系统&#xff0c;…

从后端到前端

原文地址&#xff1a;从后端到前端 - Pleasure的博客 下面是正文内容&#xff1a; 前言 在前面几章中主要介绍了系统开发的后端部分&#xff0c;但是验证接口的适用性只能通过专门的软件&#xff08;Apifox&#xff0c;Postman等&#xff09;来进行测试。那从现在开始&#xf…

mysql公用表表达式CTE

公用表达式是MySQL8.0的新特性&#xff0c;它是一个命名的临时结果集&#xff0c;作用范围是当前语句。 可以理解成为当前sql语句定义了一个视图&#xff0c;sql语句的任何地方都可以使用这个视图&#xff0c;如果被多次使用就体现出了公用表达式的特点公用。 依据语法结构和执…

【Linux】进程>环境变量地址空间进程调度

主页&#xff1a;醋溜马桶圈-CSDN博客 专栏&#xff1a;Linux_醋溜马桶圈的博客-CSDN博客 gitee&#xff1a;mnxcc (mnxcc) - Gitee.com 目录 1.环境变量 1.1 基本概念 1.2 常见环境变量 1.3 查看环境变量方法 1.4 和环境变量相关的命令 1.5 环境变量的组织方式 1.6 通…