jquery 淡入淡出优先级
我最近发布了关于悬停效果的文章。
现在可以了,但我想知道是否有办法优先考虑效果。例如,如果用户快速经历一系列图像翻转,则动画需要一段时间才能跟上。我不介意动画继续,但我希望当前悬停的项目立即淡入。
这是我目前的代码:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<title>Robs Test</title>
<style>
body{background:gray;}
div{position:relative;}
.box{
position:relative;
width:150px;
height:150px;
float:left;
display:block;
background:black;
margin-right:20px;
}
.boxover{
position:absolute;
top:10px;
left:0px;
width:100%;
height:20px;
z-index:100;
background:white;
display:none;
}
</style>
<script type="text/javascript">
$(function(){
$('.box').hover(over, out);
});
function over(event){
var over_id = '#box_over_' + $(this).attr('id').replace('over_','');
$(over_id).fadeIn(400);
$(over_id).css("display","normal");
}
function out(event) {
var over_id = '#box_over_' + $(this).attr('id').replace('over_','');
$(over_id).fadeOut(400);
}
</script>
</head>
<body>
<a href="#" class="box" id="over_1"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_1" class="boxover">hello</div></a>
<a href="#" class="box" id="over_2"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_2" class="boxover">there</div></a>
<a href="#" class="box" id="over_3"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_3" class="boxover">rob</div></a>
</body>
</html>
我可以做些什么来使悬停在其他div之上的div优先于其他div吗?所以我不必等到动画完成。
干杯
罗布
I have recently posted about a hover effect with hover.
It now works ok, but I was wondering if there is a way to prioritise the effect. For example, if the user goes through a sequence of image rollovers quickly it takes a while for the animation to catch up. I don't mind the animations continuing, but I would like the item that is currently being hovered over to fadein immediately.
Here is my code at the moment:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" type="text/javascript"></script>
<title>Robs Test</title>
<style>
body{background:gray;}
div{position:relative;}
.box{
position:relative;
width:150px;
height:150px;
float:left;
display:block;
background:black;
margin-right:20px;
}
.boxover{
position:absolute;
top:10px;
left:0px;
width:100%;
height:20px;
z-index:100;
background:white;
display:none;
}
</style>
<script type="text/javascript">
$(function(){
$('.box').hover(over, out);
});
function over(event){
var over_id = '#box_over_' + $(this).attr('id').replace('over_','');
$(over_id).fadeIn(400);
$(over_id).css("display","normal");
}
function out(event) {
var over_id = '#box_over_' + $(this).attr('id').replace('over_','');
$(over_id).fadeOut(400);
}
</script>
</head>
<body>
<a href="#" class="box" id="over_1"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_1" class="boxover">hello</div></a>
<a href="#" class="box" id="over_2"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_2" class="boxover">there</div></a>
<a href="#" class="box" id="over_3"><img src="pier.jpg" alt="test" width="150" height="150" /><div id="box_over_3" class="boxover">rob</div></a>
</body>
</html>
Is there something I can do to make the div that's being hovered over prioristise over the others? So I don't have to wait till the animation completes.
Cheers
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 jQuery 中的 stop 函数来使其更好。它将停止正在进行的动画。
You can use stop function in jQuery to make it better. It will stop the animation in progress.
仅当用户将鼠标悬停在图像上达到设定的阈值时间(不是每次翻转时)时,我才会触发动画(特别是如果它们很长)。
请参阅有关实现受控悬停的相关文章。
I would only trigger the animations (especially if they are long) if the user hovers over the image for set threshold amount of time (not on every rollover).
See related post on implementing controlled hover.
与 @SliverNinja 链接到的内容相关,专门针对您的场景:
这是小提琴: http://jsfiddle.net/9RR93/< /a>
Related to what @SliverNinja linked to, specifically for your scenario:
Here's the fiddle: http://jsfiddle.net/9RR93/
如果你想全力以赴,你可以使用 jquery 动画为对象构建自己的优先级队列系统。然后,每当一个新的动画开始时,您可以检查队列以查看是否存在需要停止的现有动画(如果其优先级较低),或者新的动画是否不应开始(因为已经存在更高优先级的动画)在队列中运行)。刚刚在最后 10 分钟写了这个,所以检查一下是否有错误,哈哈
If you want to go whole ham, you can build your own priority queue system for objects using jquery animations. Then whenever a new animation is started, you can check the queue to see if there is an existing animation that needs to be stopped (if its priority is lower), or if the new animation should not start (due to a higher priority animation already running in the queue). Just wrote this in the last 10 mins so, check for bugs lol