使用 jQuery 在单击时显示/隐藏 div
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ctrl+C 和 Ctrl+V 编程会直接导致地狱...我认为你可以做这样的事情:
性能取决于很多东西(firebug 打开、div 的内容、div 的样式、半透明背景等) .) ——不仅仅是糟糕的 javascript。
Ctrl+C and Ctrl+V programming leads straight to hell... I think you can do something like that:
And performance depends on a lot of thing (firebug is on, divs' content, divs' style, half-transparent backgrounds etc.) -- not just poor javascript.
流畅性或缺乏流畅性并不是代码的错。有些浏览器速度很慢。这不是 JS 可以解决的问题。
您可以缩短代码,但我认为不会有任何实质性的性能改进。
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.
如果这是一种更干净的方法,您正在寻找这样的方法:
If it's a cleaner approach you're looking for how about something like this: