从中心放大图像百分比?
对于使用 jQuery 的网站,页面上有一些图形,单击这些图形时,会在网站的另一部分中显示信息。将鼠标悬停在上面时,图像会从中心扩大一定百分比。问题是,当您快速移入和移出鼠标时(在动画完成之前),图像无法正确调整大小。 (它们变得更小。)
$(".locationimg").hover(
function(){
var height = $(this).height()
var width = $(this).width()
var top = $(this).position().top
var left = $(this).position().left
$(this).stop().animate({
height: height*1.1 + 'px',
width: width*1.1 + 'px',
top: top - (((height*1.1)-height)/2) + 'px',
left: left - (((width*1.1)-width)/2) + 'px'
});
},
function(){
var height = $(this).height()
var width = $(this).width()
var top = $(this).position().top
var left = $(this).position().left
var height1 = height/1.1
var width1 = width/1.1
$(this).stop().animate({
height: height1 + 'px',
width: width1 + 'px',
top: top - (((height1)-height)/2) + 'px',
left: left - (((width1)-width)/2) + 'px'
});
}
);
如果可以在进入 .hover() 之前定义变量,这将很容易,因为调整图像大小将只是“高度:高度”等等。这样做的问题是,有多个图像都需要执行此操作,因此需要在 .hover() 中定义变量。
For a website using jQuery, there are graphics on a page that, when clicked on, bring up information in another section of the site. When moused over, the images expand by a percentage from their centre. The issue is that when you mouse in and out quickly (before the animation completes) the images do not resize correctly. (They get smaller.)
$(".locationimg").hover(
function(){
var height = $(this).height()
var width = $(this).width()
var top = $(this).position().top
var left = $(this).position().left
$(this).stop().animate({
height: height*1.1 + 'px',
width: width*1.1 + 'px',
top: top - (((height*1.1)-height)/2) + 'px',
left: left - (((width*1.1)-width)/2) + 'px'
});
},
function(){
var height = $(this).height()
var width = $(this).width()
var top = $(this).position().top
var left = $(this).position().left
var height1 = height/1.1
var width1 = width/1.1
$(this).stop().animate({
height: height1 + 'px',
width: width1 + 'px',
top: top - (((height1)-height)/2) + 'px',
left: left - (((width1)-width)/2) + 'px'
});
}
);
If the variables could be defined before going into the .hover(), this would be easy because resizing the image would simply be 'height: height' and so on. The problem with this is that there are several images that all need to do this, so the variables need to be defined within .hover().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
stop(false, true)
而不是仅仅使用stop()
。这意味着动画将在新动画开始之前“完成”。此外,您可以使用
data()
功能。Try to use
stop(false, true)
in stead of juststop()
. This means the animation will be 'completed' before the new animation is started.Furthermore, you can store the height and width on the element using
data()
function.在悬停()之前,您可以将原始和缩放的尺寸和位置存储在图像本身的数据属性中:
然后在悬停处理程序中,您可以只读取您想要的尺寸/位置,而不是尝试动态计算已经存储,并在鼠标悬停时转换为缩放的图像,或在鼠标移出时转换为原始图像。这也会快很多。
Before the hover(), you could store the original and zoomed dimensions and positions of each in data attributes on the images themselves:
Then inside the hover handler, instead of trying to calculate on the fly, you could just read the dimensions/positions you've already got stored, and transition to the zoomed ones on mouseover, or the original on mouseout. This will also be a lot faster.