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