D. Make
D. Make

Reputation: 600

JQuery animation works incorrect

I have 3 forms, 2 of them hidden. I press link to change one visible form to other. So I need to make an animation of extension or constriction when I change active form. Now I have this, but it is not working correctly:

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

If i use:

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

It works correctly, but dont make animation. How can I fix it? Thanks and have a Happy New Year!

$('.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>

Added:

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

Upvotes: 1

Views: 145

Answers (1)

Dejan.S
Dejan.S

Reputation: 19128

Your js is not correct. "Toggle" is not a valid value for height and opacity. There is also the issue with not being able to animate height with auto. A question, why do you set .hidden on the forms? Looking into your html I can see it contains not valid html, like <a> does not have for that is <label>.

In your case you can go with two routes.

  1. you use slideToggle(); in jQuery
  2. you animate the max-height and set it to a approximate value

In the example I used #2 (to follow your example)

Also for this you need to add overflow: hidden; on the form (I made that in the css). Also You can change the jquery from .click() to .on('click'), that would be a recommendation to get into.

I find it more clear to play around in 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>

Upvotes: 1

Related Questions