70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
(function (){
|
|
var comfiture = {};
|
|
var activeLink;
|
|
var activeContent;
|
|
var wallpapers = [
|
|
'1.gif', '2.gif', '3.gif', '4.gif',
|
|
'5.gif', '6.gif', '7.gif', '8.gif',
|
|
'9.gif', '10.gif', '11.gif', '12.gif',
|
|
'13.gif', '14.gif', '15.gif', '16.gif',
|
|
'17.gif', '18.gif', '19.gif', '20.gif',
|
|
'21.gif', '22.gif', '23.gif',
|
|
'1.png', '2.png', '3.png',
|
|
'5.png', '6.png', '7.png', '8.png',
|
|
'9.png',
|
|
];
|
|
|
|
//Function to set the wallpaper
|
|
comfiture.setupWallpaper = function () {
|
|
let wallpaper = wallpapers[Math.floor(Math.random() * wallpapers.length)];
|
|
document.querySelector('.background').style.backgroundImage = 'url("backgrounds/'+wallpaper+'")';
|
|
};
|
|
|
|
//Onload
|
|
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);
|
|
|
|
//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.');
|
|
}
|
|
}
|
|
|
|
//Expose comfiture object to document
|
|
window.comfiture = comfiture;
|
|
|
|
})(); |