你需要使内容的div position: absolute
,然后把它们放在一个position: relative
容器内。试试这个:
.content-container {
position: relative;
width: 400px;
height: 400px;
}
.content-container div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
$("#link1").click(function() {
$('#content1').fadeIn('slow');
$('#content2').fadeOut('slow');
});
$("#link2").click(function() {
$('#content1').fadeOut('slow');
$('#content2').fadeIn('slow');
});
Updated fiddle
为了减少维修量增加时/删除链接,尝试像它使用类使元素更通用以下的方法必需的, data
属性保持相关元件之间的连接。
<a class="link" href="#" data-rel="content1">Link 1</a>
<a class="link" href="#" data-rel="content2">Link 2</a>
<a class="link" href="#" data-rel="content3">Link 3</a>
<a class="link" href="#" data-rel="content4">Link 4</a>
<a class="link" href="#" data-rel="content5">Link 5</a>
<div class="content-container">
<div id="content1">This is the test content for part 1</div>
<div id="content2">This is the test content for part 2</div>
<div id="content3">This is the test content for part 3</div>
<div id="content4">This is the test content for part 4</div>
<div id="content5">This is the test content for part 5</div>
</div>
$(".link").click(function() {
$('.content-container div').fadeOut('slow');
$('#' + $(this).data('rel')).fadeIn('slow');
});
More extensible update
这似乎工作的伟大,但如何将它站起来,如果我不得不说的10个链接,而不是只有两个? – fightstarr20
@ fightstarr20看到我的更新。通过使用泛型类和数据属性,我更容易维护它。它现在将自动工作x链接。 –
感谢这个它的伟大工程,我现在唯一剩下的问题是,当有以上内容,那么它,你点击一个链接显示每次跳转到顶部 - http://jsfiddle.net/Wqc9N/8/ – fightstarr20