2015-10-19 26 views
8

我有一個使用HTML和CSS的登錄頁面。它功能齊全,所有這些東西。但是,當登錄失敗時,我想讓登錄表單動搖。我如何使它動搖。現在,它什麼也沒做。如何晃動登錄表單出錯

這是我的HTML/CSS/JavaScript。如果需要更多的代碼,請告訴我。

HTML

<div class="input"> 
    <c:if test="${not empty error}"> 
     <div class="error">${error}</div> 
    </c:if> 
    <c:if test="${not empty msg}"> 
     <div class="msg">${msg}</div> 
    </c:if> 
    <div class="email"> 
     <input type="text" name="username" 
      placeholder="[email protected]"></input> 
    </div> 
    <div class="password"> 
     <input type="password" name="password" placeholder="*********"></input> 
    </div> 
</div> 

<input class="login_btn" type="submit" name="submit" value=""> 

CSS

.container .login_component .login_wrap .login_wp .input input { 
    width: 290px; 
    height: 50px; 
    padding-left: 20px; 
    font-size: 15px; 
    border: none; 
    background: transparent; 
} 

.container .login_component .login_wrap .login_wp .input .email { 
    height: 53px; 
    margin-top: 20px; 
    background: 
     url("http://xiilab.mynetgear.com:81/c.hwang/rems/images/login/input.png"); 
    background-repeat: no-repeat; 
    background-position: center center; 
} 

.container .login_component .login_wrap .login_wp .input .password { 
    height: 53px; 
    margin-top: 20px; 
    background: 
     url("http://xiilab.mynetgear.com:81/c.hwang/rems/images/login/input.png"); 
    background-repeat: no-repeat; 
    background-position: center center; 
} 

.container .login_component .login_wrap .login_wp .login_btn { 
    height: 55px; 
    margin-top: 30px; 
    background: 
     url("http://xiilab.mynetgear.com:81/c.hwang/rems/images/login/login_btn.png"); 
    background-repeat: no-repeat; 
    background-position: center center; 
    border: none; 
    width: 310px; 
} 

的JavaScript

$('input[type=submit]').on('click', function(e){ 
     e.preventDefault(); 
     $('.input').addClass('ahashakeheartache'); 
}); 

$('.input').on('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e){ 
     $('.input').delay(200).removeClass('ahashakeheartache'); 
}); 
+0

這可能是人們更容易地回答,而不必首先要挖一個大的一段代碼。如果* real *代碼不是真的需要,那麼最好給一個非常簡單的例子,或者甚至讓代碼完整。 :) – JakeTheSnake

回答

6

jQuery中使用 簡單的動畫,包括jQuery UI的

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 

    $(document).on('click', '#yoursubmitdiv', function(){ 

    //code if login fails 

     $("#yourloginfield").effect("shake"); // this will shake your div 



    }); 
+1

不要發佈jQuery解答非jQuery問題。 –

+0

@JosiahKeller廢話。看看給定的JavaScript示例。很明顯,JQuery。這並不是說這個答案是錯誤的;這個問題是不恰當的標籤。 –

+0

@ Two-BitAlchemist你是對的。我會將我的批評修改爲「不要將jQuery UI答案發布到vanilla jQuery問題」。 –

9

如果您使用animate.css,則可以創建該效果。這裏是您可以下載源代碼的github項目。還有其他一些效果可以查看。

代碼從animate.css顫抖:

.animated { 
    -webkit-animation-duration: 1s; 
    animation-duration: 1s; 
    -webkit-animation-fill-mode: both; 
    animation-fill-mode: both; 
} 

.animated.infinite { 
    -webkit-animation-iteration-count: infinite; 
    animation-iteration-count: infinite; 
} 

.animated.hinge { 
    -webkit-animation-duration: 2s; 
    animation-duration: 2s; 
} 

.animated.bounceIn, 
.animated.bounceOut { 
    -webkit-animation-duration: .75s; 
    animation-duration: .75s; 
} 

.animated.flipOutX, 
.animated.flipOutY { 
    -webkit-animation-duration: .75s; 
    animation-duration: .75s; 
} 

@-webkit-keyframes shake { 
    from, to { 
    -webkit-transform: translate3d(0, 0, 0); 
    transform: translate3d(0, 0, 0); 
    } 

    10%, 30%, 50%, 70%, 90% { 
    -webkit-transform: translate3d(-10px, 0, 0); 
    transform: translate3d(-10px, 0, 0); 
    } 

    20%, 40%, 60%, 80% { 
    -webkit-transform: translate3d(10px, 0, 0); 
    transform: translate3d(10px, 0, 0); 
    } 
} 

@keyframes shake { 
    from, to { 
    -webkit-transform: translate3d(0, 0, 0); 
    transform: translate3d(0, 0, 0); 
    } 

    10%, 30%, 50%, 70%, 90% { 
    -webkit-transform: translate3d(-10px, 0, 0); 
    transform: translate3d(-10px, 0, 0); 
    } 

    20%, 40%, 60%, 80% { 
    -webkit-transform: translate3d(10px, 0, 0); 
    transform: translate3d(10px, 0, 0); 
    } 
} 

.shake { 
    -webkit-animation-name: shake; 
    animation-name: shake; 
} 

JS

$(document).ready(function(){ 
    $('.submit').click(function(e){ 
     // Code to check login 
     // If fail 
     $('.input').addClass('animated shake'); 
    }); 
}) 
+0

對不起,我是一個Java開發人員,所以我不知道很多關於HTML的知識。你能幫我使用這個代碼並將其實施到我的? – bmarkham

+0

@bmarkham在你的CSS中,你必須添加animate.css,然後添加動畫類並動搖。當您檢查密碼是否正確或錯誤時,您可以添加animated.css中的class shake或其他類。查看編輯答案 –

+0

從上面的animate.css中複製代碼,並將包含登錄失敗的登錄信息的類別「shake」添加到包含登錄信息的div中 – diegorp

3

我真的建議在這裏使用普通的JavaScript,保持簡單,但同時不要太簡單。不僅僅是shake,而不是50行代碼。請詢問代碼是否有不清楚的地方,儘量保持它儘可能緊湊。

var box1Left1 = 100, box1Left2, keepShaking = true; 
 
var originalLeftPos = parseInt(document.getElementById("box1").style.left); 
 
    
 
setTimeout(stopShake, 1000); //Shake for how long 
 
function stopShake() { keepShaking = false; } 
 
    
 
setInterval(shake, 10); //Set shorter interval for faster shake 
 
function shake() { 
 
    if (keepShaking == true) { 
 
    if (box1Left1 < originalLeftPos + 5) { // "+5" = The shake distance. Go right. 
 
     box1Left1++; 
 
     document.getElementById("box1").style.left = box1Left1 + "px"; 
 
     box1Left2 = box1Left1; 
 
    } 
 
    if (box1Left1 >= (originalLeftPos + 5)) { // Go left. 
 
     box1Left2--; 
 
     document.getElementById("box1").style.left = box1Left2 + "px"; 
 
    } 
 
    if(box1Left2 == originalLeftPos) { box1Left1 = box1Left2; } // Go Right Again 
 
    } 
 
}
<div id="box1" style="position:absolute; top: 10px; left: 100px; width: 100px; height: 50px; background-color: #aa39fc;"> 
 
    Incorrect Password 
 
</div>

+0

當我單擊登錄按鈕時,我不確定此代碼是否搖動。 – bmarkham

+0

@bmarkham它不會,這只是一個例子。如果你正在尋找複製和粘貼的東西,我認爲你對這裏的幫助略有扭曲。 – JakeTheSnake

+0

不,我只是尋求可以應用於我所有問題的幫助,而不僅僅是一個。 – bmarkham