我對jQuery很新,所以請溫和。我會如何縮短這個jQuery?
我發現了兩個網上的文章:
添加淡入/淡出翻車精靈使用jQuery: http://css-tricks.com/fade-image-within-sprite/ (路第三:傳統支持版本)
我已經使用了這兩個例子,並設法使它們工作GETHER:在一個列表中
http://www.marccohen.co.uk/dev/menu_example.htm
的翻車褪色精靈也觸發另一個列表動畫懸停,且反之亦然。麻煩的是所產生的jQuery的我已經是「非常」長,如此新來這個,我還是不知道我怎樣才能縮短:
$(function() {
$(".menuicon1")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn({duration:300});
$('ul.hover_block li.slide1').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
$(this).find("span").stop(true, true).fadeOut({duration:300});
$('ul.hover_block li.slide1').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon2")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn({duration:300});
$('ul.hover_block li.slide2').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
$(this).find("span").stop(true, true).fadeOut({duration:300});
$('ul.hover_block li.slide2').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon3")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn({duration:300});
$('ul.hover_block li.slide3').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
$(this).find("span").stop(true, true).fadeOut({duration:300});
$('ul.hover_block li.slide3').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon4")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn({duration:300});
$('ul.hover_block li.slide4').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
$(this).find("span").stop(true, true).fadeOut({duration:300});
$('ul.hover_block li.slide4').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$(".menuicon5")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn({duration:300});
$('ul.hover_block li.slide5').find('img').animate({top:'192px'},{queue:false,duration:150});
}, function() {
$(this).find("span").stop(true, true).fadeOut({duration:300});
$('ul.hover_block li.slide5').find('img').animate({top:'276px'},{queue:false,duration:150});
});
$('ul.hover_block li.slide1').hover(function(){
$(this).find('img').animate({top:'192px'},{queue:false,duration:150});
$('.menuicon1').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
$(this).find('img').animate({top:'276px'},{queue:false,duration:150});
$('.menuicon1').find("span").stop(true, true).fadeOut({duration:300});
});
$('ul.hover_block li.slide2').hover(function(){
$(this).find('img').animate({top:'192px'},{queue:false,duration:150});
$('.menuicon2').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
$(this).find('img').animate({top:'276px'},{queue:false,duration:150});
$('.menuicon2').find("span").stop(true, true).fadeOut({duration:300});
});
$('ul.hover_block li.slide3').hover(function(){
$(this).find('img').animate({top:'192px'},{queue:false,duration:150});
$('.menuicon3').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
$(this).find('img').animate({top:'276px'},{queue:false,duration:150});
$('.menuicon3').find("span").stop(true, true).fadeOut({duration:300});
});
$('ul.hover_block li.slide4').hover(function(){
$(this).find('img').animate({top:'192px'},{queue:false,duration:150});
$('.menuicon4').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
$(this).find('img').animate({top:'276px'},{queue:false,duration:150});
$('.menuicon4').find("span").stop(true, true).fadeOut({duration:300});
});
$('ul.hover_block li.slide5').hover(function(){
$(this).find('img').animate({top:'192px'},{queue:false,duration:150});
$('.menuicon5').find("span").stop(true, true).fadeIn({duration:300});
}, function(){
$(this).find('img').animate({top:'276px'},{queue:false,duration:150});
$('.menuicon5').find("span").stop(true, true).fadeOut({duration:300});
});
});
非常感謝所有建議...
編寫一個函數/擴展jQuery,傳入數字(每個代碼塊之間唯一更改),併爲每個元素(如果重構爲函數)或選擇器列表(如果擴展jQuery) 。 –
您可能沒有意識到這一點,但有一個codereview.stackexchange.com用於檢查大量代碼。 (這裏你的問題沒有問題,但你也可以使用codereview。) – kojiro
簡化代碼,並不是真的沒有。 – dda