首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery onmouseover图像大小增加

问Jquery onmouseover图像大小增加
EN

Stack Overflow用户
提问于 2011-03-19 04:53:04
回答 4查看 21.1K关注 0票数 2

我正在尝试做一种效果,当鼠标悬停在图像上时,它会变大50%,一旦鼠标移出它的区域,它就会变大。使用jquery可以做到这一点吗?多么?有没有可能在没有jquery的情况下做到这一点?没有jquery会有多难呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-19 05:02:55

这就是了:

代码语言:javascript
复制
$('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/

票数 16
EN

Stack Overflow用户

发布于 2011-03-19 05:08:48

你可以通过使用纯CSS来做到这一点。这是一个running sample。

给定此HTML:

代码语言:javascript
复制
<img class="foo" src="/img/logo.png">

添加此CSS:

代码语言:javascript
复制
body { background-color: black }
.foo {
    height:25px;
}
.foo:hover {
    height:50px;
}

如果你的目标浏览器不支持像样的CSS,那就使用jQuery,但我在IE8上测试过,它支持这一点。

票数 4
EN

Stack Overflow用户

发布于 2011-03-19 05:01:40

使用jQuery可以做到这一点,它是一个 JavaScript 库,等等:你也可以使用普通的JavaScript。

jQuery:

代码语言:javascript
复制
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/

代码语言:javascript
复制
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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5357724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档