Created
October 29, 2017 17:29
-
-
Save prof3ssorSt3v3/f00d99fb69272fc2e0e4f4dd46e89f41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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