Skip to content

Instantly share code, notes, and snippets.

@c7x43t
Last active June 24, 2021 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c7x43t/02100f5cdc08fe653f44c85c8142c90a to your computer and use it in GitHub Desktop.
Save c7x43t/02100f5cdc08fe653f44c85c8142c90a to your computer and use it in GitHub Desktop.
// scroll to id tagged element when there is a fixed sized header bar
function executeDelayed(f,...args){
Promise.resolve().then(function(){
window.requestAnimationFrame(function(){
f.call(this,...args);
});
});
}
var queue=[];
window.addEventListener(/*"DOMContentLoaded"*/"load", processQueue);
function processQueue(){
for(var e of queue) e.f.call(e.context,...e.args)
}
function executeOnLoad(f,...args){
if(document.readyState === "complete" /*|| document.readyState === "interactive"*/) {
processQueue();
f.call(this,...args);
}else{
queue.push({f: f,args: args,context: this});
}
}
var queue2=[];
function processQueue2(){
for(var e of queue2) e.f.call(e.context,...e.args)
}
function isPreloaderActive(){ // this is theme specific
return document.querySelector(".preloader").style.display!=="none";
}
function waitForPreloadInactive(){
if(isPreloaderActive()){
window.requestAnimationFrame(waitForPreloadInactive);
}else{
processQueue2();
}
}
function executeAfterPreloader(f,...args){
if(!isPreloaderActive()) {
processQueue2();
f.call(this,...args)
}else{
queue2.push({f: f,args: args,context: this});
}
}
////
var queue3=[];
function processQueue3(){
for(var e of queue3) e.f.call(e.context,...e.args)
}
var _previousHash=window.location.hash;
function executeAfterHashChange(f,...args){
queue3.push({f: f,args: args,context: this});
}
window.addEventListener('hashchange', processQueue3, false);
////
var queue4=[];
function processQueue4(){
for(var e of queue4) e.f.call(e.context,...e.args)
}
//var _previousHash=window.location.hash;
function executeAfterPopstate(f,...args){
queue4.push({f: f,args: args,context: this});
}
window.addEventListener('popstate', processQueue4, false);
////
function isOnUeberUnsPage(){
var pathname=window.location.pathname;
return /ueber-uns/.exec(pathname)!==null;
}
function scrollToHash(){
executeDelayed(function(){
var hash=window.location.hash;
if(isOnUeberUnsPage()&&hash){
var element=document.querySelector(hash);
var rect=element.getBoundingClientRect();
var header=document.querySelector("#fixedNavigation");
var headerRect=header.getBoundingClientRect();
var positionY=rect.y+window.scrollY-rect.height-headerRect.height;
// console.log({rect:rect,headerRect:headerRect, scrollY:window.scrollY,positionY:positionY});
window.scrollTo(0, positionY);
}
});
}
waitForPreloadInactive();
executeOnLoad(function(){
executeAfterPreloader(scrollToHash);
});
document.body.addEventListener('click',function(event){
var target=event.target;
if(target.tagName==="A"&&isOnUeberUnsPage()&&/ueber-uns/.exec(target.href)!==null){
console.log(target.href);
// Fix same href triggering on click, because it does not trigger
// hashChange or popstate events
if(window.location.hash===(new URL(target.href)).hash){
console.log('same href trigger')
executeDelayed(scrollToHash);
}else{
executeAfterHashChange(scrollToHash);
}
//executeAfterPopstate(scrollToHash);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment