简体   繁体   English

如何使DIV显示在其他Div之上

[英]how to make DIV show up on top of other Divs

I upload my code on jsFiddle, you can see it there. 我在jsFiddle上传我的代码,你可以在那里看到它。

http://jsfiddle.net/SrT2U/2/ http://jsfiddle.net/SrT2U/2/

When you click on the link, the hidden FAQ section will show up, and it will push other divs down. 当您单击该链接时,隐藏的常见问题解答部分将显示,并且它将推送其他div。 But that is not what I want, I need all other divs stay where they are, and my hidden FAQ section just float on the top. 但这不是我想要的,我需要所有其他div保持原状,我隐藏的常见问题部分只是漂浮在顶部。 Not sure how to do it. 不知道怎么做。 Not even sure if this should be done in HTML, CSS or jQuery. 甚至不确定是否应该在HTML,CSS或jQuery中完成。

My jQuery code: 我的jQuery代码:

$(function(){
    $(".OpenTopMessage").click(function () {
        $("#details").slideToggle("slow");
    });
});

HTML code: HTML代码:

<div style="border: 1px solid #000;">
    <span>link</span>
    <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
    <span>link</span>
    <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
    <span>link</span>
    <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
    <span>link</span>
    <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
    <span>link</span>
    <span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
</div>

<div id="faqBox">
   <table width="100%">
<tr><td><a href="#" id="openFAQ" class="OpenTopMessage">this is hte faq section</a></td>
</tr>
    </table>

    <div id="details" style="display:none">
    <br/><br/><br/><br/><br/><br/><br/><br/>
 the display style property is set to none to ensure that the element no longer affects the layout of the page
 <br/><br/><br/><br/><br/><br/><br/><br/>
    </div>
</div>
<br/><br/>
<div style="background:#c00;">other stuff heren the height reaches 0 after a hiding animation, the display style property is set to </div>  

An easy fix would be to just add position:absolute to your faqBox div. 一个简单的解决方法是只添加position:absolute到你的faqBox div。

jsFiddle example jsFiddle例子

Position:absolute takes the element out of the flow of the document and in this case, allows it to appear on top of your other element and not push it down. Position:absolute将元素从文档流中取出,在这种情况下,允许它出现在其他元素的顶部而不是将其向下推。

You could use position:absolute on the #details div. 您可以在#details div上使用position:absolute

Example: http://jsfiddle.net/SrT2U/9/ 示例: http //jsfiddle.net/SrT2U/9/

You would need to fiddle with the margin to get it to line up. 您需要调整margin以使其排队。


EDIT: Noticing that you are using margin:0 auto; 编辑:注意到您正在使用margin:0 auto; to center the FAQ box. 以FAQ框为中心。 You may need to find another way in order to line up the box. 你可能需要找到另一种方法来排队。


EDIT 2: 编辑2:

I noticed if you placed the FAQ div inside the #faqbox table and then change margin:0 auto; 我注意到你是否将FAQ div放在#faqbox表中,然后更改margin:0 auto; to margin-top: 20px; margin-left:-16px; margin-top: 20px; margin-left:-16px; margin-top: 20px; margin-left:-16px; in the #details div and it all works well. #details div中,一切正常。

Example 2: http://jsfiddle.net/SrT2U/13/ 示例2: http //jsfiddle.net/SrT2U/13/

NB: placing the div inside the table like so is not code to spec, but it does work re centering the FAQs. 注意:将div放在table是不符合规范的代码,但它确实可以重新定位常见问题解答。

If you want a div to show up on top of other elements without changing others' positions, you need to set it CSS property of position to absolute. 如果你想让div显示在其他元素之上而不改变其他元素的位置,你需要将它的CSS属性设置为绝对值。

In your css files specify the following: 在您的css文件中指定以下内容:

#details{
    position: absolute;
    left: 100px;//your desired position as regard to the left of your window
    top: 100px;//your desired position as regard to the top of your window
}

You can also do that using jQuery as following: 您也可以使用jQuery执行以下操作:

$(".OpenTopMessage").click(function () {
    $("#details").slideToggle("slow");
    $("#details").css("position", "absolute");
    $("#details").css("left", "40px/*some value*/");
    $("#details").css("top", "40px/*some value*/");
});

If you want to still locate your pop-up box at the center of your window, you can use jQuery to center it as following. 如果您仍想在窗口中心找到弹出框,可以使用jQuery将其置于中心位置。

$(".OpenTopMessage").click(function () {
    $("#details").slideToggle("slow");
    $("#details").css("position", "absolute");
    window_width = $(window).width(); //Get the user's window's width
    window_height = $(window).height(); //Get the user's window's height
    $("#details").css("left", (window_width-$("#details").width())/2);
    $("#details").css("top", (window_height-$("#details").height())/2);
});

Then your box will be centered. 然后你的盒子将居中。

Also, you will probably find your button "this is hte faq section" is covered by your div, but you can easily enable closing the pop-up box by adding a close button or add the following code: 此外,您可能会发现您的div所涵盖的按钮“this is hte faq section”,但您可以通过添加关闭按钮或添加以下代码轻松启用关闭弹出框:

$("body:not(#details)").click(function() {
    $("#details").slideToggle("slow");
});

This enables you to click on any part of the page to close the targeted div #details except for the div itself. 这使您可以单击页面的任何部分来关闭目标div #details,除了div本身。

Usually, if you are trying to make a pop-up box or dialog, you can try to use other pre-designed plugins, including jQuery UI ( http://jqueryui.com/dialog/ ) or blockUI ( http://www.malsup.com/jquery/block/ ), of which the former only supports dialogs but the latter supports all kinds of pop-up boxes. 通常,如果您尝试制作弹出框或对话框,可以尝试使用其他预先设计的插件,包括jQuery UI( http://jqueryui.com/dialog/ )或blockUI( http:// www .malsup.com / jquery / block / ),其中前者仅支持对话框,后者支持各种弹出框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM