Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在holding div中的javascript浮动菜单栏_Javascript - Fatal编程技术网

在holding div中的javascript浮动菜单栏

在holding div中的javascript浮动菜单栏,javascript,Javascript,我的网页上有一个JavaScript菜单栏,当浏览器栏到达菜单顶部时,它会锁定在一个固定位置,并随窗口移动。但是,我需要将菜单包含在一个div中,如何才能做到这一点 这是我的菜单栏: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script type='text/javascrip

我的网页上有一个JavaScript菜单栏,当浏览器栏到达菜单顶部时,它会锁定在一个固定位置,并随窗口移动。但是,我需要将菜单包含在一个div中,如何才能做到这一点

这是我的菜单栏:

<script type="text/javascript"     src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type='text/javascript'>
$(window).load(function(){
    $(window).scroll(function(){
        if ($(window).scrollTop() >= 200)
        {
            $("#floatbar").css({position:'fixed',left:'0',top:'0'});
        }
        else
        {
            $("#floatbar").css({position:'absolute',left:'0',top:'200px'});
        }
    });
});
</script>
其中,menu runner是菜单的包含div,而floatbar显然包含运行JavaScript的菜单


但是,当我尝试这段代码时,菜单会从顶部向左移动200px,而不是在menu runner div中。如何使浮动条定位在menu runner div中,然后在div中使用JavaScript向下滚动,就像它应该的那样。

if($(window).scrollTop()>=0)
是否有任何特殊原因需要将其包装在div中?你想实现什么?菜单将被放置在两列布局的右侧,左侧列包含文本,右侧列将是菜单栏,当浏览者查看数据时,菜单栏会向下移动,因此他们可以快速从页面上的一个位置跳到另一个位置(菜单仅链接到页面上的ID)。我不能使用绝对定位来实现这一点,因为屏幕大小不同,菜单栏将位于不同的位置。我可以使用AP从屏幕顶部定位菜单,但在任何其他大小的屏幕上,水平轴的定位都是错误的。不确定这是否有帮助,因为它不会修复您自己的代码,但是这似乎是解决你问题的办法?
<div id="menu_runner">
    <div id="floatbar">
        <a href="#issue49">Issue 49</a><br />
        <a href="#issue48">Issue 48</a><br />
        <a href="#issue47">Issue 47</a><br />
        <a href="#issue46">Issue 46</a><br />
    </div>
</div>
#menu_runner {
    width: 100px;
    height: 2000px;
    float: right;
    position: relative;
}
#floatbar {
    width: 70px;
    position: absolute;
    top: 200px;
}
#floatbar {
     position:fixed;
     top: 10px;  /* tells browser the div should position at the very bottom of the window */
     right: 1px;  /* tells the browser the div should have no space on the right */
     left: 1px;  /* tells the browser the div should have no space on the left */
     margin: 1px; /* because we want this width: 100%, the margin must be 0 */
     padding: 1px; /* because we want the width: 100%, the padding must be 0 */
     width: 10px; /* makes the div a bar stretching across the bottom of the screen */
     height: 35px; /* makes the floating bar 35 pixels high*/
     z-index: 9999; /*positions this div on top of all other elements in the site - this number can increase or decrease to your liking */
     border:1px solid #000;
}