【问题标题】:how to make a div scroll up and fix at the top如何使 div 向上滚动并固定在顶部
【发布时间】:2012-07-24 09:10:56
【问题描述】:

我是 jquery 的新手,希望在我向下滚动浏览器中的特定位置后让 div 向上滚动并固定在顶部,就像 http://www.airbnb.com/jobs/

【问题讨论】:

标签: jquery css


【解决方案1】:

试试下面的DEMO

这与我的其他答案 (Answer) 相同,只是更改了一些值。原生 JS 解决方案。

代码是:

JS:

window.onscroll=function () {
var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
var head = document.getElementById("header")
if(top > 550){
    head.style.position = "fixed";
    head.style.height="50px"
    head.style.top="0px"
    }
    else {
if(head.getAttribute("style"))
 head.removeAttribute("style")                
    }
}

HTML:

<div class="container">
  <div id="header" class="header">
    Hello World
  </div>
</div>

CSS:

.container{
 height:2000px;
 width:100%;
 background-color:yellow;
}

.header{
 text-align:center;
 background-color:red;
 height:100px;
 position:relative;
 min-height:50px;
 width:100%;
 top:500px;
}

希望对你有帮助

【讨论】:

  • +1 来自我,虽然你没有捕捉到的一个方面是每张图片显示的数量随着你向下滚动而变化(这很漂亮)。
【解决方案2】:

这样写:

$(window).scroll(function () {
                    var height = $('body').height();
                    var scrollTop = $(window).scrollTop();

                      if(scrollTop>500){
                          $('#header').css({ 'position': 'fixed', 'top' : '0' });
                    }
                      else{
                         $('#header').css({ 'position': 'relative', 'top': '500px'});
                       }
                });

查看http://jsfiddle.net/68eXM/14/

【讨论】:

    【解决方案3】:

    FIDDLE 演示:http://jsfiddle.net/collabcoders/PZp8R/3/

    扩展它以包含 jquery 动画,您将需要包含 jquery、jquery.effects.core.min.js、jquery.effects.slide.min.js 和 jquery.effects.transfer.min.js 文件。

    在您的 CSS 中,您需要将 div 的上边距设置为负数,这将是动画的起点。

    CSS

    .tophead {  
        width:100%;
        height: 100px;
        color:#fff;
        background-color:#111;
    }
    
    .container {
        height:2000px;
        width:100%;
        margin-top:-100px;
    }
    
    .header{
        text-align:center;
        background-color:#000;
        height:100px;
        position:relative;
        min-height:100px;
        width:100%;
        top:500px;
        color: #fff;
    }​
    

    在您的 javascript 中,这将使整个 div 在 3 秒内从 -100 像素下降到 0

    Javascript

    $(document).ready(function () {
        $(".container").animate({
             marginTop: "0"
        },2000);
    });
    
    $(window).scroll(function () {
        var height = $('body').height();
        var scrollTop = $(window).scrollTop();
        if(scrollTop>500){
             $('#header').css({ 'position': 'fixed', 'top' : '0' });
        } else {
             $('#header').css({ 'position': 'relative', 'top': '500px'});
       }
    });
    

    HTML

    <div class="container">
        <div id="tophead" class="tophead">
          Sliding Down Container
        </div>
      <div id="header" class="header">
        Hello World
      </div>
    </div>​
    

    【讨论】:

    • 想把这个放在小提琴里吗?
    【解决方案4】:

    很简单..就像这个例子:

    $(window).scroll(function () {
        var height = $('body').height();
        var scrollTop = $(window).scrollTop();
        if(scrollTop>100){
            $('#header').css({ 'position': 'fixed', 'top' : '0', 'opacity': '0.7'});
        } else {
             $('#header').css({ 'position': 'relative', 'top': '100px', 'opacity': '1'});
        }
    });
    

    http://jsfiddle.net/PZp8R/55/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多