Jquery - 悬停时将图像高度和宽度扩展 20%

标签 jquery this jquery-animate

晚上好 - 动态访问图像高度和宽度的最佳方法是什么。
我想在图像宽度和高度上添加 20%,并在周围的 div 悬停时设置动画,我想我需要使用“this”,但不确定如何访问图像。

非常感谢您的帮助。

干杯保罗

最佳答案

您可以使用 .height() 执行类似的操作和 .width()带函数参数:

$("img").hover(function() {
    $(this).height(function(i, h) { return h * 1.2; })
           .width(function(i, w) { return w * 1.2; });
}, function() {
    $(this).height(function(i, h) { return h / 1.2; })
           .width(function(i, w) { return w / 1.2; });
});​

You can give it a try here ,如果您想要平滑的动画,您可以存储初始高度/宽度和 .animate() ,像这样:

$("img").each(function() {
    $.data(this, 'size', { width: $(this).width(), height: $(this).height() });
}).hover(function() {
    $(this).stop().animate({ height: $.data(this,'size').height*1.2, 
                             width: $.data(this,'size').width*1.2 });
}, function() {
    $(this).stop().animate({ height: $.data(this,'size').height, 
                             width: $.data(this,'size').width });
});​

You can give it a try here ,请务必在 $(window).load() 中运行这些选项之一,并且 $(document).ready() ,因为尺寸可能尚未加载。

关于Jquery - 悬停时将图像高度和宽度扩展 20%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3630734/

相关文章:

javascript - 下拉菜单悬停,但子菜单不悬停

javascript - 为什么 jQuery 必须接收 2 个对象 : body & html when scrolling whole page with animation?

javascript - 使用 var 作为对象的键(特别是 jQuery.animate)

javascript - "jquery.unobtrusive-ajax.js"已经过时了吗?

javascript - 如何获取点击了哪个元素?

javascript - 设置 openlayers 弹出窗口大小的正确方法是什么?

c++ - 无法使用 this 指针在对象上使用重载的 [] 运算符

jQuery - 'this' 选择器在回调函数中不起作用

javascript - 调用 mylibray onclick 中的函数并传递此函数

javascript - 用 javascript 制作的动画 li - 为什么它们都同时移动?