使用 jQuery 在单击时显示/隐藏 div

发布于 2024-09-06 11:40:17 字数 1923 浏览 10 评论 0原文

我有 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');
  });
});

I've got 4 divs and on click of the navigation I want to show one of them and hide the others. I have it working but I feel its not as smooth as I know it could be, its definitely my code that needs to be refactored! Heres what I have.

$('#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');
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-09-13 11:40:17

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');
    });
}

性能取决于很多东西(firebug 打开、div 的内容、div 的样式、半透明背景等) .) ——不仅仅是糟糕的 javascript。

Ctrl+C and Ctrl+V programming leads straight to hell... I think you can do something like that:

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');
    });
}

And performance depends on a lot of thing (firebug is on, divs' content, divs' style, half-transparent backgrounds etc.) -- not just poor javascript.

永不分离 2024-09-13 11:40:17

流畅性或缺乏流畅性并不是代码的错。有些浏览器速度很慢。这不是 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');
  });
});

The smoothness or lackthereof is not the fault of your code. Some browsers are slow. It's just not something that can be fixed from JS.

You can make your code shorter but I don't think there will be any material performance improvements.

$('#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');
  });
});
贱贱哒 2024-09-13 11:40:17

如果这是一种更干净的方法,您正在寻找这样的方法:

$(".my4Divs").click(function() {
    var self = this;
    $(".my4Divs").each(function() {
       if(self == this) {
          $(self).addClass("selected");
          ...
          ...
          return;
       }
       $(this).removeClass("selected");
       ...
       ...
    });
});

If it's a cleaner approach you're looking for how about something like this:

$(".my4Divs").click(function() {
    var self = this;
    $(".my4Divs").each(function() {
       if(self == this) {
          $(self).addClass("selected");
          ...
          ...
          return;
       }
       $(this).removeClass("selected");
       ...
       ...
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文