291 lines
8.7 KiB
HTML
291 lines
8.7 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>12方向Tooltip</title>
|
|
<style>
|
|
/* 基础样式 */
|
|
body {
|
|
min-height: 100vh;
|
|
margin: 0;
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 2rem;
|
|
padding: 2rem;
|
|
background: #f0f2f5;
|
|
}
|
|
|
|
.trigger {
|
|
padding: 0.8rem 1.6rem;
|
|
border: none;
|
|
border-radius: 6px;
|
|
background: #2196f3;
|
|
color: white;
|
|
font-size: 1rem;
|
|
cursor: pointer;
|
|
transition: opacity 0.2s;
|
|
}
|
|
|
|
.trigger:hover {
|
|
opacity: 0.9;
|
|
}
|
|
|
|
/* Tooltip 核心样式 */
|
|
.tooltip {
|
|
position: fixed;
|
|
z-index: 10000;
|
|
min-width: 60px;
|
|
max-width: 240px;
|
|
padding: 0.8rem 1.2rem;
|
|
background: rgba(0, 0, 0, 0.8);
|
|
color: white;
|
|
font-size: 0.9rem;
|
|
line-height: 1.4;
|
|
border-radius: 4px;
|
|
visibility: hidden;
|
|
opacity: 0;
|
|
transition:
|
|
opacity 0.2s,
|
|
visibility 0.2s;
|
|
pointer-events: none;
|
|
}
|
|
|
|
/* 箭头通用样式 */
|
|
.tooltip::after {
|
|
content: '';
|
|
position: absolute;
|
|
width: 0;
|
|
height: 0;
|
|
border: 6px solid transparent;
|
|
}
|
|
|
|
/* 不同方向的样式 */
|
|
/* 上方系列 */
|
|
.tooltip.top {
|
|
transform: translate(-50%, calc(-100% - 12px));
|
|
}
|
|
.tooltip.top::after {
|
|
bottom: -12px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
border-top-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.top-left {
|
|
transform: translate(calc(-100% + 12px), calc(-100% - 12px));
|
|
}
|
|
.tooltip.top-left::after {
|
|
bottom: -12px;
|
|
left: 12px;
|
|
border-top-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.top-right {
|
|
transform: translate(12px, calc(-100% - 12px));
|
|
}
|
|
.tooltip.top-right::after {
|
|
bottom: -12px;
|
|
right: 12px;
|
|
border-top-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
/* 下方系列 */
|
|
.tooltip.bottom {
|
|
transform: translate(-50%, 12px);
|
|
}
|
|
.tooltip.bottom::after {
|
|
top: -12px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
border-bottom-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.bottom-left {
|
|
transform: translate(calc(-100% + 12px), 12px);
|
|
}
|
|
.tooltip.bottom-left::after {
|
|
top: -12px;
|
|
left: 12px;
|
|
border-bottom-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.bottom-right {
|
|
transform: translate(12px, 12px);
|
|
}
|
|
.tooltip.bottom-right::after {
|
|
top: -12px;
|
|
right: 12px;
|
|
border-bottom-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
/* 左侧系列 */
|
|
.tooltip.left {
|
|
transform: translate(calc(-100% - 12px), -50%);
|
|
}
|
|
.tooltip.left::after {
|
|
right: -12px;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
border-left-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.left-top {
|
|
transform: translate(calc(-100% - 12px), calc(-100% + 12px));
|
|
}
|
|
.tooltip.left-top::after {
|
|
right: -12px;
|
|
top: 12px;
|
|
border-left-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.left-bottom {
|
|
transform: translate(calc(-100% - 12px), 12px);
|
|
}
|
|
.tooltip.left-bottom::after {
|
|
right: -12px;
|
|
bottom: 12px;
|
|
border-left-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
/* 右侧系列 */
|
|
.tooltip.right {
|
|
transform: translate(12px, -50%);
|
|
}
|
|
.tooltip.right::after {
|
|
left: -12px;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
border-right-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.right-top {
|
|
transform: translate(12px, calc(-100% + 12px));
|
|
}
|
|
.tooltip.right-top::after {
|
|
left: -12px;
|
|
top: 12px;
|
|
border-right-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.tooltip.right-bottom {
|
|
transform: translate(12px, 12px);
|
|
}
|
|
.tooltip.right-bottom::after {
|
|
left: -12px;
|
|
bottom: 12px;
|
|
border-right-color: rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
/* 显示状态 */
|
|
.tooltip.visible {
|
|
visibility: visible;
|
|
opacity: 1;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<!-- 触发元素示例 -->
|
|
<button class="trigger" data-tooltip="顶部提示" data-direction="top">顶部</button>
|
|
<button class="trigger" data-tooltip="顶部左侧提示" data-direction="top-left">顶部左</button>
|
|
<button class="trigger" data-tooltip="顶部右侧提示" data-direction="top-right">顶部右</button>
|
|
<button class="trigger" data-tooltip="底部提示" data-direction="bottom">底部</button>
|
|
<button class="trigger" data-tooltip="底部左侧提示" data-direction="bottom-left">底部左</button>
|
|
<button class="trigger" data-tooltip="底部右侧提示" data-direction="bottom-right">底部右</button>
|
|
<button class="trigger" data-tooltip="左侧提示" data-direction="left">左侧</button>
|
|
<button class="trigger" data-tooltip="左侧顶部提示" data-direction="left-top">左侧顶</button>
|
|
<button class="trigger" data-tooltip="左侧底部提示" data-direction="left-bottom">左侧底</button>
|
|
<button class="trigger" data-tooltip="右侧提示" data-direction="right">右侧</button>
|
|
<button class="trigger" data-tooltip="右侧顶部提示" data-direction="right-top">右侧顶</button>
|
|
<button class="trigger" data-tooltip="右侧底部提示" data-direction="right-bottom">右侧底</button>
|
|
|
|
<script>
|
|
class TooltipManager {
|
|
constructor() {
|
|
this.tooltip = null
|
|
this.init()
|
|
}
|
|
|
|
init() {
|
|
// 创建tooltip元素
|
|
this.tooltip = document.createElement('div')
|
|
this.tooltip.className = 'tooltip'
|
|
document.body.appendChild(this.tooltip)
|
|
|
|
// 绑定触发事件
|
|
document.querySelectorAll('.trigger').forEach((trigger) => {
|
|
trigger.addEventListener('mouseenter', (e) => this.show(e))
|
|
trigger.addEventListener('mouseleave', () => this.hide())
|
|
})
|
|
}
|
|
|
|
show(e) {
|
|
const { direction } = e.target.dataset
|
|
const text = e.target.dataset.tooltip
|
|
const triggerRect = e.target.getBoundingClientRect()
|
|
|
|
// 设置内容
|
|
this.tooltip.textContent = text
|
|
this.tooltip.className = `tooltip ${direction}`
|
|
|
|
// 计算初始位置
|
|
const { offsetWidth: tw, offsetHeight: th } = this.tooltip
|
|
const { left: tx, top: ty, width: trw, height: thr } = triggerRect
|
|
const offset = 12 // 基础偏移量
|
|
|
|
// 方向位置映射
|
|
const positionMap = {
|
|
top: { left: tx + trw / 2, top: ty - th - offset },
|
|
'top-left': { left: tx + trw - 12, top: ty - th - offset },
|
|
'top-right': { left: tx + 12, top: ty - th - offset },
|
|
bottom: { left: tx + trw / 2, top: ty + thr + offset },
|
|
'bottom-left': { left: tx + trw - 12, top: ty + thr + offset },
|
|
'bottom-right': { left: tx + 12, top: ty + thr + offset },
|
|
left: { left: tx - tw - offset, top: ty + thr / 2 },
|
|
'left-top': { left: tx - tw - offset, top: ty + 12 },
|
|
'left-bottom': { left: tx - tw - offset, top: ty + thr - 12 },
|
|
right: { left: tx + trw + offset, top: ty + thr / 2 },
|
|
'right-top': { left: tx + trw + offset, top: ty + 12 },
|
|
'right-bottom': { left: tx + trw + offset, top: ty + thr - 12 },
|
|
}
|
|
|
|
// 获取计算后的位置
|
|
let { left, top } = positionMap[direction]
|
|
|
|
// 边界检测(防止超出视口)
|
|
const viewportWidth = window.innerWidth
|
|
const viewportHeight = window.innerHeight
|
|
|
|
// 水平方向边界处理
|
|
if (left < 0)
|
|
left = 12 // 左边界
|
|
else if (left + tw > viewportWidth) left = viewportWidth - tw - 12 // 右边界
|
|
|
|
// 垂直方向边界处理
|
|
if (top < 0)
|
|
top = 12 // 上边界
|
|
else if (top + th > viewportHeight) top = viewportHeight - th - 12 // 下边界
|
|
|
|
// 应用位置
|
|
this.tooltip.style.left = `${left}px`
|
|
this.tooltip.style.top = `${top}px`
|
|
|
|
// 显示tooltip
|
|
requestAnimationFrame(() => {
|
|
this.tooltip.classList.add('visible')
|
|
})
|
|
}
|
|
|
|
hide() {
|
|
this.tooltip.classList.remove('visible')
|
|
}
|
|
}
|
|
|
|
// 初始化Tooltip管理器
|
|
new TooltipManager()
|
|
</script>
|
|
</body>
|
|
</html>
|