简体   繁体   English

将 class 添加到元素以在 Z9E13B69D1D2DA927Z7777102ACAAAF7154A3 中运行 CSS animation

[英]Adding a class to an element to run CSS animation in Javascript

I'm trying to add my CSS animation class to an element when triggered by the js code.当由 js 代码触发时,我正在尝试将我的 CSS animation class 添加到元素中。 JS: JS:

 if (document.getElementById("user").value == "test"
   && document.getElementById("pass").value == "test")
    {
        alert( "Welcome back!" );
    }
    else {
        document.getElementById("fail").className += "animation";
    }

The animation won't play when the onclick event listener is triggered.当 onclick 事件监听器被触发时,animation 将不会播放。 The 'if' part of the statement works correctly but the 'else' part will not run.语句的“if”部分正常工作,但“else”部分不会运行。

CSS: CSS:

.animation {
    animation: shake 1s;

}

@keyframes shake {


      25% { transform: translate(10px)}
      50% { transform: translate(-10px)}
      75% { transform: translate(10px)}
      100% { transform: translate(-10px)}


}

Relevant HTML:相关 HTML:

                        <!--LOGIN-->

                    <div id="fail">

                        <p>My Account</p>

                        <p id="note">Username must be between 6 and 10 characters. <br/>Password must be at least 5 characters contain at least one letter and number.</p>
                        <form id="login">

                            <input type="text" placeholder="Username" id="user"/>

                            <br/>

                            <input type="password" placeholder="Password" id="pass"/>

                            <br/>

                            <input onclick="login()" type="submit" value="Submit">
                        </form>

                    </div>

change the submit input attribute type = button.更改提交输入属性 type = button。

submit will initiate a request and refresh the page, the animation will not be seen. submit 将发起请求并刷新页面,animation 将不会被看到。

U might be putting your script in your login function which submit the form as soon as clicked and reload the page.您可能会将您的脚本放在您的登录 function 中,一旦单击并重新加载页面就提交表单。 Try this script...试试这个脚本...

document.getElementById("login").addEventListener("submit", function (e) {
  e.preventDefault();
  if (
    document.getElementById("user").value == "test" &&
    document.getElementById("pass").value == "test"
  ) {
    alert("Welcome back!");
  } else {
    document.getElementById("fail").className += "animation";
  }
});

You need to define load and click events before to add the CSS class.您需要在添加 CSS class 之前定义loadclick事件。

Try it with the next code and provide feedback please.尝试使用下一个代码并提供反馈。

<script>
  addEventListener("load", eventClick);

  function eventClick() {
    document.getElementById("user").addEventListener("click", addClass);
  };

  function addClass() {
    var e = document.getElementById("user");
    e.classList.add("animation");
  };
</script>

ppcoding's solution does work, but in a lot of cases with forms, you do want them to submit—even if you aren't using the built in GET or POST methods for submissions. ppcoding 的解决方案确实有效,但在 forms 的很多情况下,您确实希望他们提交 — 即使您没有使用内置的 GET 或 POST 方法进行提交。

Especially for collaboration with other developers, using a <button> with type="submit" is better, so that it's clear that it submits the information.尤其是与其他开发者协作时,使用type="submit"<button>效果更好,这样提交信息就很清楚了。

When doing some kind of custom submission function, you should use preventDefault() on the event:在进行某种自定义提交 function 时,您应该在事件上使用preventDefault()

function login(e) {
  e.preventDefault()
  // ... continued code
}

Also, it's not recommended to use event listeners in the HTML.此外,不建议在 HTML 中使用事件侦听器。 It's better to put it in your JavaScript:最好把它放在你的 JavaScript 中:

<!-- HTML Before -->
<form>
  // other inputs
  <input onclick="login()" type="submit" value="Submit">
</form>

<!-- HTML After -->
<form id="login">
  // other inputs
  <button type="submit">Submit</button>
</form>

// JavaScript
const loginForm = document.querySelectorAll('#login')

loginForm.addEventListener('submit', e => {
  e.preventDefault()
  // ... login
})

I know there was already a solution, but for those that are looking to make their code more professional, this is how you do it.我知道已经有一个解决方案,但对于那些希望让他们的代码更专业的人来说,这就是你的做法。

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

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