我有4个div,点击导航我要显示其中一个并隐藏其他的。我有它的工作,但我觉得它不像我所知道的那么顺利,它绝对是我需要重构的代码!这是我拥有的。
$('#details-speakers').click(function() {
$('#home').slideUp('slow', function() {});
$('#sessions-content').slideUp('slow', function() {});
$('#cases-content').slideUp('slow', function() {});
$('html,body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#speakers-content').slideDown('slow', function() {
$('#details-speakers').addClass('selected');
//Remove other classes
$('#details-sessions').removeClass('selected');
$('#details-cases').removeClass('selected');
$('#details-workshops').removeClass('selected');
});
});
$('#details-sessions').click(function() {
$('#home').slideUp('slow', function() {});
$('#speakers-content').slideUp('slow', function() {});
$('#cases-content').slideUp('slow', function() {});
$('html,body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#sessions-content').slideDown('slow', function() {
$('#details-sessions').addClass('selected');
//Remove other classes
$('#details-speakers').removeClass('selected');
$('#details-cases').removeClass('selected');
$('#details-workshops').removeClass('selected');
});
});
$('#details-cases').click(function() {
$('#home').slideUp('slow', function() {});
$('#speakers-content').slideUp('slow', function() {});
$('#sessions-content').slideUp('slow', function() {});
$('html,body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#cases-content').slideDown('slow', function() {
$('#details-cases').addClass('selected');
//Remove other classes
$('#details-speakers').removeClass('selected');
$('#details-sessions').removeClass('selected');
$('#details-workshops').removeClass('selected');
});
});
答案 0 :(得分:2)
Ctrl + C和Ctrl + V编程直接导致地狱......我想你可以这样做:
var divs = $('#details-speakers, #details-sessions, #details-cases');
divs.click(function () {
divs.not(this).add('#home').slideUp('slow');
// animation with home and body
$(this).slideDown('slow', function () {
$(this).addClass('selected');
divs.not(this).removeClass('selected');
});
}
性能取决于很多事情(萤火虫打开,divs'内容,divs'风格,半透明背景等) - 不仅仅是糟糕的javascript。
答案 1 :(得分:1)
顺畅或缺乏不是代码的错误。有些浏览器很慢。这不是可以从JS修复的东西。
您可以缩短代码,但我认为不会有任何重大的性能改进。
$('#details-speakers').click(function() {
$('#home, #sessions-content, #cases-content').slideUp('slow');
$('body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#speakers-content').slideDown('slow', function() {
$('#details-speakers').addClass('selected');
//Remove other classes
$('#details-sessions, #details-cases, #details-workshops').removeClass('selected');
});
});
$('#details-sessions').click(function() {
$('#home, #speakers-content, #cases-content').slideUp('slow');
$('body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#sessions-content').slideDown('slow', function() {
$('#details-sessions').addClass('selected');
//Remove other classes
$('#details-speakers, #details-cases, #details-workshops').removeClass('selected');
});
});
$('#details-cases').click(function() {
$('#home, #speakers-content, #sessions-content').slideUp('slow');
$('body').animate({scrollTop: $("#details").offset().top - 16}, 200, "swing");
$('#cases-content').slideDown('slow', function() {
$('#details-cases').addClass('selected');
//Remove other classes
$('#details-speakers, #details-sessions, #details-workshops').removeClass('selected');
});
});
答案 2 :(得分:0)
如果这是一种更清洁的方法,那么你正在寻找类似的东西:
$(".my4Divs").click(function() {
var self = this;
$(".my4Divs").each(function() {
if(self == this) {
$(self).addClass("selected");
...
...
return;
}
$(this).removeClass("selected");
...
...
});
});