jQuery,多次悬停,优化代码
我创建了一个代码,当鼠标悬停时改变背景的位置,我对它的工作方式很满意,但是我有大约 50 个可能的位置,并且这个代码看起来非常麻烦。具有“mouseover”和“mouseout”的行几乎相同,只是数字发生了变化。是否可以简化这段代码,这样就不会一遍又一遍地编写相同的内容?
$('.b-test a').addClass('over-1')
$('.b-test a.over-1').live("mouseover", function(){
$(this).css("background-position", "0 -120px");
});
$('.b-test a.over-1').live("mouseout", function(){
$(this).addClass("over-2").removeClass('over-1');
});
$('.b-test a.over-2 ').live("mouseover", function(){
$(this).css("background-position", "0 -240px");
});
$('.b-test a.over-2 ').live("mouseout", function(){
$(this).addClass("over-3").removeClass('over-2');
});
$('.b-test a.over-3 ').live("mouseover", function(){
$(this).css("background-position", "0 -360px");
});
$('.b-test a.over-3 ').live("mouseout", function(){
$(this).removeClass('over-3').addClass("over-4");
});
$('.b-test a.over-4 ').live("mouseover", function(){
$(this).css("background-position", "0 0");
});
$('.b-test a.over-4 ').live("mouseout", function(){
$(this).removeClass('over-4').addClass("over-1");
});
还有一个问题。我可以在鼠标悬停时设置随机背景位置,但它应该是120的倍数吗?
非常感谢您的帮助。
I created a code that changes the position of the background when mouse over, I'm pleased with how it works, but I have about 50 possible positions and this code looks very cumbersome. Rows with "mouseover" and "mouseout" virtually identical, only the numbers changing. Is it possible to simplify this code, that would not write the same thing over and over again?
$('.b-test a').addClass('over-1')
$('.b-test a.over-1').live("mouseover", function(){
$(this).css("background-position", "0 -120px");
});
$('.b-test a.over-1').live("mouseout", function(){
$(this).addClass("over-2").removeClass('over-1');
});
$('.b-test a.over-2 ').live("mouseover", function(){
$(this).css("background-position", "0 -240px");
});
$('.b-test a.over-2 ').live("mouseout", function(){
$(this).addClass("over-3").removeClass('over-2');
});
$('.b-test a.over-3 ').live("mouseover", function(){
$(this).css("background-position", "0 -360px");
});
$('.b-test a.over-3 ').live("mouseout", function(){
$(this).removeClass('over-3').addClass("over-4");
});
$('.b-test a.over-4 ').live("mouseover", function(){
$(this).css("background-position", "0 0");
});
$('.b-test a.over-4 ').live("mouseout", function(){
$(this).removeClass('over-4').addClass("over-1");
});
And one more question. Can I set random background position when mouse hover, but it should be multiple 120?
Many thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许你可以声明一个对象来保存类和 y 位置之间的关系?
有点像这样:
然后像这样重写你的代码:
当然,这假设所讨论的元素只有一个类。
编辑
如果您的元素具有或可能具有多个类,您可以迭代对象成员来检查它是哪一个,如下所示:
Maybe you could declare an object which holds the relation between class and y position?
A bit like this:
and then rewite your code like this:
of course, this assumes that the elements in question only have one class.
Edit
If your elements have or are likely to have more than one class, you could iterate over the objects members to check which one it is, like this:
我认为你最好的选择是为每个 Y 位置创建一个带有插槽的数组(因为没有
a.over-0
对象,所以在位置 0 处放置一个 null),然后循环遍历它们。 (尚未完全测试,但想法已经存在)如果您想使用随机 Y 坐标,请尝试此...
I think your best bet is to make an array with a slot for each Y-Position (putting a null in position 0 since you have no
a.over-0
object), and loop through them thusly. (Not fully tested, but the idea is there)If you want to use random Y-Coordinates, try this...