【问题标题】:How to keep a part of a page fixed on top when scroling滚动时如何将页面的一部分固定在顶部
【发布时间】:2016-01-02 06:17:57
【问题描述】:

当页面有足够的内容需要向下滚动时,标题和菜单将消失。现在,标题不是问题,但我真的很希望菜单保持固定在页面顶部,同时仍然向下滚动内容。

我在这里发布带有该标题和菜单的页面代码

<html>
<head>
<noscript><meta http-equiv="REFRESH" content="0;URL=error-no-javascript.html"></noscript>
<link rel="shortcut icon" href="http://www.vtsim.com/favicon.png" />
<title>TRAINZ - VTsim.com</title>
</head>
<body ondragstart="return false" onselectstart="return false" oncontextmenu="return false" link="red" alink="red" vlink="red">
<font face="Arial" size="2" color="black">

<p align="center">
<img src="http://www.vtsim.com/image/head_trz_1.png"><a href="http://fs.vtsim.com" border="0"><img src="http://www.vtsim.com/image/head_trz_2.png"></a>
</p>

//--- now starts the menu wich I pretend to be kept fixed on top when scrolling

<p align="center">
<a href="http://trainz.vtsim.com" border="0"><img src="http://www.vtsim.com/image/b_home_a.png"></a><a href="http://trainz.vtsim.com/trains.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_trains_i.png"></a><a href="http://trainz.vtsim.com/tracks.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_tracks_i.png"></a><a href="http://trainz.vtsim.com/trackside.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_trkside_i.png"></a><a href="http://trainz.vtsim.com/scenery.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_scenery_i.png"></a><a href="http://trainz.vtsim.com/routes.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_routes_i.png"></a><a href="http://trainz.vtsim.com/others.htm" border="0"><img src="http://www.vtsim.com/image/b_trz_others_i.png"></a><a href="mailto:vasco@vtsim.com" border="0"><img src="http://www.vtsim.com/image/b_email.png"></a><br />
<img src="http://www.vtsim.com/image/sep_800.png">
</p>

//--- and the menu code ended here

<p align="center">
Contents will display here
</p>

</font>
</body>
</html>

【问题讨论】:

标签: html css


【解决方案1】:

基本上将您的标头包装在一个容器中,并带有一点jQuery,如下所示:

$(window).scroll(function () {
            if ($(window).scrollTop() > 226) {
                $(".header").addClass("fixed");
            } else {
                $(".header").removeClass("fixed");
            }
        });

向下滚动 226 像素(内容超过标题高度)后,您向该容器添加一个类并将其设为 fixed。

css:

.fixed {
    position:fixed;
    width:100%;
    top:0;
    left:0;        
}

FIDDLE

编辑:要在您的网络中轻松实现此脚本,只需在您的 html 中的 &lt;/head&gt; 标记之前添加此代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(window).scroll(function () {
            if ($(window).scrollTop() > 226) {
                $(".header").addClass("fixed");
            } else {
                $(".header").removeClass("fixed");
            }
        });
    </script>
    <style type="text/css">
        .fixed {
        position:fixed;
        width:100%;
        top:0;
        left:0;        
    }
    </style>

并且不要忘记添加&lt;div class="header"&gt;,就像在小提琴示例中一样。

【讨论】:

  • 这太棒了!!当我弄清楚我应该在哪里(或在 wich 文件上)实现 CSS 和 jquery 代码时,我的问题将得到完全回答。我最初做了一个简短的演示,但它经过编辑和大量删减,我解释说除了简单的 html 和复制/粘贴之外,我是一个完全的菜鸟。在此先感谢您,但我在实施方面需要进一步的帮助。
  • 我编辑了我的答案,让您知道在哪里添加代码
【解决方案2】:

我这样做的方式,我认为这是一种非常标准的方式,一旦窗口的滚动位置到达标题的顶部,就在标题上设置一个 .fixed 的 css 类。

此方法需要 javascript(更具体地说是 jQuery)!

$(function() {
  createSticky(jQuery("#header"));
});

function createSticky(sticky) {

  if (typeof sticky !== "undefined") {

    var pos = sticky.offset().top,
        win = jQuery(window);

    win.on("scroll", function() {
      win.scrollTop() >= pos ? sticky.addClass("fixed") : sticky.removeClass("fixed");
    });
  }
}
#header {
  background: #666;
  padding: 3px;
  padding: 10px 20px;
  color: #fff;
}
.fixed {
  position: fixed;
  top: 0;
  width: 100%;
}
body {
  height: 5000px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>stuff that might be above the header</p>

<div id="header">
  Home
</div>

仅 CSS

当浏览器支持增加一点时,您可以使用position: sticky,它基本上可以在没有 javascript 的情况下做同样的事情。但是,如果您的用户群是 Firefox,那么您可以继续使用,因为 Firefox 已经支持它一段时间了。

http://caniuse.com/#feat=css-sticky

【讨论】:

    【解决方案3】:

    您可以使用DOM的scrolltop属性、位置CSS属性和少量JavaScript代码来实现它,即将onscroll事件添加到body标签(o添加事件监听器)

    <body onscroll="calculate_top()"> 
    

    和id="myMenu" 在您的菜单中

    //--- now starts the menu wich I pretend to be kept fixed on top when scrolling
    <p align="center" id="myMenu">
        . . .
        <img src="http://www.vtsim.com/image/sep_800.png">
    </p>
    //--- and the menu code ended here
    

    和Javascript

    function calculate_top() {
        var y = 0;    
        element=document.getElementsByTagName("body");
    
        y=element[0].scrollTop;
        //you can see y values while you scrolling
        console.log(" y = " + y);
        if( y > 140){//old style
            document.getElementById("myMenu").style.position='fixed';
            document.getElementById("myMenu").style.top=0+'px';
        }else{
            document.getElementById("myMenu").style.position='relative';
        }
    }
    

    140 值是菜单顶部和屏幕顶部之间的度量(您可以根据需要更改)

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2013-02-08
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多