gpt4 book ai didi

javascript - JQuery 动画效果不正确

转载 作者:行者123 更新时间:2023-11-30 20:52:16 25 4
gpt4 key购买 nike

我有 3 个表单,其中 2 个是隐藏的。我按链接将一种可见形式更改为另一种形式。所以当我改变事件形式时,我需要制作一个拉伸(stretch)或收缩的动画。现在我有了这个,但它不能正常工作:

$('.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");
});

最佳答案

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

在您的情况下,您可以选择两条路线。

  1. 你使用slideToggle();在 jQuery 中
  2. 为最大高度设置动画并将其设置为近似值

在我使用#2 的示例中(按照您的示例)

此外,您还需要添加 overflow: hidden;在表格上(我在 css 中制作的)。您也可以从 .click() 更改 jquery至 .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>

关于javascript - JQuery 动画效果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com