-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanchor-basic.html
More file actions
104 lines (95 loc) · 2.58 KB
/
anchor-basic.html
File metadata and controls
104 lines (95 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>anchor()関数でツールチップを配置する</title>
<style>
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: sans-serif;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: #f5f5f5;
}
@keyframes move-anchor {
0% { translate: 0px 0px; }
25% { translate: 120px 0px; }
50% { translate: 120px 80px; }
75% { translate: 0px 80px; }
100% { translate: 0px 0px; }
}
.anchor {
anchor-name: --my-anchor;
width: fit-content;
padding: 8px 16px;
background-color: #1a73e8;
color: #fff;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
animation: move-anchor 3s ease-in-out infinite paused;
}
.anchor.is-animated {
animation-play-state: running;
}
.tooltip {
position: absolute;
position-anchor: --my-anchor;
top: calc(anchor(bottom) + 8px);
left: anchor(center);
translate: -50% 0;
padding: 8px 16px;
background-color: #333;
color: #fff;
border-radius: 4px;
white-space: nowrap;
}
/* 吹き出しの三角(上向き) */
.tooltip::before {
content: "";
position: absolute;
top: -6px;
left: 50%;
translate: -50% 0;
border: 6px solid transparent;
border-top: none;
border-bottom-color: #333;
}
.control {
position: fixed;
bottom: 16px;
left: 50%;
translate: -50% 0;
padding: 8px 16px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 0.875rem;
cursor: pointer;
}
</style>
</head>
<body>
<button class="anchor" type="button">アンカー要素</button>
<div class="tooltip">ツールチップのテキストが入ります</div>
<button class="control" type="button" id="toggle-btn">アニメーション開始</button>
<script>
const anchor = document.querySelector('.anchor');
const toggleBtn = document.getElementById('toggle-btn');
toggleBtn.addEventListener('click', () => {
anchor.classList.toggle('is-animated');
toggleBtn.textContent = anchor.classList.contains('is-animated')
? 'アニメーション停止'
: 'アニメーション開始';
});
</script>
</body>
</html>