Skip to content

Commit 6d87a6d

Browse files
committed
Add hover links for headings
1 parent 36b25fb commit 6d87a6d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

_includes/head/custom.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
</script>
1616
{% endif %}
1717
<script type="text/javascript" async src="/assets/js/love.js"></script>
18+
<script type="text/javascript" async src="/assets/js/auto-heading-links.js"></script>
1819
<!-- Minimal Mistakes layout: {{ page.layout }} -->

assets/css/main.scss

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,33 @@ body.landing {
100100
}
101101
}
102102
}
103+
104+
section.page__content {
105+
h2, h3, h4, h5, h6 {
106+
.header-link {
107+
position: relative;
108+
left: 0.5em;
109+
opacity: 0;
110+
font-size: 0.8em;
111+
-webkit-transition: opacity 0.2s ease-in-out 0.1s;
112+
-moz-transition: opacity 0.2s ease-in-out 0.1s;
113+
-o-transition: opacity 0.2s ease-in-out 0.1s;
114+
transition: opacity 0.2s ease-in-out 0.1s;
115+
}
116+
117+
&:hover .header-link {
118+
opacity: 1;
119+
}
120+
}
121+
}
122+
123+
.sr-only { // Screen reader only
124+
position: absolute;
125+
width: 1px;
126+
height: 1px;
127+
padding: 0;
128+
margin: -1px;
129+
overflow: hidden;
130+
clip: rect(0, 0, 0, 0);
131+
border: 0;
132+
}

assets/js/auto-heading-links.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var anchorForId = function (id) {
2+
var anchor = document.createElement("a");
3+
anchor.className = "header-link";
4+
anchor.href = "#" + id;
5+
anchor.innerHTML = "<span class=\"sr-only\">Permalink</span><i class=\"fa fa-link\"></i>";
6+
anchor.title = "Permalink";
7+
return anchor;
8+
};
9+
10+
var linkifyAnchors = function (level, containingElement) {
11+
var headers = containingElement.getElementsByTagName("h" + level);
12+
for (var h = 0; h < headers.length; h++) {
13+
var header = headers[h];
14+
15+
if (typeof header.id !== "undefined" && header.id !== "") {
16+
header.appendChild(anchorForId(header.id));
17+
}
18+
}
19+
};
20+
21+
document.onreadystatechange = function () {
22+
if (this.readyState === "complete") {
23+
var contentBlock = document.getElementsByClassName("page__content")[0];
24+
if (!contentBlock) {
25+
return;
26+
}
27+
for (var level = 1; level <= 6; level++) {
28+
linkifyAnchors(level, contentBlock);
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)