u
This commit is contained in:
1 parent
d17b49cdd9
commit
5136bb90bd
4 files changed
+595
-114
No files matched your search
@@ -1,105 +1,290 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Popup with Arrow</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
padding: 50px;
|
||||
}
|
||||
<!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;
|
||||
}
|
||||
|
||||
/* 触发按钮 */
|
||||
.btn {
|
||||
padding: 10px 20px;
|
||||
background-color: #3498db;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.trigger {
|
||||
padding: 0.8rem 1.6rem;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: #2196f3;
|
||||
color: white;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: opacity 0.2s;
|
||||
}
|
||||
|
||||
/* Popup 基础样式 */
|
||||
.popup {
|
||||
position: absolute;
|
||||
padding: 10px 15px;
|
||||
background-color: #333;
|
||||
color: #fff;
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
|
||||
display: none;
|
||||
z-index: 100;
|
||||
max-width: 200px;
|
||||
text-align: center;
|
||||
}
|
||||
.trigger:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
/* 箭头公共样式 */
|
||||
.popup::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-style: solid;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* 箭头方向 */
|
||||
.popup.top::after {
|
||||
bottom: -8px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
border-width: 8px 8px 0 8px;
|
||||
border-color: #333 transparent transparent transparent;
|
||||
}
|
||||
/* 箭头通用样式 */
|
||||
.tooltip::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
width: 0;
|
||||
height: 0;
|
||||
border: 6px solid transparent;
|
||||
}
|
||||
|
||||
.popup.bottom::after {
|
||||
top: -8px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
border-width: 0 8px 8px 8px;
|
||||
border-color: transparent transparent #333 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);
|
||||
}
|
||||
|
||||
.popup.left::after {
|
||||
top: 50%;
|
||||
right: -8px;
|
||||
transform: translateY(-50%);
|
||||
border-width: 8px 0 8px 8px;
|
||||
border-color: transparent transparent transparent #333;
|
||||
}
|
||||
.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);
|
||||
}
|
||||
|
||||
.popup.right::after {
|
||||
top: 50%;
|
||||
left: -8px;
|
||||
transform: translateY(-50%);
|
||||
border-width: 8px 8px 8px 0;
|
||||
border-color: transparent #333 transparent transparent;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
.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);
|
||||
}
|
||||
|
||||
<button class="btn" id="btn">Hover me</button>
|
||||
/* 下方系列 */
|
||||
.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);
|
||||
}
|
||||
|
||||
<div class="popup top" id="popup">这是一个带箭头的 Popup!</div>
|
||||
.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);
|
||||
}
|
||||
|
||||
<script>
|
||||
const btn = document.getElementById('btn');
|
||||
const popup = document.getElementById('popup');
|
||||
.tooltip.bottom-right {
|
||||
transform: translate(12px, 12px);
|
||||
}
|
||||
.tooltip.bottom-right::after {
|
||||
top: -12px;
|
||||
right: 12px;
|
||||
border-bottom-color: rgba(0, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
// 鼠标悬停显示
|
||||
btn.addEventListener('mouseenter', () => {
|
||||
const rect = btn.getBoundingClientRect();
|
||||
popup.style.display = 'block';
|
||||
// 设置 Popup 位置(示例:顶部)
|
||||
popup.style.left = rect.left + rect.width / 2 - popup.offsetWidth / 2 + 'px';
|
||||
popup.style.top = rect.top - popup.offsetHeight - 10 + 'px';
|
||||
});
|
||||
/* 左侧系列 */
|
||||
.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);
|
||||
}
|
||||
|
||||
btn.addEventListener('mouseleave', () => {
|
||||
popup.style.display = 'none';
|
||||
});
|
||||
</script>
|
||||
.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);
|
||||
}
|
||||
|
||||
</body>
|
||||
.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>
|
||||
Reference in new issue
Block a user