点击按钮在事件完成后。

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

Click button after event finished

问题

以下是翻译好的部分:

为什么在以下代码中我无法以编程方式单击按钮来再次翻译div

<div class="div1"></div>
<button onclick="play()">播放</button>
.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  transition: transform 1s;
  transform: translateX(100px);
}
const div1 = document.getElementsByClassName("div1")[0];

div1.addEventListener("transitionend", callback);
// 为什么这个不起作用?
// div1.addEventListener("transitionend", play);

function play() {
  div1.classList.add("play");
}

function callback() {
  div1.classList.remove("play");
  // 或者这个?
  // play();
}
英文:

Why in the following code I can not click the button programmatically to translate the div again ?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const div1 = document.getElementsByClassName(&quot;div1&quot;)[0];

div1.addEventListener(&quot;transitionend&quot;, callback);
// Why this one does not work?
// div1.addEventListener(&quot;transitionend&quot;, play);

function play() {
  div1.classList.add(&quot;play&quot;);
}

function callback() {
  div1.classList.remove(&quot;play&quot;);
  // or this one?
  // play();
}

<!-- language: lang-css -->

.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  transition: transform 1s;
  transform: translateX(100px);
}

<!-- language: lang-html -->

&lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
&lt;button onclick=&quot;play()&quot;&gt;Play&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

没有立即改变类别的效果。

这就是为什么您应该使用 setTimeOut 来再次调用 play 的原因。

const div1 = document.getElementsByClassName("div1")[0];

div1.addEventListener("transitionend", callback);

function play() {
  div1.classList.add("play");
}

function callback() {
  div1.classList.remove("play");
  setTimeout(play);
}

您也可以使用 CSS 动画以更清晰的代码获得相同的效果。

const div1 = document.getElementsByClassName("div1")[0];

function play() {
  div1.classList.add("play");
}

/* CSS 部分未翻译 */

希望这对您有所帮助。

英文:

there is no effect to changing the class instantly.

that is why you should use setTimeOut to call play again

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const div1 = document.getElementsByClassName(&quot;div1&quot;)[0];

div1.addEventListener(&quot;transitionend&quot;, callback);

function play() {
  div1.classList.add(&quot;play&quot;);
}

function callback() {
  div1.classList.remove(&quot;play&quot;);
  setTimeout(play);
}

<!-- language: lang-css -->

.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  transition: transform 1s;
  transform: translateX(100px);
}

<!-- language: lang-html -->

&lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
&lt;button onclick=&quot;play()&quot;&gt;Play&lt;/button&gt;

<!-- end snippet -->

you might also use css animation to get the same effect with cleaner code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const div1 = document.getElementsByClassName(&quot;div1&quot;)[0];

function play() {
  div1.classList.add(&quot;play&quot;);
}

<!-- language: lang-css -->

.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  animation: go-right 1s infinite;
}

@keyframes go-right {
  100%{
    transform: translateX(100px);
  }
}

<!-- language: lang-html -->

&lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
&lt;button onclick=&quot;play()&quot;&gt;Play&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

当您单击“播放”按钮时,将调用play函数并将“play”类添加到div1。这会触发一个过渡,当过渡结束时,会调用回调函数,该函数会移除“play”类。

如果您希望通过按钮点击来切换翻译开/关,请修改play函数以根据其是否存在来添加或移除“play”类。

function play() {
  if (div1.classList.contains("play")) {
    div1.classList.remove("play");
  } else {
    div1.classList.add("play");
  }
}
英文:

When you click the "Play" button, the play function is called and it adds the "play" class to div1. This triggers a transition and when the transition ends, the callback function is called, which removes the "play" class.

If you want to toggle the translation on/off with button click modify the play function to add or remove the "play" class based on whether it's there

function play() {
  if (div1.classList.contains(&quot;play&quot;)) {
    div1.classList.remove(&quot;play&quot;);
  } else {
    div1.classList.add(&quot;play&quot;);
  }
}

答案3

得分: 0

你应该在去除play类后停止动画,然后在零延迟后(允许浏览器重新渲染并重新启动动画)再次添加play类:

if (div1.classList.contains('play')) {
  div1.classList.remove('play');
  setTimeout(() => div1.classList.add('play'));
  return;
}
const div1 = document.getElementsByClassName("div1")[0];

div1.addEventListener("transitionend", callback);

function play() {
  if (div1.classList.contains('play')) {
    div1.classList.remove('play');
    setTimeout(() => div1.classList.add('play'));
    return;
  }
  div1.classList.add("play");
}

function callback() {
  div1.classList.remove("play");
}
.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  transition: transform 1s;
  transform: translateX(100px);
}
<div class="div1"></div>
<button onclick="play()">Play</button>
英文:

You should stop the animation with removing the play class and start it again with adding the play class after a zero timeout (that allows the browser to re-render and start the animation fresh):

  if(div1.classList.contains(&#39;play&#39;)){
    div1.classList.remove(&#39;play&#39;);
    setTimeout(()=&gt;div1.classList.add(&#39;play&#39;));
    return;
  }

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const div1 = document.getElementsByClassName(&quot;div1&quot;)[0];

div1.addEventListener(&quot;transitionend&quot;, callback);
// Why this one does not work?
// div1.addEventListener(&quot;transitionend&quot;, play);

function play() {
  if(div1.classList.contains(&#39;play&#39;)){
    div1.classList.remove(&#39;play&#39;);
    setTimeout(()=&gt;div1.classList.add(&#39;play&#39;));
    return;
  }
  div1.classList.add(&quot;play&quot;);
  
}

function callback() {
  div1.classList.remove(&quot;play&quot;);
  // or this one?
  // play();
}

<!-- language: lang-css -->

.div1 {
  width: 200px;
  height: 100px;
  background-color: limegreen;
}

.play {
  transition: transform 1s;
  transform: translateX(100px);
}

<!-- language: lang-html -->

&lt;div class=&quot;div1&quot;&gt;&lt;/div&gt;
&lt;button onclick=&quot;play()&quot;&gt;Play&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月10日 17:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874467.html
匿名

发表评论

匿名网友

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

确定