【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>
  • 效果展示:
相关推荐
小飞悟5 分钟前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor5 分钟前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js
FogLetter6 分钟前
初识图片懒加载:让网页像"懒人"一样聪明加载
前端·javascript
微客鸟窝7 分钟前
一文搞懂NVM管理Node.js:从安装到实战全攻略
前端
归于尽8 分钟前
Cookie、Session、JWT 的前世今生
前端
程序员辉哥9 分钟前
学会在Cursor中使用Rules生成代码后可以躺平了吗?
前端·后端
请你吃div14 分钟前
JavaScript 实用函数大全(超实用)
前端·javascript·面试
一个水瓶座程序猿.15 分钟前
Vue3 中使用 Vueuse
前端·javascript·vue.js
夏梦春蝉16 分钟前
ES6从入门到精通:Symbol与迭代器
前端·javascript·es6
一个水瓶座程序猿.20 分钟前
ES6数组的`flat()`和`flatMap()`函数用法
前端·ecmascript·es6