jQuery 悬停鼠标悬停/鼠标悬停计时
我在 freakyleaf.co.uk/hoverfade/ 上有以下实例,将鼠标悬停在图块上时,平铺背景图像在 600 毫秒内从 1 的不透明度渐变到 0.25 (.tile_img),然后文本在 500 毫秒内从 0 的不透明度渐变到 1 (.覆盖)。鼠标移出时,会发生相反的情况。
只要鼠标仅在鼠标悬停动画完成后离开,这一切就可以正常工作。如果鼠标在鼠标悬停动画期间离开,图块图像会淡回到完全不透明,但文本不会淡出,保持可见。
我有以下代码:
$(document).ready(function(){
$(".tile").hoverIntent(function() {
$(".tile_img", this).animate({"opacity": "0.25"}, 600,
function() { $(this).next(".overlay").animate({"opacity": "1"}, 500); }
);
},
function() {
$(".overlay", this).animate({"opacity": "0"}, 500,
function() { $(this).prev(".tile_img").animate({"opacity": "1"}, 600); }
);
});
});
和 HTML:
<div class="wrapper">
<ul id="service_boxes">
<li id="sb_recording" class="tile" onClick="location.href='recording.php';" style="cursor: pointer;">
<h2><a href="recording.php">Recording</a></h2>
<div class="tile_img"></div>
<div class="overlay"><p>Vintage analogue warmth and cutting-edge digital technology working in perfect harmony - That's the SoundARC sound!</p></div>
</li>
</ul>
</div>
我知道我也许应该使用 .stop 函数,但因此在几个地方尝试过,但到目前为止只破坏了代码。
我什至不确定我所拥有的是否是实现我想要的最好的方式;我纯粹是偶然才达到这一点,因为我是一个完全的新手。
任何帮助将不胜感激。
非常感谢。
I have the following live example at freakyleaf.co.uk/hoverfade/ whereby upon hovering over a tile, the tile background image fades form 1 to 0.25 opacity over 600ms (.tile_img), then text fades from 0 to 1 opacity over 500ms (.overlay). On mouseout, the reverse happens.
This all works fine as long as the mouse leaves only once the mouseover animation has completed. If the mouse leave during the mouseover animation, the tile image fades back to full opacity, but the text does not fade, leaving it visible.
I have the following code:
$(document).ready(function(){
$(".tile").hoverIntent(function() {
$(".tile_img", this).animate({"opacity": "0.25"}, 600,
function() { $(this).next(".overlay").animate({"opacity": "1"}, 500); }
);
},
function() {
$(".overlay", this).animate({"opacity": "0"}, 500,
function() { $(this).prev(".tile_img").animate({"opacity": "1"}, 600); }
);
});
});
And HTML:
<div class="wrapper">
<ul id="service_boxes">
<li id="sb_recording" class="tile" onClick="location.href='recording.php';" style="cursor: pointer;">
<h2><a href="recording.php">Recording</a></h2>
<div class="tile_img"></div>
<div class="overlay"><p>Vintage analogue warmth and cutting-edge digital technology working in perfect harmony - That's the SoundARC sound!</p></div>
</li>
</ul>
</div>
I understand that I should perhaps use the .stop function but so have tried it in a few places but have only broken the code so far.
I'm not even sure if what I have is the best way to achieve what I want; I have only got to this point purely by chance as I am a complete novice.
Any help would be greatly appreciated.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以通过使用 setInterval 检查动画是否仍在开启,并在完成时触发新动画来解决此问题。
You could also solve it by using setInterval to check if the animation is still on, and when it is completed fire the new animation..
通过使用
stop
方法停止动画并传递与clearQueue
和jumpToEmd
对应的 2 个参数(false、true)来尝试此操作。Try this by stopping the animation using
stop
method and passing 2 arguments(false, true) corresponding toclearQueue
andjumpToEmd
.我看到的主要问题是运行动画后半部分的回调意味着如果您尝试实现 .stop() 功能,它直到动画完成后才会运行。我相信这就是引起问题的原因,我下面的内容似乎有效;尽管仍然存在一些粗糙的问题,但问题已经解决了。
我放弃了hoverIntent的使用,因为我没有看到它的必要性,如果我错过了这一点,我深表歉意。我还发现悬停有点不可靠,所以我更喜欢以不同的方式设置进入和退出状态。我还将动画换成了 fadeTo 函数(功能相同,但阅读起来更整洁),并添加了一些 dequeue() ,尽管这主要是出于习惯。有些人可能会认为这毫无意义,但在某些时候这对我来说已经成为很好的实践。
The main problem I could see was that the callback to run the second half of the animation meant that if you tried to implement and .stop() functionality it wouldn't run until after the animation had completed. That's I believe what was causing issues, what I have below seems to work; though it's still got some rough edges the problem has gone.
I dumped the usage of hoverIntent as I didn't see the need for it, apologies if I missed the point. I also find that hover is a bit unreliable so I prefer setting in and out states differently. I've also swapped out the animations to the fadeTo function (does the same but a bit tidier to read), and thrown in some dequeue(), though this is mostly out of habit. Some might argue it's pointless but at some point it's become good practice for me.