【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>
  • 效果展示:
相关推荐
石小石Orz2 分钟前
因为没有使用路由懒加载,产生了一个难以寻找的bug
前端
Mintopia3 分钟前
Three.js 力导向图:让数据跳起优雅的华尔兹
前端·javascript·three.js
墨渊君18 分钟前
React Native 跨平台组件库实践: GlueStack UI 上手指南
前端
晓得迷路了25 分钟前
栗子前端技术周刊第 84 期 - Vite v7.0 beta、Vitest 3.2、Astro 5.9...
前端·javascript·vite
独立开阀者_FwtCoder28 分钟前
最全301/302重定向指南:从SEO到实战,一篇就够了
前端·javascript·vue.js
Moment38 分钟前
给大家推荐一个超好用的 Marsview 低代码平台 🤩🤩🤩
前端·javascript·github
小满zs41 分钟前
Zustand 第三章(状态简化)
前端·react.js
普宁彭于晏43 分钟前
元素水平垂直居中的方法
前端·css·笔记·css3
恋猫de小郭1 小时前
为什么跨平台框架可以适配鸿蒙,它们的技术原理是什么?
android·前端·flutter
云浪1 小时前
元素变形记:CSS 缩放函数全指南
前端·css