Created
November 15, 2019 10:49
-
-
Save mmgj/8cf1bfcf982f827f77e14e9fb11018c5 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
// Ideally you would import the Jump library explicitly, but don't sweat it. ;) | |
const jump = Jump; | |
// ⬇ Moved this up here as there is no reason for the variable to be set to undefined first. | |
const imageArray = [ | |
{ | |
image: 'img/mattias@400px.png', | |
description: | |
'Hemsida för content marketing specialisten och tidigare SVT-sportjournalist Mattias Brännholm. Wordpress.', | |
}, | |
{ | |
image: 'img/stildans@400px.jpg', | |
description: | |
'Hemsida för vår dansträningsgrupp. Htlm, css, php. Det finns funktioner för att boka tid för varje danspass.', | |
}, | |
{ | |
image: 'img/brama@400px.png', | |
description: 'Webbredaktör för Brama content & Film. Wordpress.', | |
}, | |
{ | |
image: 'img/brf-malarglimt@400px.png', | |
description: 'Hemsida för vår bostadsrättsförening. Wordpress.', | |
}, | |
{ | |
image: 'img/christinaak@400px.jpg', | |
description: 'Webbredaktör för Christinaakademin. Wordpress.', | |
}, | |
]; | |
let activated = false; // Never used. Remove. | |
let currentIndex; | |
let currentPicture; | |
const button1 = document.querySelector('.previous'); | |
const button2 = document.querySelector('.next'); | |
// ⬇ I changed this to avoid potentially confusing reuse of a variable name. | |
// Also moved it up to keep the "setup" stuff in one place. | |
const menuBtn = document.querySelector('#menu-btn'); | |
const fadeLayer = document.querySelector('.fade-layer'); | |
menuBtn.addEventListener('click', toggleMenu); | |
fadeLayer.addEventListener('click', toggleMenu); | |
function toggleMenu() { | |
let element = document.querySelector('nav'); | |
element.classList.toggle('menu-hidden'); | |
element = document.querySelector('.fade-layer'); | |
element.classList.toggle('hidden'); | |
} | |
function toggleSections() { | |
let section = document.querySelector('.content'); | |
let icon = document.querySelector('.collapsible'); | |
if (section.style.display === 'block') { | |
section.style.display = 'none'; | |
icon.style.transform = 'rotate(0deg)'; | |
} else { | |
section.style.display = 'block'; | |
icon.style.transform = 'rotate(180deg)'; | |
} | |
} | |
// function setAttribute(){ | |
// return image | |
// } | |
function formatText(textObject) { | |
return textObject; | |
} | |
function update() { | |
let text = document.querySelector('.description'); | |
text.innerText = formatText(currentPicture.description); | |
let images = document.querySelector('.gallery-box img'); | |
images.setAttribute('src', currentPicture.image); | |
} | |
function fadeOut() { | |
let images = document.querySelector('.gallery-box img'); | |
images.classList.add('fade'); | |
} | |
function fadeIn() { | |
let images = document.querySelector('.gallery-box img'); | |
images.classList.remove('fade'); | |
} | |
function previous() { | |
currentIndex--; | |
if (currentIndex < 0) { | |
currentIndex = imageArray.length - 1; | |
} | |
currentPicture = imageArray[currentIndex]; | |
fadeOut(); | |
setTimeout(function() { | |
update(); | |
fadeIn(); | |
}, 500); | |
} | |
function next() { | |
// 'image' is never used again. Remove. | |
let image = document.querySelector('.gallery-box img'); | |
currentIndex++; | |
if (currentIndex >= imageArray.length) { | |
currentIndex = 0; | |
} | |
currentPicture = imageArray[currentIndex]; | |
fadeOut(); | |
setTimeout(function() { | |
update(); | |
fadeIn(); | |
}, 500); | |
} | |
function onClick() { | |
jump('.top', { | |
duration: 500, | |
offset: 0, | |
callback: undefined, | |
a11y: false, | |
}); | |
} | |
let buttonToTop = document.querySelector('.tothetop'); | |
buttonToTop.addEventListener('click', onClick); | |
// Now all the setup is above this line, while all the execution is below. Nice and tidy. :) | |
if (window.location.pathname == '/index.html') { | |
// From what I can see this code is never called. | |
// Is this working as expected? If so, consider moving the functions outside the if-scope. | |
// Ask me if you don't know what and how. :) | |
function sendEmail(element, message) { | |
alert(message); | |
element.focus(); | |
} | |
function isMailReady(form) { | |
let passed = false; | |
if ( | |
form.email.value.indexOf('@') == -1 || | |
form.email.value.indexOf('.') == -1 | |
) { | |
sendEmail(form.email, 'Ange en giltig e-postadress.'); | |
} else if (form.message.value == '') { | |
sendEmail(form.message, 'Du har inte skrivit någonting.'); | |
} else { | |
passed = true; | |
} | |
return passed; | |
} | |
} else if (window.location.pathname == '/arbetsprover.html') { | |
// ⬇ Changed to sexy arrow functions because that's what the cool kids do. | |
button2.addEventListener('click', () => next()); | |
button1.addEventListener('click', () => previous()); | |
// This variable is never used again? Remove. | |
let galleryElements = document.querySelector('.gallerybox'); | |
currentIndex = 0; | |
currentPicture = imageArray[currentIndex]; | |
update(); | |
let sections = document.querySelector('.collapsible'); | |
sections.addEventListener('click', toggleSections); | |
} | |
// And finally, all the calls to external scripts go here. | |
jarallax(document.querySelectorAll('.jarallax'), { | |
speed: 0.3, | |
}); | |
jarallax(document.querySelectorAll('.jarallax-video'), { | |
speed: 0.2, | |
videoSrc: 'https://vimeo.com/372669055', | |
}); | |
tippy('button', { | |
content: 'Global content', | |
animation: 'rotate', | |
theme: 'tomato', | |
duration: 3, | |
arrow: true, | |
delay: [500, 200], | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment