英文:
why is css transition only working with second click
问题
这是您提供的代码的翻译部分:
这不是双击,只需要第二次点击才能工作。我尝试将setTimeout放在循环外面,只有当它从下一页切换到第一页时才有效,在codepen中它会改回默认状态。
功能transitionEffect() {
const button = document.querySelector("#last");
button.addEventListener("click", function() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
});
}
功能switchVisible() {
if (document.getElementById("main-cont")) {
if (document.getElementById("main-cont").style.display == "none") {
document.getElementById("main-cont").style.display = "block";
document.getElementById("overlay-cont").style.display = "none";
} else {
document.getElementById("main-cont").style.display = "none";
document.getElementById("overlay-cont").style.display = "block";
}
}
}
#overlay-cont {
display: none;
z-index: 1;
}
.bottom-layer {
position: absolute;
width: 100%;
height: 100%;
top: 100%;
left: 0;
bottom: auto;
right: auto;
background: #48466d;
transition: all 0.7s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.bottom-layer.active {
top: -100%;
}
.bottom-layer--2 {
background: #ecf3a3;
transition-delay: 0.12s;
}
.bottom-layer--3 {
background: #95a792;
transition-delay: 0.4s;
}
<button onclick="transitionEffect()" id="last">点击</button>
<div id="main-cont">
<h3>第一页</h3>
</div>
<div id="overlay-cont">
<div class="row">
<div class="col-md-6 overlay-text" id="overlay-col">
<h1>下一页</h1>
</div>
<div class="col-md-6" id="overlay-col">
</div>
</div>
</div>
<div class="transition-cont">
<div class="bottom-layer"></div>
<div class="bottom-layer bottom-layer--2"></div>
<div class="bottom-layer bottom-layer--3"></div>
</div>
希望这能帮助您理解代码的翻译。如果您有任何其他问题,请随时提出。
英文:
It is not even double click, it just requires second click to work. I tried putting the setTimeout outside of the loop and it works only when it goes from next page to first page, in codepen its changing back to default.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function transitionEffect() {
const button = document.querySelector("#last");
button.addEventListener("click", function() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
});
}
function switchVisible() {
if (document.getElementById("main-cont")) {
if (document.getElementById("main-cont").style.display == "none") {
document.getElementById("main-cont").style.display = "block";
document.getElementById("overlay-cont").style.display = "none";
} else {
document.getElementById("main-cont").style.display = "none";
document.getElementById("overlay-cont").style.display = "block";
}
}
}
<!-- language: lang-css -->
#overlay-cont {
display: none;
z-index: 1;
}
.bottom-layer {
position: absolute;
width: 100%;
height: 100%;
top: 100%;
left: 0;
bottom: auto;
right: auto;
background: #48466d;
transition: all 0.7s cubic-bezier(0.645, 0.045, 0.355, 1);
}
.bottom-layer.active {
top: -100%;
}
.bottom-layer--2 {
background: #ecf3a3;
transition-delay: 0.12s;
}
.bottom-layer--3 {
background: #95a792;
transition-delay: 0.4s;
}
<!-- language: lang-html -->
<button onclick="transitionEffect()" id="last"> click</button>
<div id="main-cont">
<h3>first page</h3>
</div>
<div id="overlay-cont">
<div class="row">
<div class="col-md-6 overlay-text" id="overlay-col">
<h1> next page</h1>
</div>
<div class="col-md-6" id="overlay-col">
</div>
</div>
</div>
<div class="transition-cont">
<div class="bottom-layer"></div>
<div class="bottom-layer bottom-layer--2"></div>
<div class="bottom-layer bottom-layer--3"></div>
</div>
</div>
<!-- end snippet -->
答案1
得分: 2
你正在在点击事件处理程序内部附加点击监听器。因此,您正在等待第一次点击来开始监听点击事件并开始响应它们。
为了使其工作,您需要重写您的点击处理程序。
function transitionEffect() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
}
英文:
You are attaching your click listener inside click event handler. Therefore you are waiting for 1st click to start listening for clicks and start responding to them.
In order to make it work you need to rewrite your click handler
function transitionEffect() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
}
答案2
得分: 1
你代码中的问题如下:
- 你给按钮添加了一个
onclick
处理程序,它调用了transitionEffect()
。 - 你的
transitionEffect
函数没有执行效果;相反,它添加了一个点击按钮的事件处理程序。 - 结果是,第一次点击除了设置一个处理程序外什么都没做。
- 此外,每次后续点击都会添加一个额外的事件处理程序。例如,点击6次后,会有6个事件处理程序,因此下次点击时,
active
类会切换6次,导致没有可见的变化。
以下是一个简单的修复方法,它移除了 transitionEffect
函数的 addEventListener
包装:
function transitionEffect() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
}
英文:
The bug in your code is the following:
- You gave the button an
onclick
handler that callstransitionEffect()
- Your
transitionEffect
function didn't execute the effect; instead, it added an event handler to the click button. - As a result, the first click didn't do anything, except set a handler.
- Furthermore, every subsequent click added an additional event handler. After clicking 6 times, for example, there were 6 event handlers, so the next time you click, the
active
classes toggled 6 times, resulting in no visible change.
Here's a simple fix, which removes the addEventListener
wrapper of the transitionEffect
function:
function transitionEffect() {
var layers = document.querySelectorAll(".bottom-layer");
for (const layer of layers) {
setTimeout(switchVisible, 900);
layer.classList.toggle("active");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论