2015-12-03 42 views
0

我试图从一种表单字段类型转换为另一种。如果我插入另一个<input>元素,则下一个元素的静态位置不会生成动画。JQuery/CSS:动画静态位置

$("#switchlogin").click(function() { 
 
    //Create the label element 
 
    var emaillabel = $("<label>").text("Email").attr("for", "email"); 
 
    //Create the input element 
 
    var emailinput = $('<input type="text">').attr({ 
 
    id: "email", 
 
    name: "email" 
 
    }); 
 

 
    //Create the label element 
 
    var pwdlabel = $("<label>").text("Repeat Password").attr("for", "password-wh"); 
 
    //Create the input element 
 
    var pwdinput = $('<input type="password">').attr({ 
 
    id: "password-wh", 
 
    name: "password-wh" 
 
    }); 
 

 
    $("#username-field").after(emaillabel); 
 
    emaillabel.after(emailinput); 
 

 
    $("#password-field").after(pwdlabel); 
 
    pwdlabel.after(pwdinput); 
 
});
input { 
 
    border: solid #e3e3e3 1pt; 
 
    display: block; 
 
    margin-bottom: 1em; 
 
    font-size: 14pt; 
 
    opacity: 0; 
 
    animation: fadeIn .3s forwards; 
 
} 
 
label { 
 
    opacity: 0; 
 
    animation: fadeIn .3s forwards; 
 
} 
 
@keyframes fadeIn { 
 
    to { 
 
    opacity: 1; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<form action="<?php the_permalink(); ?>" id="login" method="post"> 
 
    <div id="username-field"> 
 
    <label for="username">Username</label> 
 
    <input type="text" id="username" name="username"> 
 
    </div> 
 

 
    <div id="password-field"> 
 
    <label for="password">Password</label> 
 
    <input type="password" id="password" name="password"> 
 
    </div> 
 

 
    <div id="buttons-field"> 
 
    <button type="submit">Login</button> 
 
    <a href="#" id="switchlogin">Register</a> 
 
    </div> 
 
</form>

https://jsfiddle.net/fpvctpjs/2/

所以,我想那是什么,按注册链接时,该password-field向下移动顺畅,使地方的新元素。

我尝试了几件事情,如加入margin-top,position:absoluteposition:relative,但不知何故我不能得到它的工作。

感谢你的帮助,
marsman

+0

我个人认为它不是一个好主意,添加输入登录形式成为登记表格..对我来说,我将创建2种独立的形式一个用于登录,一个用于寄存器和效果基本show之一,另一了slideDown ..再(为我)它是一种更好的方式来做到这一点.. –

+0

无关你的问题,但值得一提:点击** **注册按钮将输入域_each time_ –

回答

2

您的密码字段不顺畅动画,因为它的运动是被附加在其他领域的结果。

什么是最好的是让其他领域总是在那里,但隐藏在一个div高度为0; - 然后当你点击重置按钮,动画到该div的期望高度和淡入孩子的投入。

+0

的作品就像一个魅力,谢谢! :) – marsman

+0

@marsman真棒。你介意把这个标记为答案吗?干杯。 – RooWM