简体   繁体   English

为什么打开导航栏时所有页面都移动?

[英]Why all the page move when I'm open my Navbar?

I'm trying to make the navbar when I'm open only the navbar move and not all the page move like now 我试图让导航栏时,我开放导航栏移动,而不是所有的页面举动像现在

My code below:- Head: 我的代码如下:-头:

<script src="/js/jquery.js"></script>
<script lang="ja" type="text/javascript">
    var i = 0;
    $(document).ready(function () {
        $("#b").click(function () {
            $("#menu").slideToggle("slow");
        });
    });

    function ToggleMenu(loc) {
        $("#menu").slideToggle("slow");

        setTimeout(function () {
            window.location.href = loc;
        }, 750);
    }
</script>

Body: 身体:

<form id="form1" runat="server">
    <img id="b" src="/img/ic_menu_black_48dp_2x.png" style="height: 30px; width: 30px; margin-top: -5px; cursor: pointer;" />
    <div id="menu" style="border: 1px solid black; padding: 8px 8px; display: none; text-align: center; font-size: 20px">
        <a onclick="ToggleMenu('/')" class="navbartxt">Home</a>
        <br />
        <br />
        <a onclick="ToggleMenu('/Account/Register.aspx')" class="navbartxt">Register</a>
        <br />
        <br />
        <a onclick="ToggleMenu('/Account/Login.aspx')" class="navbartxt">Login</a>
    </div>
    <br />
    <br />
    Hello
</form>

When the navbar open the word "Hello" going down. 当导航栏打开时,单词“ Hello”下降。 I want the word and the rest of the page not change the position. 我希望这个词和页面的其余部分不改变位置。 I am trying to do this with jquery but I am not successful. 我正在尝试使用jquery来执行此操作,但是我不成功。

It has to do with the positioning: try ... 它与定位有关:尝试...

<div id="menu" style="border: 1px solid black; padding: 8px 8px; display: none; text-align: center; font-size: 20px; position: absolute; background-color: white;">

Note the: position: absolute; 注意: position:绝对; background-color: white; 背景颜色:白色; added. 添加。

EDIT 编辑

Fiddle: https://jsfiddle.net/rfornal/wptb8fp3/ 小提琴: https : //jsfiddle.net/rfornal/wptb8fp3/

EDIT 2 编辑2

To set the width relative to the <form> as you asked below, width the form's width set, you also need to specify `position: relative;". 要按照以下要求设置相对于<form>的宽度,设置表单的宽度设置的宽度,还需要指定`position:relative;”。

<form id="form1" runat="server" style="width:20%; position:relative;">
  ...
  <div id="menu" style="border: 1px solid black; padding: 8px 8px; display: none; text-align: center; font-size: 20px; position: absolute; background-color: white; width:100%;">

... note the width's and position on the form. ...注意表格的宽度和位置。

It's probably a CSS problem. 这可能是CSS问题。 Under normal circumstances, all DOM elements are created as Blocks, meaning they appear below each other. 在正常情况下,所有DOM元素都将创建为“块”,这意味着它们彼此之间出现。 There are some exceptions by default (and forcibly by using "display: inline;"), such as the SPAN element. 默认情况下(并使用“ display:inline;”强制使用)有一些例外,例如SPAN元素。

What's probably going on is that your menu is not a Fixed or Absolute positioned element, which causes the rest of your page to adjust to the size-change of your menu. 可能发生的情况是您的菜单不是固定位置或绝对位置元素,这会导致页面的其余部分根据菜单的大小变化进行调整。 You can read more about position types here: 您可以在此处阅读有关职位类型的更多信息:

http://www.w3schools.com/csSref/pr_class_position.asp http://www.w3schools.com/csSref/pr_class_position.asp

use position:absolute; in your menu element
style="position:absolute;border: 1px solid black; padding: 8px 8px; display: none; text-align: center; font-size: 20px"

Here is a working example. 是一个工作示例。

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

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