Skip to content

Instantly share code, notes, and snippets.

@andy23512
Created March 5, 2020 13:17
Show Gist options
  • Select an option

  • Save andy23512/1550fa4c0f445d6444e5544fb79e30c5 to your computer and use it in GitHub Desktop.

Select an option

Save andy23512/1550fa4c0f445d6444e5544fb79e30c5 to your computer and use it in GitHub Desktop.
.anchor-menu {
background-color: rgba(255, 255, 255, 0);
position: fixed;
bottom: 0;
left: 0;
z-index: 1000;
font-family: 'Roboto';
font-size: 14px;
width: 20px;
transition: all 0.3s ease;
white-space: nowrap;
border-radius: 5px 10px 10px 5px;
}
.anchor-menu ul {
list-style: none;
padding: 0.2rem 0.5rem 0.2rem 0;
margin: 0;
}
.anchor-menu li {
position: relative;
cursor: pointer;
}
.anchor-menu li:hover {
font-weight: bold;
}
.anchor-menu span {
padding-left: 0.5rem;
display: inline-block;
transform: translateX(-100%);
transition: all 0.3s ease;
}
.anchor-menu li::after {
content: '';
background-color: #747474;
display: block;
width: 1rem;
height: 3px;
position: absolute;
top: 50%;
transition: all 0.3s ease 0.3s;
}
.anchor-menu li.active {
color: #2176ff;
font-weight: bold;
}
.anchor-menu li.active::after {
background-color: #2176ff;
width: 2rem;
}
.anchor-menu:hover {
width: 150px;
transition: all 0.3s ease 0.3s;
background-color: rgba(255, 255, 255, 0.7);
}
.anchor-menu:hover span {
transform: translateX(0%);
transition: transform 0.3s ease 0.3s;
}
.anchor-menu:hover li::after {
transform: translateX(-100%);
transition: all 0.3s ease;
}
@media only screen and (max-width: 900px) {
.anchor-menu {
display: none;
}
}
var $ = jQuery
$(document).ready(function(){
var sections = $('h1, h2').map(function(){
return {
title: $(this).text(),
el: $(this)
}
}).get()
if(sections[0].title !== 'aetherAI') return
var body = $('html, body')
var anchorContent = '<ul>' + sections.map(function(section){return '<li><span>' + section.title + '</span></li>'}).join('') + '</ul>'
$('<div class="anchor-menu"/>').appendTo('body').html(anchorContent)
sections.forEach(function(section){
section.position = section.el.offset().top
})
$(document).on('click', '.anchor-menu li', function(){
var sectionIndex = $(this).index()
var position = sectionIndex === 0 ? 0 : sections[sectionIndex].el.offset().top - 32
body.stop().animate({scrollTop: position}, 500, 'swing')
})
$(document).scroll(function(){
var centerLine = body.scrollTop() + $(window).height() / 2
var found = false;
for(var i = 0; i < sections.length; i++) {
var section = sections[i]
if(section.el.offset().top > centerLine) {
$('.anchor-menu li').eq(i - 1).addClass('active').siblings('.active').removeClass('active')
found = true;
break;
}
}
if(!found) {
$('.anchor-menu li').eq(sections.length - 1).addClass('active').siblings('.active').removeClass('active')
}
}).scroll()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment