2015-08-31 58 views
0

当用户悬停div时,此div增加大小。但是当用户从这个div中移除光标时如何减小大小。JQuery在悬停时增加div大小

var hoveredDiv; 
     $('.promoContent').hover(function() { 
      hoveredDiv = $(this).attr('id'); 
      $('#' + hoveredDiv).animate({height: '100%'}); 
     }); 
+1

使用,'$(选择).mouseenter({...})。 mouseleave({...});' –

+0

谢谢。有用。 – qr11

+0

为什么你会在这个简单的悬停情况下使用js而不是css?为什么在地狱你得到元素的ID再次选择它? –

回答

1

您可以使用

的.hover()方法结合处理两种的mouseenter和鼠标离开 事件。当鼠标位于元素内时,您可以使用它在 期间将行为简单应用到元素。

$('.promoContent').hover(function() { 
    $(this).animate({height: '100%'}); 
},function(){ 
     $(this).animate({height: '50%'}); 
}); 
1

您也可以使用鼠标悬停及移出事件。

http://jsfiddle.net/xno5hb34/1/

<button class="promoContent" id="hoveredDiv" style="width:30%; height: 60%; background-color: red">MouseOverToIncreaseSize</button> 

var hoveredDiv; 
    $('.promoContent').mouseover(function() { 
     hoveredDiv = $(this).attr('id'); 
     $('#' + hoveredDiv).animate({ height: (parseInt($('#' + hoveredDiv).css("height").trim('px')) * 2) + "px" }); 
    }); 

    $('.promoContent').mouseout(function() { 
     hoveredDiv = $(this).attr('id'); 
     $('#' + hoveredDiv).animate({ height: (parseInt($('#' + hoveredDiv).css("height").trim('px'))/2) + "px" }); 
    }); 
0
var hoveredDiv; 
    $('.promoContent').hover(function() { 
     hoveredDiv = $(this).attr('id'); 
     $('#' + hoveredDiv).animate({"height": "100%"}); 
    }); 

$( '#' + hoveredDiv).animate({ “高度”: “100%”});

只需添加双qute( “”)

“高度”: “100%”