【发布时间】:2022-01-01 13:53:09
【问题描述】:
这两个全局变量都在代码中起作用。
如果两者都在代码中工作,你怎么知道哪个是正确的?
有人告诉我这些是正确的,但你怎么知道该用哪一个?
如果两者都在代码中工作?
我也被告知这些绝对不会在引擎盖下做同样的事情。虽然代码可能会运行,并且似乎在两个 JSFiddle 版本中都可以运行,但实际上并没有。
谁能帮我理解一下,哪个是正确的,为什么?
let currentPlayButton = {};
https://jsfiddle.net/n5cpvtok/
let currentPlayButton = “”;
https://jsfiddle.net/n5cpvtok/1/
let currentPlayButton;
https://jsfiddle.net/bo5jm2r6/
我看不出有什么不同。
当点击 svg 时,它们会淡出。
这是预期的行为。
const manageCover = (function makeManageCover() {
const config = {};
const body = document.body;
let currentPlayButton = {};
function show(el) {
el.classList.remove("hide");
}
function hide(el) {
el.classList.add("hide");
}
function hideAll(elements) {
elements.forEach(hide);
}
function resetBackground(backgroundSelector) {
const allBackgrounds = document.querySelectorAll(backgroundSelector);
function hideBackground(background) {
background.classList.add("bg1");
}
allBackgrounds.forEach(hideBackground);
}
function resetButtons(buttonSelector) {
const allButtons = document.querySelectorAll(buttonSelector);
function hideButton(button) {
button.classList.add("isOpen");
}
allButtons.forEach(hideButton);
}
function resetPage() {
resetBackground("body");
resetButtons(".outer");
}
function markAsPlayed(played) {
played.classList.add("played");
}
function showCover(playButton) {
hideAll(config.containers);
resetPage();
markAsPlayed(playButton);
const cover = playButton.parentElement;
cover.classList.add("active");
show(cover);
}
function animationEndHandler(evt) {
const animationName = evt.animationName;
if (animationName === "initial-fade") {
body.classList.remove("initial-fade");
showCover(currentPlayButton);
}
}
function coverClickHandler(evt) {
currentPlayButton = evt.currentTarget;
body.classList.add("initial-fade");
}
function addClickToButtons(playButtons) {
playButtons.forEach(function playButtonHandler(playButton) {
playButton.addEventListener("click", coverClickHandler);
});
}
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);
body.addEventListener("animationend", animationEndHandler);
}
return {
addCoverHandler,
init
};
}());
【问题讨论】:
-
什么是
showCover?coverClickHandler在哪里调用? -
请在问题本身中发布所有相关代码,而不仅仅是指向它的链接。
标签: javascript string object global-variables