将 jquery 调用合并到 1 个函数中
我有以下在悬停时动画的jquery:
$('#footerNetwork').hover(function(){
$('#popupNetwork').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupNetwork').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerPort').hover(function(){
$('#popupPort').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupPort').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerAirport').hover(function(){
$('#popupAirport').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupAirport').animate({top:'30px'},{queue:true,duration:500});
});
等等...
我怎样才能将这些组合到on函数中,该函数可以识别哪个链接已悬停(即:footerNetwork)并针对适当的div进行动画(popupNetwork)? 谢谢!
I have the following jquery which animates on hover:
$('#footerNetwork').hover(function(){
$('#popupNetwork').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupNetwork').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerPort').hover(function(){
$('#popupPort').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupPort').animate({top:'30px'},{queue:true,duration:500});
});
$('#footerAirport').hover(function(){
$('#popupAirport').animate({top:'-62px'},{queue:true,duration:500});
}, function(){
$('#popupAirport').animate({top:'30px'},{queue:true,duration:500});
});
etc...
how can I combine these into on function which recognises which link has been hovered (ie: footerNetwork) and targets the appropriate div to animate (popupNetwork)??
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做:
如果你向这些元素添加一个类,例如
class="footer"
那么你可以更改.hover()
改为$('.footer').hover(function(){
使其更加简洁要获取适当的#popup_____
元素,我们只需获取当前 ID,例如footerNetwork
并执行.replace()
获取弹出窗口 ID。tops
对象是存储各种最高值,因为它们不同。You could do something like this:
If you add a class to those elements say
class="footer"
then you can change the.hover()
to$('.footer').hover(function(){
to make it even cleaner. To get the appropriate#popup_____
element we're just take the current ID, e.g.footerNetwork
and doing a.replace()
to get the popup ID. Thetops
object is to store the various top values since they differ.我假设您在要添加悬停行为的项目上有一个类
foo
。然后,只需遵循(明显的)footer...
模式:根据文档的结构,您还可以通过其容器来标识“footer...”元素,而不是通过其容器来识别“footer...”元素。按班级。
I'll assume you have a class
foo
on the items to which you want to add the hover behavior. Then it's just a matter of following the (apparent)footer...
pattern:Depending on how your document is structured, you could also identify the "footer..." elements by their container, rather than by class.
由于偏移量不是固定的,因此不可能通过一次调用获得相同的结果,但是,像这样的函数可以解决问题:
然后只需为每个动画调用该函数:
我猜会更好。当它们很多时,您可以使用类似以下内容的内容:
注意:未测试
Since the offsets are not fixed, it's not really possible to get the same result with one call, but, a function like this will do the trick:
Then just call the function for each animation:
Is much better I guess. When there'll be a lot of them, you could use something like:
note: Not tested