How it works is, the back/home button allows you to go back to the previous page.
One of these would be clicked.
To go back to the previous page, this would be clicked.
How can the back/home button be improved?
https://jsfiddle.net/4m7yborc/
What would also need to be added to the code is for the video to stop if you click the home button while the video is playing.
Currently, if the video is playing and you click the home button, the video doesn’t shut off, it just keeps playing.
back/home button code:
function homeClickHandler(evt) {
const home = evt.currentTarget;
showHome(home);
}
function showHome() {
const theActive = document.querySelector(".with-curtain.active");
const theHides = document.querySelectorAll(".hide");
const theBody = document.querySelector("body");
theActive.classList.remove("active");
theHides.forEach(function(removeHide) {
removeHide.classList.remove("hide");
});
void theBody.offsetWidth; //restart animation
theBody.classList.toggle("bodytoggle");
}
function addClickToHome(goHome) {
goHome.forEach(function addEventHandler(goHome) {
goHome.addEventListener("click", homeClickHandler);
});
}
This was added:
const goHome = document.querySelectorAll('.home');
addClickToHome(goHome);
To here:
function init(selectors) {
config.containers = document.querySelectorAll(selectors.container);
const playButtons = document.querySelectorAll(selectors.playButton);
addClickToButtons(playButtons);
const goHome = document.querySelectorAll('.home');
addClickToHome(goHome);
}
Full Code:
const manageCover = (function makeManageCover() {
const config = {};
function show(el) {
el.classList.remove("hide");
}
function hide(el) {
el.classList.add("hide");
}
function hideAll(elements) {
elements.forEach(hide);
}
function showCovers(playButton) {
const cover = playButton.parentElement;
cover.classList.add("active");
show(cover);
}
function coverClickHandler(evt) {
hideAll(config.containers);
const cover = evt.currentTarget;
showCovers(cover);
}
function homeClickHandler(evt) {
const home = evt.currentTarget;
showHome(home);
}
function showHome() {
const theActive = document.querySelector(".with-curtain.active");
const theHides = document.querySelectorAll(".hide");
const theBody = document.querySelector("body");
theActive.classList.remove("active");
theHides.forEach(function (removeHide) {
removeHide.classList.remove("hide");
});
void theBody.offsetWidth; //restart animation
theBody.classList.toggle("bodytoggle");
}
function addClickToButtons(playButtons) {
playButtons.forEach(function addEventHandler(playButton) {
playButton.addEventListener("click", coverClickHandler);
});
}
function addClickToHome(goHome) {
goHome.forEach(function addEventHandler(goHome) {
goHome.addEventListener("click", homeClickHandler);
});
}
function addCoverHandler(coverSelector, handler) {
const cover = document.querySelector(coverSelector);
cover.addEventListener("click", handler);
}
function init(selectors) {
config.containers = document.querySelectorAll(selectors.container);
const playButtons = document.querySelectorAll(selectors.playButton);
addClickToButtons(playButtons);
const goHome = document.querySelectorAll(".home");
addClickToHome(goHome);
}
return {
addCoverHandler,
init
};
}());
manageCover.init({
container: ".container",
playButton: ".thePlay"
});