【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>
  • 效果展示:
相关推荐
周航宇JoeZhou5 分钟前
JP4-7-MyLesson后台前端(五)
java·前端·vue·elementplus·前端项目·mylesson·管理平台
Yaavi8 分钟前
一个基于markdown的高性能博客模板
前端·开源·源码
艾小码27 分钟前
手把手教你实现一个EventEmitter,彻底告别复杂事件管理!
前端·javascript·node.js
幸福摩天轮36 分钟前
npm发布包
前端
前端AK君39 分钟前
Gitlab 线上合并冲突的坑
前端
ze_juejin39 分钟前
ES6 Module 深入学习
前端
章丸丸42 分钟前
Tube - Studio Videos
前端·后端
因吹斯汀2 小时前
一饭封神:当AI厨神遇上你的冰箱,八大菜系大师在线battle!
前端·vue.js·ai编程
心一信息2 小时前
ThreeJS骨骼示例
css·css3·html5
再学一点就睡2 小时前
NATAPP 内网穿透指南:让本地项目轻松 “走出去”
前端