我得到认真处理jQuery的,但发现自己一遍又一遍的重复代码...jQuery的简化代码(初级)
肯定有写这个简单的方法:
$('#more-mcr, #more-hilton, #more-lpool').hide();
$('#mcr-hatters').hoverIntent(function() {
$('#mcr-hilton').stop().animate({opacity: 0.4});
$('#more-mcr').fadeIn({duration:200});
}, function() {
$('#mcr-hilton').stop().animate({opacity: 1});
$('#more-mcr').fadeOut({duration:200});
});
$('#mcr-hilton').hoverIntent(function() {
$('#mcr-hatters').stop().animate({opacity: 0.4});
$('#more-hilton').fadeIn({duration:200});
}, function() {
$('#mcr-hatters').stop().animate({opacity: 1});
$('#more-hilton').fadeOut({duration:200});
});
$('#lpool-hostel').hoverIntent(function() {
$('#more-lpool').fadeIn({duration:200});
}, function() {
$('#more-lpool').fadeOut({duration:200});
});
$('#offers-mcr').hoverIntent(function() {
$('#offers-lpool').stop().animate({opacity: 0.4});
$('#offers-bham').stop().animate({opacity: 0.4});
}, function() {
$('#offers-lpool').stop().animate({opacity: 1});
$('#offers-bham').stop().animate({opacity: 1});
});
$('#offers-lpool').hoverIntent(function() {
$('#offers-mcr').stop().animate({opacity: 0.4});
$('#offers-bham').stop().animate({opacity: 0.4});
}, function() {
$('#offers-mcr').stop().animate({opacity: 1});
$('#offers-bham').stop().animate({opacity: 1});
});
$('#offers-bham').hoverIntent(function() {
$('#offers-lpool').stop().animate({opacity: 0.4});
$('#offers-mcr').stop().animate({opacity: 0.4});
}, function() {
$('#offers-lpool').stop().animate({opacity: 1});
$('#offers-mcr').stop().animate({opacity: 1});
});
我'd也想设置hoverIntent的延迟,但我认为这是不可能的,因为我目前编写代码的方式...?
如果有很多相同的项目,两个类可能会工作,但如果它将停留在这几个,ID选择器可能会更快。 – justkt 2010-06-03 16:41:34