Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created October 29, 2017 17:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/f00d99fb69272fc2e0e4f4dd46e89f41 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/f00d99fb69272fc2e0e4f4dd46e89f41 to your computer and use it in GitHub Desktop.
const app = {
pages: [],
show: new Event('show'),
init: function(){
app.pages = document.querySelectorAll('.page');
app.pages.forEach((pg)=>{
pg.addEventListener('show', app.pageShown);
})
document.querySelectorAll('.nav-link').forEach((link)=>{
link.addEventListener('click', app.nav);
})
history.replaceState({}, 'Home', '#home');
window.addEventListener('popstate', app.poppin);
},
nav: function(ev){
ev.preventDefault();
let currentPage = ev.target.getAttribute('data-target');
document.querySelector('.active').classList.remove('active');
document.getElementById(currentPage).classList.add('active');
console.log(currentPage)
history.pushState({}, currentPage, `#${currentPage}`);
document.getElementById(currentPage).dispatchEvent(app.show);
},
pageShown: function(ev){
console.log('Page', ev.target.id, 'just shown');
let h1 = ev.target.querySelector('h1');
h1.classList.add('big')
setTimeout((h)=>{
h.classList.remove('big');
}, 1200, h1);
},
poppin: function(ev){
console.log(location.hash, 'popstate event');
let hash = location.hash.replace('#' ,'');
document.querySelector('.active').classList.remove('active');
document.getElementById(hash).classList.add('active');
console.log(hash)
//history.pushState({}, currentPage, `#${currentPage}`);
document.getElementById(hash).dispatchEvent(app.show);
}
}
document.addEventListener('DOMContentLoaded', app.init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment