comfitu.re/script.js

65 lines
2 KiB
JavaScript

console.debug('load');
(function (){
console.debug('anonymous');
var activeLink;
var activeContent;
var wallpapers = [
'1.gif', '2.gif', '3.gif', '4.gif',
'5.gif', '6.gif', '7.gif', '8.gif',
'9.gif',
'1.png', '2.png', '3.png', '4.png',
'5.png', '6.png', '7.png', '8.png',
'9.png',
];
window.addEventListener('load', function(){
//Hide all content
let contents = document.querySelectorAll('.content');
contents.forEach(function (element) {
element.classList.add('hide');
});
//Activate content matching hash in url (or home instead)
let hash = window.location.hash;
if(!hash) hash = '#home';
switchActiveContent(hash);
//Set random wallpaper
let wallpaper = wallpapers[Math.floor(Math.random() * wallpapers.length)];
document.querySelector('.background').style.backgroundImage = 'url("backgrounds/'+wallpaper+'")';
//Add click event handlers for header/footer links
window.addEventListener("click", event => {
var elements = document.querySelectorAll('header a, footer a');
elements.forEach(element => {
if (element && element.contains(event.target)) {
let hash = element.getAttribute('href');
switchActiveContent(hash);
}
});
});
});
function switchActiveContent(id)
{
let link = document.querySelector('a[href="'+id+'"]');
let content = document.querySelector(id);
if(link && content) {
if(activeLink) activeLink.classList.remove('active');
if(activeContent) activeContent.classList.add('hide');
link.classList.add('active');
content.classList.remove('hide');
activeLink = link;
activeContent = content;
} else {
console.error('Error: not switching content; link or content element for id ' + id + ' does not exist.');
}
}
})();