[英]jQuery continuously scales a div element using hover()
I recently tried to use jQuery's effect('scale')
function with the hover()
function in jQuery. 我最近尝试将jQuery的
effect('scale')
函数与jQuery中的hover()
函数一起使用。 The idea is to enlarge the div element on mouseenter, and shrink it back to normal on mouseleave. 这个想法是在mouseenter上放大div元素,而在mouseleave上将其缩小到正常。 The code is as follows:
代码如下:
$('.boxgrid').hover(function(){
$(this).effect('scale', {percent:125}, 1000);
}, function() {
$(this).effect('scale', {percent:80}, 1000);
});
I tried testing this in a jsfiddle, but instead of upscaling and downscaling once when a mouse enters the element, it keeps enlarging it. 我尝试在jsfiddle中对此进行测试,但是当鼠标进入元素时,它不会放大和缩小,而是会不断放大。 You can see the jsfiddle here .
您可以在这里看到jsfiddle。 My question is how do I fix it?
我的问题是如何解决? My understanding was that the mouseenter event was fired only once, and reset when the mouseleaves event is fired, but this seems to say otherwise?
我的理解是mouseenter事件仅被触发一次,并在mouseleaves事件被触发时重置,但这似乎是另外一个说法? Am I missing something?
我想念什么吗? Any help is greatly appreciated.
任何帮助是极大的赞赏。
It calls the hover function every time the animation finishes. 每当动画结束时,它将调用悬停函数。 I modified it as follows and it appears to work:
我对其进行了如下修改,并且似乎可以正常工作:
window.boxScaled = false;
$('.boxgrid').hover(function(){
if(!window.boxScaled) {
window.boxScaled = true;
$(this).effect('scale', {percent:125}, 1000);
}
}, function() {
if(window.boxScaled) {
window.boxScaled = false;
$(this).effect('scale', {percent:80}, 1000)
}
});
I think you need to force the animation to stop if you leave the box before it has finished scaling (or reenter the box before it has shrunk): 我认为,如果您在动画完成缩放之前离开动画框(或在动画缩小之前重新进入动画框),则需要强制动画停止播放:
$('.boxgrid').hover(function(){
$(this).stop().effect('scale', {percent:125}, 1000);
}, function() {
$(this).stop().effect('scale', {percent:80}, 1000);
});
http://jsfiddle.net/magicaj/9GLEy/10/ http://jsfiddle.net/magicaj/9GLEy/10/
Your understanding of hover (mouseenter/mouseleave) is correct, they should only fire once upon entering/leaving. 您对悬停(鼠标/滑鼠)的理解是正确的,它们只能在进入/离开时触发一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.