2013-05-27 49 views
0

我正試圖編寫一個函數,當鼠標進入時它可以改變元素的字體大小,並將其恢復爲原始字體大小當鼠標離開它時。這是我的:更改鼠標移過來的字體大小,在鼠標離開時還原爲原始

$(".month").hover(function(){ 

     var size = $(this).css("font-size"); 

     $(this).stop().animate({ 
      fontSize: start_font + font_off, 
      opacity: '1' 
     }, 200);  

    },function(){ 

     $(this).stop().animate({ 
      fontSize: size, 
      opacity: '1' 
     }, 200); 
    }); 

它改變鼠標的字體大小,但是當鼠標離開時,它只是保持相同的大小。 (在字體大小更改之後,我做了一個警告(大小),並且它保存了正確的值。)我在這裏做錯了什麼?

+0

什麼是START_FONT,font_off – Gautam3164

+2

爲什麼不使用CSS3過渡,讓它優雅降級? – elclanrs

+0

我把var start_font = 50; var font_off = 8;在頁面頂部(功能外) – Mushy

回答

2

據我瞭解,這將有助於你

$(".month").hover(function(){ 

    var size = $(this).css("font-size"); 

    $(this).stop().animate({ 
     fontSize: start_font + font_off, 
     opacity: '1' 
    }, 200);  

},function(){ 
    var size = $(this).css("font-size");  //Add this 
    $(this).stop().animate({ 
     fontSize: size - font_off, 
     opacity: '1' 
    }, 200); 
}); 

或通過CSS,你可以這樣做:盤旋狀

.month:hover { 
    font-size: 150%; 
} 
6

你可以用CSS實現這一點很容易:

.month:hover { 
    font-size: 150%; 
    } 

但是,在jQuery中你可以這樣做:

$(".month").hover(function(){ 
    $(this). 
    stop(). 
    animate({ 
     fontSize: "5em" 
     }, 1000); 
    }, 
    function(){ 
    $(this). 
     stop(). 
     animate({ 
     fontSize: "1em" 
     }, 1000); 
    } 
); 

See jsfiddle。請注意,我用ems因爲The 「em」 is a scalable unit that is used in web document media. An em is equal to the current font-size, for instance, if the font-size of the document is 12pt, 1em is equal to 12pt.Source

+0

有沒有辦法在JavaScript中做到這一點? .month元素的字體大小會動態變化(使用javascript),我想將所有內容都放在一個地方。此外,我試圖學習JavaScript,所以它會有所幫助 – Mushy

+0

是的,[這應該工作](http://jsfiddle.net/pgqKM/)。 –

+0

問題是.month的元素都有不同的大小,所以獲得它們大小的唯一方法(從我所瞭解的)就是執行.css()。 – Mushy

1
$(".month").hover(function(){ 
    var size = $(this).css("font-size"); 
    alert(size); 
    $(this).stop().animate({ 
     fontSize: start_font + font_off, 
     opacity: '1' 
    }, 200);  

},function(){ 

    $(this).stop().animate({ 
     fontSize: size,//In this line u r getting error as size not defined 
     opacity: '1' 
    }, 200); 
    alert('leaving '+size); 
}); 
0

你可以做到這一點有兩種方式:

從jQuery的動畫功能:

$('#my-list li').hover(function() { 
     $(this).stop().animate({ fontSize : '20px' }); 
}, 
function() { 
     $(this).stop().animate({ fontSize : '12px' }); 
}); 

並從CSS

a{ 
    font-size:16px; 
} 

a:hover { 
    font-size:18px; 
} 
0
$(".month").hover(function(){ 

if($('#month').hasClass('hoverout')){ 

    $(this).removeClass("hoverout"); 
} 

    $(this).addClass("hoverin"); 
    $(this).stop().animate({ 
     opacity: '1' 
    }, 200);  

},function(){ 

    if($('#month').hasClass('hoverin')){ 

      $(this).removeClass("hoverin"); 
     } 

    $(this).addClass("hoverout"); 

    $(this).stop().animate({ 
     opacity: '1' 
    }, 200); 

}); 

CSS

.hoverin{ 

    font-size:20px; 
} 

.hoverout {

font-size:50px; 

} 
相關問題