0
嗨那裏我正在開發客戶端網站的交互性,我覺得像我的按鈕功能的JS和carousel.fader窗口是有點冗餘構建。有人可以看看這段代碼,看看它是否可以更簡化,以減少頁面加載時間,真的只是清理JS。任何幫助是極大的讚賞。JavaScript精簡
function cycleImages(){
$('.fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',7).addClass('active');//make the next image the top one
});
});
}
function cycleImages2(){
$('.about-fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',7).addClass('active');//make the next image the top one
});
});
}
function cycleImages3(){
$('.contact-fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',7).addClass('active');//make the next image the top one
});
});
}
function cycleImages4(){
$('.sourcing-fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',5).addClass('active');//make the next image the top one
});
});
}
function cycleImages5(){
$('.consulting-fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',5).addClass('active');//make the next image the top one
});
});
}
function cycleImages6(){
$('.installation-fader').each(function(){
var $active = $(this).find('.active');
var $next = ($(this).find('.active').next().length > 0) ? $(this).find('.active').next() : $(this).find('img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',5).addClass('active');//make the next image the top one
});
});
}
$(document).ready(function(){
$(".about-button").on("click", function(){
$(".homepage-container").hide();
$(".contact-container").removeClass("display-flex");
$(".services-sourcing-container").removeClass("display-flex");
$(".services-consulting-container").removeClass("display-flex");
$(".services-installation-container").removeClass("display-flex");
$(".about-container").addClass("display-flex");
});
$(".home-button").on("click", function(){
$(".homepage-container").show();
$(".about-container").removeClass("display-flex");
$(".services-sourcing-container").removeClass("display-flex");
$(".services-consulting-container").removeClass("display-flex");
$(".services-installation-container").removeClass("display-flex");
$(".contact-container").removeClass("display-flex");
});
$(".contact-button").on("click", function() {
$(".homepage-container").hide();
$(".about-container").removeClass("display-flex");
$(".services-sourcing-container").removeClass("display-flex");
$(".services-consulting-container").removeClass("display-flex");
$(".services-installation-container").removeClass("display-flex");
$(".contact-container").addClass("display-flex");
})
$(".sourcing-button").on("click", function() {
$(".homepage-container").hide();
$(".about-container").removeClass("display-flex");
$(".contact-container").removeClass("display-flex");
$(".services-consulting-container").removeClass("display-flex");
$(".services-installation-container").removeClass("display-flex");
$(".services-sourcing-container").addClass("display-flex");
})
$(".consulting-button").on("click", function() {
$(".homepage-container").hide();
$(".about-container").removeClass("display-flex");
$(".contact-container").removeClass("display-flex");
$(".services-sourcing-container").removeClass("display-flex");
$(".services-installation-container").removeClass("display-flex");
$(".services-consulting-container").addClass("display-flex");
})
$(".installation-button").on("click", function() {
$(".homepage-container").hide();
$(".about-container").removeClass("display-flex");
$(".contact-container").removeClass("display-flex");
$(".services-sourcing-container").removeClass("display-flex");
$(".services-consulting-container").removeClass("display-flex");
$(".services-installation-container").addClass("display-flex");
})
// run every 7s
setInterval('cycleImages()', 5000);
setInterval('cycleImages2()', 5000);
setInterval('cycleImages3()', 5000);
setInterval('cycleImages4()', 5000);
setInterval('cycleImages5()', 5000);
setInterval('cycleImages6()', 5000);
});
我要給這是一個嘗試,我同意鏈接推子功能結合在一起,反而會點擊功能的工作方式它應該爲每一個按鈕你擁有它的方式。看起來好像按鈕和按鈕被點擊後,它會簡單地將display-flex類添加到.about-container中,而不是其他任何東西 – jazzninja
對不起,我在閱讀這些行時犯了一個錯誤。答案已更新。 –
再次,我會試一試並在完成後關閉問題。謝謝您的幫助! – jazzninja