106 lines
2.3 KiB
HTML
106 lines
2.3 KiB
HTML
<!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;
|
|
}
|
|
|
|
/* 触发按钮 */
|
|
.btn {
|
|
padding: 10px 20px;
|
|
background-color: #3498db;
|
|
color: white;
|
|
border: none;
|
|
cursor: pointer;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
/* 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;
|
|
}
|
|
|
|
/* 箭头公共样式 */
|
|
.popup::after {
|
|
content: '';
|
|
position: absolute;
|
|
width: 0;
|
|
height: 0;
|
|
border-style: solid;
|
|
}
|
|
|
|
/* 箭头方向 */
|
|
.popup.top::after {
|
|
bottom: -8px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
border-width: 8px 8px 0 8px;
|
|
border-color: #333 transparent transparent transparent;
|
|
}
|
|
|
|
.popup.bottom::after {
|
|
top: -8px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
border-width: 0 8px 8px 8px;
|
|
border-color: transparent transparent #333 transparent;
|
|
}
|
|
|
|
.popup.left::after {
|
|
top: 50%;
|
|
right: -8px;
|
|
transform: translateY(-50%);
|
|
border-width: 8px 0 8px 8px;
|
|
border-color: transparent transparent transparent #333;
|
|
}
|
|
|
|
.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>
|
|
|
|
<button class="btn" id="btn">Hover me</button>
|
|
|
|
<div class="popup top" id="popup">这是一个带箭头的 Popup!</div>
|
|
|
|
<script>
|
|
const btn = document.getElementById('btn');
|
|
const popup = document.getElementById('popup');
|
|
|
|
// 鼠标悬停显示
|
|
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';
|
|
});
|
|
|
|
btn.addEventListener('mouseleave', () => {
|
|
popup.style.display = 'none';
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|