您需要制作内容 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');
});
更新的小提琴
为了减少添加/删除链接时所需的维护量,请尝试以下方法,该方法使用类使元素更通用,并使用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');
});
更可扩展的更新