用浮动堆叠两个 DIV:右

发布于 2024-11-14 14:12:03 字数 1755 浏览 4 评论 0原文

嗨嗨,我正在尝试使用 DIV 创建一个向下滑动的菜单,但遇到了一个我无法真正弄清楚如何克服的问题。让我们看一下下面的代码:

<!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> 
    <title>test</title> 
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#menu").click(function () {
                $('#menuItem').slideDown('slow');
            });
        });
    </script>
</head> 
<body> 
    <div style="width: 500px; margin: 0px auto; padding: 10px;">
        <div id="menuItem" style="border: 1px solid blue; position: relative; z-index: 10; float: right; display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>
        <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
            My Menu
        </div>
    </div>

    <div style="margin: 50px 0px; padding: 0px; text-align: center;">
        <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
            <div style=" position: relative; z-index: 1; float: right;">Form Element</div>
        </div>
    </div>
</body>
</html>

我想要实现的就是让我的滑动菜单停留在我的表单元素 div 的顶部。请告知如何做到这一点。非常感谢!

:)

Hihi, I am trying to create a slide down menu using DIV, but hit a problem that I can't really figure out how to overcome. Let's take a look at the code below:

<!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> 
    <title>test</title> 
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#menu").click(function () {
                $('#menuItem').slideDown('slow');
            });
        });
    </script>
</head> 
<body> 
    <div style="width: 500px; margin: 0px auto; padding: 10px;">
        <div id="menuItem" style="border: 1px solid blue; position: relative; z-index: 10; float: right; display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>
        <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
            My Menu
        </div>
    </div>

    <div style="margin: 50px 0px; padding: 0px; text-align: center;">
        <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
            <div style=" position: relative; z-index: 1; float: right;">Form Element</div>
        </div>
    </div>
</body>
</html>

All I want to achieve is to make my slide down menu stay on top of my form element div. Please advice how can this be done. Many thanks!

:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携君以终年 2024-11-21 14:12:03

快速而肮脏:我为菜单添加了一个绝对定位的外部包含,然后将 top:40px 应用于内容 div 将其向下推以补偿菜单。

<div style="position:absolute;width:100%;">
    <div style="width: 500px; margin: 0px auto; padding: 10px; ">
        <div id="menuItem" style="border: 1px solid blue; position: relative; z-index: 10; float: right;  display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>
        <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
            My Menu
        </div>
    </div>
</div>

    <div style="margin: 50px 0px; padding: 0px; text-align: center;  position:relative; top:40px; ">
        <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
            <div style=" clear:both;  z-index: 1; float: right;">Form Element</div>
        </div>
    </div>
</body>
</html>

当我输入此内容时,dotty 已经在我面前用几乎相同的方法回答了。但是,在上面的代码中,各个菜单 div 按照您的需要正确地彼此相邻浮动,就像您在问题中发布的第一个代码中一样。

现在可能有一些 div 和样式元素有点多余。

编辑:我现在确实想到 dotty 代码中菜单的操作实际上可能是您想要的菜单。

Quick and dirty: I added an absolutely-positioned outer containing for the menu, and then applied top:40px to the content div to push it down to compensate for the height of the menu.

<div style="position:absolute;width:100%;">
    <div style="width: 500px; margin: 0px auto; padding: 10px; ">
        <div id="menuItem" style="border: 1px solid blue; position: relative; z-index: 10; float: right;  display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>
        <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
            My Menu
        </div>
    </div>
</div>

    <div style="margin: 50px 0px; padding: 0px; text-align: center;  position:relative; top:40px; ">
        <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
            <div style=" clear:both;  z-index: 1; float: right;">Form Element</div>
        </div>
    </div>
</body>
</html>

As I type this, dotty's already answered before me with a pretty much identical approach. However, in the code above, the individual menu divs are properly floating next to each other as you want them to, as they do in the first code you posted in the question.

There are probably some div and styling elements that are a little redundant there now.

Edit: It does occur to me now that the operation of the menu in dotty's code is actually probably how you intended for the menu to be.

无法言说的痛 2024-11-21 14:12:03

将 #menuItem div 放入 #menu div 内,并将 #menuItem div 的位置设置为绝对位置并删除其浮动。

<!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> 
    <title>test</title> 

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("#menu").click(function () {
            $('#menuItem').slideDown('slow');
        });
    });
</script>
</head> 
<body> 
<div style="width: 500px; margin: 0px auto; padding: 10px;">

    <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
        My Menu

        <div id="menuItem" style="border: 1px solid blue; position: absolute; z-index: 10; display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>

    </div>
</div>

<div style="margin: 50px 0px; padding: 0px; text-align: center;">
    <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
        <div style=" position: relative; z-index: 1; float: right;">Form Element</div>
    </div>
    </div>
</body>
</html>

Put the #menuItem div inside the #menu div, and set the #menuItem div's position to absolute and remove it's float.

<!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> 
    <title>test</title> 

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $("#menu").click(function () {
            $('#menuItem').slideDown('slow');
        });
    });
</script>
</head> 
<body> 
<div style="width: 500px; margin: 0px auto; padding: 10px;">

    <div id="menu" style="border: 1px solid blue; position: relative; z-index: 10; float: right; cursor: pointer;">
        My Menu

        <div id="menuItem" style="border: 1px solid blue; position: absolute; z-index: 10; display: none; cursor: pointer;">
            MenuItem1<br />
            MenuItem2<br />
            MenuItem3<br />
            MenuItem4<br />
            MenuItem5<br />
            MenuItem6<br />
            MenuItem7
        </div>

    </div>
</div>

<div style="margin: 50px 0px; padding: 0px; text-align: center;">
    <div style="width: 500px; margin: 0px auto; border: 1px solid red; padding: 10px; height: 500px;">
        <div style=" position: relative; z-index: 1; float: right;">Form Element</div>
    </div>
    </div>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文