Skip to content

Commit 231896c

Browse files
committed
add link hovering tooltip
1 parent 955fb42 commit 231896c

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

src/layouts/BaseLayout.astro

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import "../styles/global.css";
1515
<body>
1616
<div class="page-wrapper">
1717
<Header />
18-
18+
1919
<!-- First slot for content outside the content div -->
2020
<slot name="outside-content" />
2121

@@ -26,6 +26,49 @@ import "../styles/global.css";
2626

2727
<Footer />
2828
</div>
29-
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
29+
<script
30+
id="MathJax-script"
31+
async
32+
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
33+
></script>
34+
<div id="tooltip" class="tooltip"></div>
35+
36+
<script>
37+
(function () {
38+
const tooltip = document.getElementById("tooltip");
39+
40+
document.addEventListener("mouseover", function (event) {
41+
const target = event.target;
42+
if (target.tagName.toLowerCase() === "a") {
43+
const href = target.getAttribute("href");
44+
45+
const link = document.createElement("a");
46+
link.href = href;
47+
48+
if (link.hostname && link.hostname !== location.hostname) {
49+
const displayHostname = link.hostname.replace(/^www\./, "");
50+
51+
tooltip.textContent = displayHostname;
52+
tooltip.style.display = "block";
53+
tooltip.style.left = event.pageX + 10 + "px";
54+
tooltip.style.top = event.pageY + 10 + "px";
55+
}
56+
}
57+
});
58+
59+
document.addEventListener("mouseout", function (event) {
60+
if (event.target.tagName.toLowerCase() === "a") {
61+
tooltip.style.display = "none";
62+
}
63+
});
64+
65+
document.addEventListener("mousemove", function (event) {
66+
if (tooltip.style.display === "block") {
67+
tooltip.style.left = event.pageX + 10 + "px";
68+
tooltip.style.top = event.pageY + 10 + "px";
69+
}
70+
});
71+
})();
72+
</script>
3073
</body>
3174
</html>

src/pages/about.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ author: Andrew
66
tags: ["one", "two", "three"]
77
---
88

9+
10+
<a href="https://sub.example.com/page">External Link with Subdomain</a><br>
11+
<a href="https://www.google.com/">Google</a><br>
12+
<a href="/local/path">Local Link</a><br>
13+
<a href="#section">Anchor Link</a><br>
14+
<a href="page.html">Relative Link</a><br>
15+
16+
917
# h1 Heading 8-)
1018
## h2 Heading
1119
### h3 Heading

src/pages/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: Andrew
66
tags: ["one", "two", "three"]
77
---
88

9-
Lorem ipsum odor amet, consectetuer adipiscing elit. Sem ac proin erat; efficitur maecenas lobortis tempus. Sed duis euismod ultricies netus arcu magnis rutrum velit. Hac himenaeos finibus lacus nascetur curabitur ornare montes. Nibh ullamcorper risus ligula, leo mauris scelerisque ex felis. Sed pretium gravida volutpat semper nisl primis pellentesque eu torquent. Tempus vulputate efficitur etiam class diam sapien potenti?
9+
hey
1010

1111
```js
1212
for i in range(0,5)

src/styles/global.css

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
* {
2-
font-family: "Charter", sans-serif;
3-
color: #f0e7d8;
2+
font-family: "Charter", sans-serif;
3+
color: #f0e7d8;
44
}
55

6-
p, a {
7-
font-size: 1.1em;
6+
p,
7+
a {
8+
font-size: 1.1em;
89
}
910
body {
10-
width: 80%;
11-
margin: 0 auto;
12-
background-color: #1b1919;
13-
min-height: 100vh;
14-
display: flex;
15-
flex-direction: column;
11+
width: 80%;
12+
margin: 0 auto;
13+
background-color: #1b1919;
14+
min-height: 100vh;
15+
display: flex;
16+
flex-direction: column;
1617
}
1718

1819
.page-wrapper {
19-
display: flex;
20-
flex-direction: column;
21-
flex-grow: 1;
20+
display: flex;
21+
flex-direction: column;
22+
flex-grow: 1;
2223
}
2324

2425
.content {
25-
flex-grow: 1;
26-
width: 80%;
27-
margin: 0 auto;
26+
flex-grow: 1;
27+
width: 80%;
28+
margin: 0 auto;
2829
}
2930

30-
3131
footer {
32-
margin-top: auto;
32+
margin-top: auto;
3333
}
3434

3535
h1 {
36-
font-size: 2.em;
36+
font-size: 2em;
37+
}
38+
39+
.tooltip {
40+
position: absolute;
41+
background-color: #392e2e;
42+
color: #f0e7d8;
43+
padding: 5px 8px;
44+
font-size: 14px;
45+
display: none;
46+
pointer-events: none;
47+
font-weight: bold;
48+
border: 1px solid #f0e7d8;
3749
}

0 commit comments

Comments
 (0)