我正在尝试做一种效果,当鼠标悬停在图像上时,它会变大50%,一旦鼠标移出它的区域,它就会变大。使用jquery可以做到这一点吗?多么?有没有可能在没有jquery的情况下做到这一点?没有jquery会有多难呢?
发布于 2011-03-19 05:02:55
这就是了:
$('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)
});
});发布于 2011-03-19 05:08:48
你可以通过使用纯CSS来做到这一点。这是一个running sample。
给定此HTML:
<img class="foo" src="/img/logo.png">添加此CSS:
body { background-color: black }
.foo {
height:25px;
}
.foo:hover {
height:50px;
}如果你的目标浏览器不支持像样的CSS,那就使用jQuery,但我在IE8上测试过,它支持这一点。
发布于 2011-03-19 05:01:40
使用jQuery可以做到这一点,它是一个 JavaScript 库,等等:你也可以使用普通的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;
}https://stackoverflow.com/questions/5357724
复制相似问题