CSS过渡为什么只在第二次点击时有效?

huangapple go评论60阅读模式
英文:

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(&quot;#last&quot;);
  button.addEventListener(&quot;click&quot;, function() {
    var layers = document.querySelectorAll(&quot;.bottom-layer&quot;);
    for (const layer of layers) {
      setTimeout(switchVisible, 900);
      layer.classList.toggle(&quot;active&quot;);
    }
  });
}

function switchVisible() {
  if (document.getElementById(&quot;main-cont&quot;)) {
    if (document.getElementById(&quot;main-cont&quot;).style.display == &quot;none&quot;) {
      document.getElementById(&quot;main-cont&quot;).style.display = &quot;block&quot;;
      document.getElementById(&quot;overlay-cont&quot;).style.display = &quot;none&quot;;
    } else {
      document.getElementById(&quot;main-cont&quot;).style.display = &quot;none&quot;;
      document.getElementById(&quot;overlay-cont&quot;).style.display = &quot;block&quot;;
    }
  }
}

<!-- 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 -->

&lt;button onclick=&quot;transitionEffect()&quot; id=&quot;last&quot;&gt; click&lt;/button&gt;
&lt;div id=&quot;main-cont&quot;&gt;
  &lt;h3&gt;first page&lt;/h3&gt;
&lt;/div&gt;
&lt;div id=&quot;overlay-cont&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-6 overlay-text&quot; id=&quot;overlay-col&quot;&gt;
      &lt;h1&gt; next page&lt;/h1&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-md-6&quot; id=&quot;overlay-col&quot;&gt;

    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;transition-cont&quot;&gt;
  &lt;div class=&quot;bottom-layer&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;bottom-layer bottom-layer--2&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;bottom-layer bottom-layer--3&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

View on CodePen

答案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(&quot;.bottom-layer&quot;);
  for (const layer of layers) {
    setTimeout(switchVisible, 900);
    layer.classList.toggle(&quot;active&quot;);
  }
}

答案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 calls transitionEffect()
  • 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(&quot;.bottom-layer&quot;);
    for (const layer of layers) {
      setTimeout(switchVisible, 900);
      layer.classList.toggle(&quot;active&quot;);
    }
}

huangapple
  • 本文由 发表于 2023年5月29日 00:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定