2015-12-03 44 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