コンテンツ 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');
});
より拡張可能なアップデート