JQuery动画工作不正确

时间:2017-12-31 12:23:00

标签: javascript jquery

我有3个表格,其中2个隐藏。我按链接将一个可见表单更改为其他表单。因此,当我更改活动表单时,我需要制作扩展或缩小的动画。现在我有了这个,但它无法正常工作:

$('.message a').click(function(){
       $('.form form').addClass('hidden');
       var form = $(this).attr('for');
       $('.' + form + '').animate({ height: "toggle", opacity: "toggle"}, "slow").removeClass('hidden');
   });

如果我使用:

$('.message a').click(function(){
       $('.form form').addClass('hidden');
       var form = $(this).attr('for');
       $('.' + form + '').removeClass('hidden');
   });

它工作正常,但不制作动画。 我该如何解决?谢谢,祝你新年快乐!

$('.message a').click(function(){
       $('.form form').addClass('hidden');
       var form = $(this).attr('for');
       $('.' + form + '').removeClass('hidden');
   });
body {
 font-family: "Roboto", sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
}

.login-page {
  width: 360px;
  padding: 8% 0 0;
  margin: auto;
  position: relative; top: 25px; left: 0; right: 0; bottom: 25px;
}
.form {
  position: relative;
  z-index: 1;
  background: #FFFFFF;
  max-width: 360px;
  margin: 0 auto 100px;
  padding: 45px;
  text-align: center;
  box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.2), 0 1px 1px 0 rgba(0, 0, 0, 0.24);
}
.form input {
  font-family: "Roboto", sans-serif;
  outline: 0;
  background-color: #f2f2f2;
  width: 100%;
  border: 0;
  margin: 0 0 15px;
  padding: 15px;
  box-sizing: border-box;
  font-size: 14px;

}

.form button {
  font-family: "Roboto", sans-serif;
  text-transform: uppercase;
  outline: 0;
  background: #5d98cc;
  width: 100%;
  border: 0;
  padding: 15px;
  color: #FFFFFF;
  font-size: 14px;
  -webkit-transition: all 0.3 ease;
  transition: all 0.3 ease;
  cursor: pointer;
}
.form button:hover,.form button:active,.form button:focus {
  background: #397eba;
}
.form .message {
  margin: 15px 0 0;
  color: #b3b3b3;
  font-size: 12px;
}
.form .message a {
  color: #5d98cc;
  text-decoration: none;
}

.container {
  position: relative;
  z-index: 1;
  max-width: 300px;
  margin: 0 auto;
}
.container:before, .container:after {
  content: "";
  display: block;
  clear: both;
}
.container .info {
  margin: 50px auto;
  text-align: center;
}
.container .info h1 {
  margin: 0 0 15px;
  padding: 0;
  font-size: 36px;
  font-weight: 300;
  color: #1a1a1a;
}
.container .info span {
  color: #4d4d4d;
  font-size: 12px;
}
.container .info span a {
  color: #000000;
  text-decoration: none;
}
.container .info span .fa {
  color: #EF3B3A;
}

.hidden {
  display: none;
}

.error-message {
  color: #dc2d2d;
  font-size: 12px;
  margin-bottom:10px;
}

.input-error {
  font-family: "Roboto", sans-serif;
  outline: 0;
  background-color: #f2f2f2;
  width: 100%;
  border: 0;
  margin: 0 0 15px;
  padding: 15px;
  box-sizing: border-box;
  font-size: 14px;
  background-image:url(http://127.0.0.1:8000/static/icons/cancel.svg);
  background-repeat:no-repeat;
  background-size: 25px 25px;
  background-position: 95% 50%;
}
.input-correct {
  font-family: "Roboto", sans-serif;
  outline: 0;
  background-color: #f2f2f2;
  width: 100%;
  border: 0;
  margin: 0 0 15px;
  padding: 15px;
  box-sizing: border-box;
  font-size: 14px;
  background-image:url(http://127.0.0.1:8000/static/icons/checked.svg);
  background-repeat:no-repeat;
  background-size: 25px 25px;
  background-position: 95% 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="login-page">
    <div class="form">

      <form class="register-form hidden" method="POST" action="{% url 'signup' %}" id = "formregister">

          <input class="input" type="text"
                 name="username" placeholder="имя пользователя" maxlength="150"/>

          <input  class="input"  type="text"
                 name="email" placeholder="email"/>

          <input class="input" type="password" id="password1"
                  name="password1" placeholder="пароль"/>

          <input class="input" type="password" id="password2"
                 name="password2" placeholder="повторите пароль"/>

          <div class="errorTxtReg error-message">
                    


          </div>

          <button type="submit">создать</button>


          <p class="message">Уже зарегистрированны? <a href="#" id ="alogin-reg" for="login-form">Войти.</a></p>
          <p class="message">Забыли пароль? <a href="#" id ="arestore-reg" for="reset-form">Восстановить.</a></p>
      </form>


      <form class="login-form" method="POST" action="{% url 'login' %}" id = "formlogin">
   

          <input class="input" type="text" name="username"
          placeholder="имя пользователя" maxlength="254"/>

          <input class="input" type="password" name="password"
          placeholder="пароль" maxlength="254"/>

          <div class="errorTxtLog error-message"></div>

           <button type="submit">войти</button>

           <p class="message">Не зарегистрированны? <a href="#" id ="aregister-log" for="register-form">Создать аккаунт.</a></p>
           <p class="message">Забыли пароль? <a href="#" id ="arestore-log" for="reset-form">Востановить.</a></p>
      </form>



      <form class="reset-form hidden" method="POST" action="{% url 'reset'%}" id = "formreset">
 

          <input class="input" type="text" name="email"
                 placeholder="email" maxlength="200" />

          <div class="errorTxtRes error-message"></div>

          <button type="submit">Восстановить</button>

          <p class="message">Не зарегистрированны? <a href="#" id ="aregister-res" for="register-form">Создать аккаунт.</a></p>
          <p class="message">Уже зарегистрированны? <a href="#" id ="alogin-res" for="login-form">Войти.</a></p>
      </form>
  </div>
</div>

添加了:

 $('.message a').click(function(){
       $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
    });

1 个答案:

答案 0 :(得分:1)

您的js不正确。 “切换”不是高度和不透明度的有效值。还有一个问题是无法使用heightauto制作动画。一个问题,为什么要在表单上设置.hidden?查看你的HTML我可以看到它包含无效的html,例如<a>没有for <label>

在您的情况下,您可以使用两条路线。

  1. 您在jQuery中使用slideToggle();
  2. 为max-height设置动画并将其设置为近似值
  3. 在我使用#2的例子中(按照你的例子)

    此外,您需要在表单上添加overflow: hidden;(我在css中创建了它)。您也可以将jquery从.click()更改为.on('click'),这是建议进入。

    我发现在jsfiddle,demo

    中玩游戏更为明确

    $('.message a').on('click', function() {
      $('.form form').addClass('hidden');
      var form = $(this).attr('for');
    
      $('.' + form).animate({
    "maxHeight": 400,
    'opacity': 1
      }, "slow").removeClass('hidden');
    });
    body {
     font-family: "Roboto", sans-serif;
     -webkit-font-smoothing: antialiased;
     -moz-osx-font-smoothing: grayscale;
    }
    
    .login-page {
      width: 360px;
      padding: 8% 0 0;
      margin: auto;
      position: relative; top: 25px; left: 0; right: 0; bottom: 25px;
    }
    .form {
      position: relative;
      z-index: 1;
      background: #FFFFFF;
      max-width: 360px;
      margin: 0 auto 100px;
      padding: 45px;
      text-align: center;
      box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.2), 0 1px 1px 0 rgba(0, 0, 0, 0.24);
    overflow: hidden;
    }
    .form input {
      font-family: "Roboto", sans-serif;
      outline: 0;
      background-color: #f2f2f2;
      width: 100%;
      border: 0;
      margin: 0 0 15px;
      padding: 15px;
      box-sizing: border-box;
      font-size: 14px;
    
    }
    
    .form button {
      font-family: "Roboto", sans-serif;
      text-transform: uppercase;
      outline: 0;
      background: #5d98cc;
      width: 100%;
      border: 0;
      padding: 15px;
      color: #FFFFFF;
      font-size: 14px;
      -webkit-transition: all 0.3 ease;
      transition: all 0.3 ease;
      cursor: pointer;
    }
    .form button:hover,.form button:active,.form button:focus {
      background: #397eba;
    }
    .form .message {
      margin: 15px 0 0;
      color: #b3b3b3;
      font-size: 12px;
    }
    .form .message a {
      color: #5d98cc;
      text-decoration: none;
    }
    
    .container {
      position: relative;
      z-index: 1;
      max-width: 300px;
      margin: 0 auto;
    }
    .container:before, .container:after {
      content: "";
      display: block;
      clear: both;
    }
    .container .info {
      margin: 50px auto;
      text-align: center;
    }
    .container .info h1 {
      margin: 0 0 15px;
      padding: 0;
      font-size: 36px;
      font-weight: 300;
      color: #1a1a1a;
    }
    .container .info span {
      color: #4d4d4d;
      font-size: 12px;
    }
    .container .info span a {
      color: #000000;
      text-decoration: none;
    }
    .container .info span .fa {
      color: #EF3B3A;
    }
    
    .hidden {
      display: none;
    }
    
    .error-message {
      color: #dc2d2d;
      font-size: 12px;
      margin-bottom:10px;
    }
    
    .input-error {
      font-family: "Roboto", sans-serif;
      outline: 0;
      background-color: #f2f2f2;
      width: 100%;
      border: 0;
      margin: 0 0 15px;
      padding: 15px;
      box-sizing: border-box;
      font-size: 14px;
      background-image:url(http://127.0.0.1:8000/static/icons/cancel.svg);
      background-repeat:no-repeat;
      background-size: 25px 25px;
      background-position: 95% 50%;
    }
    .input-correct {
      font-family: "Roboto", sans-serif;
      outline: 0;
      background-color: #f2f2f2;
      width: 100%;
      border: 0;
      margin: 0 0 15px;
      padding: 15px;
      box-sizing: border-box;
      font-size: 14px;
      background-image:url(http://127.0.0.1:8000/static/icons/checked.svg);
      background-repeat:no-repeat;
      background-size: 25px 25px;
      background-position: 95% 50%;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="login-page">
        <div class="form">
    
          <form class="register-form hidden" method="POST" action="{% url 'signup' %}" id = "formregister">
    
              <input class="input" type="text"
                     name="username" placeholder="имя пользователя" maxlength="150"/>
    
              <input  class="input"  type="text"
                     name="email" placeholder="email"/>
    
              <input class="input" type="password" id="password1"
                      name="password1" placeholder="пароль"/>
    
              <input class="input" type="password" id="password2"
                     name="password2" placeholder="повторите пароль"/>
    
              <div class="errorTxtReg error-message">
                        
    
    
              </div>
    
              <button type="submit">создать</button>
    
    
              <p class="message">Уже зарегистрированны? <a href="#" id ="alogin-reg" for="login-form">Войти.</a></p>
              <p class="message">Забыли пароль? <a href="#" id ="arestore-reg" for="reset-form">Восстановить.</a></p>
          </form>
    
    
          <form class="login-form" method="POST" action="{% url 'login' %}" id = "formlogin">
       
    
              <input class="input" type="text" name="username"
              placeholder="имя пользователя" maxlength="254"/>
    
              <input class="input" type="password" name="password"
              placeholder="пароль" maxlength="254"/>
    
              <div class="errorTxtLog error-message"></div>
    
               <button type="submit">войти</button>
    
               <p class="message">Не зарегистрированны? <a href="#" id ="aregister-log" for="register-form">Создать аккаунт.</a></p>
               <p class="message">Забыли пароль? <a href="#" id ="arestore-log" for="reset-form">Востановить.</a></p>
          </form>
    
    
    
          <form class="reset-form hidden" method="POST" action="{% url 'reset'%}" id = "formreset">
     
    
              <input class="input" type="text" name="email"
                     placeholder="email" maxlength="200" />
    
              <div class="errorTxtRes error-message"></div>
    
              <button type="submit">Восстановить</button>
    
              <p class="message">Не зарегистрированны? <a href="#" id ="aregister-res" for="register-form">Создать аккаунт.</a></p>
              <p class="message">Уже зарегистрированны? <a href="#" id ="alogin-res" for="login-form">Войти.</a></p>
          </form>
      </div>
    </div>