我在一个框中有一个简短的介绍文本,如果用户想进一步阅读,他可以切换一个隐藏的 div 元素来显示其余内容。在这个介绍框下,我有一些其他元素,我总是想保持在那个位置。我希望切换的文本“显示在”固定内容的上方,并且我不希望它在切换的 div 打开时向下推。我尝试了各种 z-index 值、绝对和相对定位,但无济于事。有没有一个干净的基于 CSS 的解决方案?请帮忙!这是我正在尝试做的演示:
> <!DOCTYPE html PUBLIC "-//W3C//DTD
> XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html
> xmlns="http://www.w3.org/1999/xhtml"
> xml:lang="en"> <head> <style
> type="text/css"> .container{width:
> 400px;
> height: 500px;
> border: 1px dashed #999;
> } div.container{padding:0; margin:0} #morecontent{
> z-index: 100
> } #morecontent,.introcontent{background: #DFFAFF;} div#fixedcontent{background:
> #FFDFDF;
> z-index: -1;
> position: absolute;
> width: 400px
> } </style> <title>Toggle overlap - test</title> </head> <body>
> <div class="container"> <div id="">
> <script type="text/javascript">
> function toggle(obj){
> var el=document.getElementById('morecontent');
> if (el.style.display !='none'){
> el.style.display='none';
> }
> else {el.style.display='';
> }
> }
> </script>
> <p class="introcontent">Lorem ipsum dolor sit amet, consectetur
> adipiscing elit. Aenean in pede congue
> ipsum sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.<br />
> There's more if you press toggle...
>
> </p>
> <a href="JavaScript: toggle(this)">Toggle</a>
> <div id="morecontent" style="display:none;">
> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in
> pede congue ipsum sollicitudin
> pellentesque. Nunc t tortor dolor,
> sagittis nec, placerat vel, commodo
> sed, nunc. Vivamus bibendum molestie
> orci. Duis nec leo at libero fermentum
> molestie. Nam eu risus.
> </p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Aenean in pede congue ipsum
> sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.
> </p>
> </div>
>
> <div id="fixedcontent">
> <p>This should stay 'under' the toggled content!</p>
> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in
> pede congue ipsum sollicitudin
> pellentesque. Nunc t tortor dolor,
> sagittis nec, placerat vel, commodo
> sed, nunc. Vivamus bibendum molestie
> orci. Duis nec leo at libero fermentum
> molestie. Nam eu risus.
> </p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Aenean in pede congue ipsum
> sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.
> </p>
> </div> </div> </div> </body> </html>