【CSS】手写 Tooltip 提示组件

文章目录

效果示例

代码实现

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>一颗不甘坠落的流星</title>
		<style>
			body {
				padding: 120px;
			}
			.tooltip {
				position: relative;
				display: inline-block;
				/* 如果需要在可悬停文本下面显示点线 */
				border-bottom: 1px dotted black;
				/* 悬浮小手 */
				cursor: pointer;
			}

			/* Tooltip 文本 */
			.tooltip .tooltiptext {
				visibility: hidden;
				background-color: black;
				color: #fff;
				text-align: center;
				border-radius: 6px;
				padding: 5px 0;
				position: absolute;
				z-index: 1;
				opacity: 0.8;
			}

			/* 创建Tooltip 箭头 */
			.tooltip .tooltiptext::after {
				content: "";
				position: absolute;
				border-width: 5px;
				border-style: solid;
			}

			/* 将鼠标悬停在工具提示容器上时,显示工具提示文本 */
			.tooltip:hover .tooltiptext {
				visibility: visible;
			}

			/* 上悬浮样式 */
			#top .tooltiptext {
				width: 120px;
				bottom: 100%;
				left: 50%;
				/* -width/2 : -(120/2 = 60)*/
				margin-left: -60px;
			}

			#top .tooltiptext::after {
				top: 100%;
				left: 50%;
				margin-left: -5px;
				border-color: black transparent transparent transparent;
			}

			/* 下悬浮样式 */
			#bottom .tooltiptext {
				width: 120px;
				top: 100%;
				left: 50%;
				/* -width/2 : -(120/2 = 60)*/
				margin-left: -60px;
			}

			#bottom .tooltiptext::after {
				bottom: 100%;
				left: 50%;
				margin-left: -5px;
				border-color: transparent transparent black transparent;
			}

			/* 左悬浮样式 */
			#left .tooltiptext {
				width: 120px;
				top: -5px;
				right: 105%;
			}

			#left .tooltiptext::after {
				top: 50%;
				left: 100%;
				margin-top: -5px;
				border-color: transparent transparent transparent black;

			}

			/* 右悬浮样式 */
			#right .tooltiptext {
				width: 120px;
				top: -5px;
				left: 105%;
			}

			#right .tooltiptext::after {
				top: 50%;
				right: 100%;
				margin-top: -5px;
				border-color: transparent black transparent transparent;
			}
		</style>
	<body>
		<div class="tooltip" id="top">
			<span>上悬浮展示</span>
			<span class="tooltiptext">Tooltip text</span>
		</div>
		<div class="tooltip" id="bottom">
			<span>下悬浮展示</span>
			<span class="tooltiptext">Tooltip text</span>
		</div>
		<div class="tooltip" id="left">
			<span>左悬浮展示</span>
			<span class="tooltiptext">Tooltip text</span>
		</div>
		<div class="tooltip" id="right">
			<span>右悬浮展示</span>
			<span class="tooltiptext">Tooltip text</span>
		</div>
	</body>
	<script type="text/javascript"></script>
</html>
  • 效果展示:
相关推荐
excel5 分钟前
如何解决 Nuxt DevTools 中关于 unstorage 包的报错
前端
Rust研习社8 分钟前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
C澒27 分钟前
AI 生码 - API2Code:接口智能匹配与 API 自动化生码全链路设计
前端·低代码·ai编程
浔川python社37 分钟前
HTML头部元信息避坑指南技术文章大纲
前端·html
IT_陈寒1 小时前
SpringBoot配置加载顺序把我坑惨了
前端·人工智能·后端
kyriewen1 小时前
Next.js部署:从本地跑得欢,到线上飞得稳
前端·react.js·next.js
Moment1 小时前
面试官:给 llm 传递上下文,有哪几个身份 role ❓❓❓
前端·后端·面试
跨境数据猎手1 小时前
跨境独立站系统技术拆解(附带源码)
服务器·前端·php
豹哥学前端1 小时前
用猜数字游戏,一口气掌握 JavaScript 核心知识点(附完整代码)
前端·javascript
忆往wu前2 小时前
从0到1一步步拆解搭建,梳理一个 Vue3 简易图书后台全开发流程
前端·javascript·vue.js