2016-11-23 101 views
0

我試圖在鼠標懸停在圖像上時對圖片進行動畫處理。我目前使用「animate.css」來獲取動畫。有兩個問題:如何讓jQuery在鼠標懸停圖像上識別一次

1)如何讓jQuery在每次懸停時「激發」我的腳本?例如,如果我創建了一個警報,警報將會多次觸發(將鼠標懸停並將其關閉)。

2)我目前的jQuery語法有什麼問題?動畫不播放。

HTML

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <link href="CSS/stylesheet_test.css" rel="stylesheet" type="text/css"> 
    <link rel="stylesheet" href="CSS/animate.css"> 
    <link href="https://fonts.googleapis.com/css?family=Work+Sans:500&amp;subset=latin-ext" rel="stylesheet"> 
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet"> 
    <script type="text/javascript" src="scripts/jquery-3.1.1.min.js"></script> 
    <script type="text/javascript" src="scripts/test.js"></script> 
    <title>Daryl N.</title> 
    </head> 
    <body> 
    <div class="container"> 
     <div class="navbar"> 
     <div class="nav-text"><span id="home"><a href="index_test.html">Home</a></span><span id="archive">Archive</span></div> 
     </div> 
     <div id="first" class="background"> 
     <div class="align-vert"><img id="me-pic" src="./images/me.jpg" alt="A picture of me is suppose to be here" style="width:240px;height:240px;" class="me-border animated bounceIn"><span class="daryl animated bounceIn">Hi, I'm Daryl</span></div> 
     </div> 
     <script type="text/javascript" src="scripts/test.js"></script> 
     <div id="second" class="background"> 
     <p class="align-vert">This is my personal website</p> 
     </div> 
    </div> 
    </body> 
</html> 

JS

$(document).ready(function() 
{ 
    $('#me-pic').hover(function() 
    { 
    $(".bounceIn").toggleClass("bounce"); 
    }); 
}); 

我的CSS

body,html 
{ 
    width: 100%; 
    min-width: 200px; 
    height: 100%; 
    padding: 0; 
    margin: 0; 
} 

.container 
{ 
    width: 100%; 
    min-width: 500px; 
    height: 100%; 
} 

.background 
{ 
    height: 100%; 
    width: 100%; 
    display: inline-block; 
    position: relative; 
    z-index: 1; 
} 

.align-vert 
{ 
    text-align: center; 
    position: relative; 
    top: 50%; 
    transform: translateY(-50%); 
} 

.me-border 
{ 
    border-radius: 50%; 
    border: 2px solid #FFF; 
    vertical-align:middle 
} 

.navbar 
{ 
    position: fixed; 
    background-color: rgba(0, 0, 0, 0.9); 
    margin: 0; 
    padding: 0; 
    width: 100%; 
    height: 50px; 
    z-index: 2; 
} 

.nav-text 
{ 
    color: #a3a3a3; 
    font-family: 'Raleway', sans-serif; 
    font-weight: 600; 
    font-size: 16px; 
    text-align: center; 
    position: relative; 
    top: 50%; 
    transform: translateY(-50%); 
} 
    #home 
    { 
    margin-right: 50px; 
    } 

    #home:hover 
    { 
     color: #777777; 
    } 

    #home a:hover, a:visited, a:link, a:active 
    { 
     text-decoration: none; 
     color: inherit; 
    } 

    #archive 
    { 
    margin-left: 50px; 
    } 

    #archive:hover 
    { 
     color: #777777; 
    } 


.daryl 
{ 
    font-family: 'Work Sans', sans-serif; 
    display: inline-block; 
    padding-left: 20px; 
    font-size: 30px; 
    color: #FFF; 
} 
#first 
{ 
    background: #2C2D45; 
} 

#second 
{ 
    background: #354677; 
    font-size: 40px; 
    font-family: 'Work Sans', sans-serif; 
    color: white; 
} 

here爲animate.css

難道我的CSS文件會導致衝突?

在此先感謝!

+0

你沒有表現出CSS。什麼是「反彈」類? – Banzay

+0

請看我更新的帖子。 – darylnak

+0

我不太確定你的問題,但你可以嘗試mouseenter而不是懸停,看看它是否有效 – Chiller

回答

2

在你的JS中,我會改用mouseentermouseleave事件處理程序。根據你的解釋,這應該很好地服務。它會在鼠標進入div時觸發一次,在它離開時觸發一次。

$(document).ready(function() 
 
{ 
 
    $('#me-pic').on('mouseenter', function() { 
 
    $(".bounceIn").toggleClass("bounce"); 
 
    }); 
 
    
 
    $('#me-pic').on('mouseleave', function() { 
 
    $(".bounceIn").toggleClass("bounce"); 
 
    }); 
 
    
 
});

+0

'.hover()'方法在傳遞一個函數時,將爲mouseenter和mouseleave事件執行該處理程序。這允許用戶在處理程序中使用jQuery的各種切換方法,或者根據事件在處理程序中做出不同的響應。type – Banzay

+0

這似乎不適用於animate.css。我試着用你的方法警報,它的工作原理。 animate.css似乎有一些特殊之處,可以防止效果正常觸發。謝謝您的幫助! – darylnak

相關問題