Skip to content

Commit

Permalink
<497> Add anchors to main sections
Browse files Browse the repository at this point in the history
  • Loading branch information
federicobucchi committed Mar 24, 2024
1 parent 650f313 commit 1411f79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ layout: source
element.className = classString;
}

function addHeadingLinks() {
const headings = document.querySelectorAll('h2[id]');
const dash = '#';

for (const heading of headings) {
const dashLink = document.createElement('a');

dashLink.setAttribute('href', `${dash}${heading.id}`);
dashLink.innerHTML = dash;
heading.classList.add('with-anchor');
heading.appendChild(dashLink);
}
}

document.getElementById('menu-toggle').addEventListener('mousedown', function() {
const menuToggle = document.getElementById('menu-toggle');
toggleClass(menuToggle, 'open');
Expand All @@ -57,5 +71,7 @@ layout: source
}
});
});

addHeadingLinks();
});
})();
15 changes: 15 additions & 0 deletions assets/stylesheets/_screen.scss
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ cite {
}
}

h2.with-anchor {
&:hover {
a {
display: inline-block;
}
}

a {
color: var(--color-text);
display: none;
margin-left: .2em;
text-decoration: none;
}
}

#logo {
margin-bottom: 0;

Expand Down

0 comments on commit 1411f79

Please sign in to comment.