2011-03-18 65 views
2

我試圖做一個效果,當鼠標懸停在圖像上時,它會增大其大小的50%,並在鼠標移出其區域後立即返回。可以用jQuery來做到這一點嗎?怎麼樣?有沒有可能做到這一點沒有jQuery?在沒有jQuery的情況下做這件事有多難?Jquery onmouseover圖像大小增加

+1

沒有jQuery會有多難? **不必要**很難。 – 2011-03-18 20:55:23

+0

在我的例子中,你可能會認爲沒有太大的區別。但是如果你想使用不同的選擇器(即不是ID)來獲取圖像,那麼怎麼樣?如果你想動畫過渡(jQuery可以輕鬆實現這一點),那該怎麼辦?而且,使用jQuery,您不必擔心跨瀏覽器兼容性會接近相同的程度。 – dianovich 2011-03-18 21:04:39

回答

15

在這裏你去:

$('img').load(function() { 
    $(this).data('height', this.height); 
}).bind('mouseenter mouseleave', function(e) { 
    $(this).stop().animate({ 
     height: $(this).data('height') * (e.type === 'mouseenter' ? 1.5 : 1) 
    }); 
}); 

現場演示:http://jsfiddle.net/simevidas/fwUMx/5/

+0

如果圖像在固定寬度的表格內,它會起作用 – Vish 2011-03-20 18:18:44

+1

非常感謝,它很棒。 http://jsfiddle.net/fwUMx/6/。當鼠標移動到圖像上時,表格可能不會移動。 – Vish 2011-03-20 18:21:25

+1

@user我已經能夠通過明確設置表格單元格的高度來獲得體面的效果。看到這裏:http://jsfiddle.net/simevidas/fwUMx/10/ – 2011-03-20 19:48:26

2

這是可能使用jQuery,這是一個的JavaScript等alors要做到這一點:你也可以使用普通的JavaScript。

的jQuery:

var $image = $('#imageID'), //Or some other selector 
    imgWidth = $image.width(), 
    imgHeight = $image.height(); 
$('#imageID').hover(function() { 
    //The mouseover 
    $(this).width(imgWidth * 2); 
    $(this).height(imgHeight * 2);  
}, function() { 
    $(this).width(imgWidth); 
    $(this).height(imgHeight); 
}); 

普通的JavaScript:
見這裏的例子:http://jsfiddle.net/axpVw/

var image = document.getElementById('imageID'), 
    imageWidth = image.width, 
    imageHeight = image.height; 
image.onmouseover = function() { 
    image.width = 2 * imageWidth; 
    image.height = 2 * imageHeight; 
} 
image.onmouseout = function() { 
    image.width = imageWidth; 
    image.height = imageHeight; 
} 
+0

@ŠimeVidas爲jQuery提供了一個更清晰的例子 - 真的會推薦動畫屬性。 – dianovich 2011-03-18 21:08:07

4

您可以使用純CSS做到這一點。這是一個running sample

鑑於這種HTML:

<img class="foo" src="/img/logo.png"> 

添加這個CSS:

body { background-color: black } 
.foo { 
    height:25px; 
} 
.foo:hover { 
    height:50px; 
} 

使用jQuery,如果你的目標瀏覽器的一個不支持CSS體面,但我在IE8測試,它支持這個。

+0

您正在將高度值硬編碼到CSS中。如果圖像具有不同的尺寸怎麼辦? – 2011-03-18 21:11:11

+0

你必須做一個按圖像的CSS樣式。如果你有圖像的話,這將是一個使用jquery的好地方。 – JoshRivers 2011-03-18 22:23:34

+0

好主意:)我想upvote你的答案,但沒有足夠的代表。 – Vish 2011-03-20 18:33:45

1

這兩種方式都是一件容易的事。我對你的小提琴在這兩個方面的例子:

  1. jQuery的http://jsfiddle.net/G7yTU/

    $(document).ready(function(){  
    var ht= $("img").height(),  
    wd=$("img").width(),  
    mult=1.5; //change to the no. of times you want to increase your image 
         //size. 
    $("img").on('mouseenter', function(){ 
    $(this).animate({height: ht*mult, 
           width: wd*mult}, 500); 
    }); 
    $("img").on('mouseleave', function(){ 
    $(this).animate({height: ht, 
           width: wd}, 500); 
    }) 
    }); 
    
  2. CSShttp://jsfiddle.net/zahAB/1/

    img{ 
    -webkit-transition:all 0.5s; 
    -moz-transition:all 0.5s; 
    -ms-transition:all 0.5s; 
    -o-transition:all 0.5s; 
        } 
    
    img:hover{ 
        width:150px; 
        height:150px; 
    } 
    

如果您需要任何幫助,請聯繫。

+0

哪種方法更好? – Vish 2014-02-08 05:57:03

+0

我會說使用Jquery,因爲它將被全球支持,並且可以根據您的需要輕鬆編輯它。恩。如果你想讓圖像擴大300%,只需將變量更改爲3並完成工作即可。 – 2014-02-08 06:23:07