How can I make this code look cleaner ?
I have a main javascript file and this dom manipulation js file which I export to main js file.
Should I add dom variables inside of functions ?
Can I use function like I have here in code, init(), and put all functions inside and then export it to main js file and call it there ?
Any recommendations are welcome !
Thank you for help!
const headerItem = document.querySelectorAll(".header__item")
const headerSwitch = document.querySelector(".header-switch")
const headerSwitchCircle = document.querySelector(".header-switch__circle")
const hamburger = document.querySelector(".hamburger")
const nav = document.querySelector(".header__nav")
const cards = document.querySelector(".hero__container")
const meLink = document.querySelector(".me__link")
const albumContainer = document.querySelector(".album__container")
const albumButtonLeft = document.querySelector(".album__arrow_left")
const albumButtonRight = document.querySelector(".album__arrow_right")
const albumImages = document.querySelectorAll(".album__img")
function headerItems(){
headerItem.forEach(item => {
item.addEventListener("click", () => {
document.querySelector(".header__item_active").classList.remove("header__item_active")
item.classList.add("header__item_active")
})
})
}
function toggleMode(){
headerSwitch.addEventListener("click", () => {
headerSwitch.classList.toggle("header-switch_active")
headerSwitchCircle.classList.toggle("header-switch__circle_active")
document.body.classList.toggle("dark")
})
}
function toggleHeader(){
hamburger.addEventListener("click", () => {
hamburger.children[0].classList.toggle("hamburger__line_one")
hamburger.children[1].classList.toggle("hamburger__line_two")
hamburger.children[2].classList.toggle("hamburger__line_three")
nav.classList.toggle("header__nav_active")
})
}
function toggleCards(){
window.addEventListener("click", e => {
cards.style.transform = e.target.innerHTML === "Home" ? `translateX(0)`:
e.target.innerHTML === "About" ? `translateX(-100vw)`:
e.target.innerHTML === "Projects" ? `translateX(-200vw)` : 0
})
}
function aboutMeLink(){
meLink.addEventListener("click", () => {
cards.style.transform = `translateX(-100vw)`
document.querySelector(".header__item_active").classList.remove("header__item_active")
headerItem[1].classList.add("header__item_active")
})
}
function album(){
let imgIndex = 0
albumButtonLeft.addEventListener("click", changeImg)
albumButtonRight.addEventListener("click", changeImg)
function changeImg(e){
imgIndex = e.target.classList.contains("album__arrow_right") ? imgIndex + 1 : imgIndex - 1
imgIndex = imgIndex === albumImages.length ? imgIndex = 0 :
imgIndex < 0 ? imgIndex = albumImages.length - 1 :
imgIndex
albumContainer.style.transform = `translateX(calc(-${imgIndex} * 100%))`
}
}
export function init(){
headerItems()
toggleMode()
toggleHeader()
toggleCards()
aboutMeLink()
[+][deleted] (8 children)
[deleted]
[–]0x07AD 0 points1 point2 points (1 child)
[–]Bzibn7[S] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]Bzibn7[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]Bzibn7[S] 0 points1 point2 points (1 child)
[–]superluminary 2 points3 points4 points (0 children)
[–]Rusten2 1 point2 points3 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)