简体   繁体   English

单击div上的一个按钮来翻转div

[英]Flipping a div with the click of a button on the div

I am trying to teach myself HTML, CSS and JS and started to play around on codepen.io. 我试图自学HTML,CSS和JS,并开始在codepen.io上玩。 I've been in a rut, trying to get the div (on this pen http://codepen.io/janicedarling/pen/MwNZVp ) to flip when the button "create account" or "submit" is clicked. 我一直很生气,试图让div(在这支笔上http://codepen.io/janicedarling/pen/MwNZVp )在单击“创建帐户”或“提交”按钮时翻转。 Currently using this tutorial http://callmenick.com/post/css-transitions-transforms-animations-flipping-card I am able to get the div to flip when anywhere is clicked. 当前使用本教程http://callmenick.com/post/css-transitions-transforms-animations-flipping-card我可以使div在单击任何位置时翻转。 I tried changing this in the javascript 我试图在javascript中更改此设置
document.querySelectorAll(".tab");
to
document.querySelector("#create");
but then nothing happens still. 但随后仍然没有任何反应。 I left the CSS in place. 我将CSS留在原地。 Can anyone offer any advice on how to get the required transformation? 谁能提供有关如何进行所需转换的建议?

First, add an id to your submit button: 首先,向您的“提交”按钮添加一个id

    <input id="signup" type="submit" placeholder="Go">

Next, let's fix your JS. 接下来,让我们修复您的JS。 You don't need a bunch of code from that other example that looks for all the different cards to wire up. 您不需要其他示例中的一堆代码即可寻找所有不同的卡。 In your case, you have only one card, and you need only two event callbacks (one to flip and the other to un-flip). 在您的情况下,您只有一张卡,并且只需要两个事件回调(一个用于翻转,另一个用于取消翻转)。 We use the new id here to find the specific button that causes the un-flip. 我们在此处使用新的ID查找导致翻转的特定按钮。

(function() {
  var tab = document.querySelector('.tab');

  document.getElementById('create').addEventListener('click', function() {
    tab.classList.add('flipped');
  }, false);

  document.getElementById('signup').addEventListener('click', function() {
    tab.classList.remove('flipped');
  }, false);

})();

With this, two specific buttons are wired up to the flip and un-flip actions, so the card only flips when you want it to. 这样,两个特定的按钮就可以连接到翻转和取消翻转操作,因此卡仅在需要时翻转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM