jQuery - 在滚动中的特定点之后/之后添加一个类,上面有可变长度的内容

时间:2010-08-20 02:26:57

标签: javascript jquery css

一般来说jQuery和JavaScript都是新手。我在 http://jsbin.com/alibi3/2/ 上嘲笑了我的问题示例 - 下面有解释。

我有一个div,当用户滚动浏览页面上的某个点后,会被分配一个“固定”类,以便它跟随用户向下移动页面。这样就可以了。

问题是,div之上的内容可以切换为显示/隐藏 - 当显示时,固定类仍然被应用于它隐藏的那一点,所以它似乎'跳”。

如何告诉我的固定类添加函数上面的div已显示/隐藏,从而调整添加“固定”类的点?

感谢。

HTML:

<div id="drawer">
    <a href="#">Click here to toggle drawer</a>
    <p id="drawercontents">Here is the stuff in the drawer; hidden by default.</p>
</div>
<div id="article">
    blah, blah...
</div>
<div id="nav">
    This should follow down the page once we scroll past the start of the article,
    and 'stick' back in place when we are back at the top.
</div>

CSS:

  #article {
    width: 400px;
    float: left;
    margin-right: 20px;
    padding: 20px;
    background: #eee;
  }
  #nav {
    width: 200px;
    float: left;
    background: #ff0;
    padding: 20px;
  }
  #drawer {
    width: 660px;
    padding: 20px;
    color:#fff;
    background: #000;
    margin-bottom: 10px;
  }
  .fixed { position: fixed; left: 460px; top: 0px; }

的JavaScript:

$(document).ready(function() {
  $('#drawercontents').hide();

  $('#drawer a').click(function(e){
    e.preventDefault();
    $('#drawercontents').toggle('fast');
  });

  var top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function () {
    // what is the y position of the scroll?
    var y = $(window).scrollTop();    
    // whether that's below the start of article?
    if (y >= top) {
      // if so, add the fixed class
      $('#nav').addClass('fixed');
    } else {
      // otherwise, remove it
      $('#nav').removeClass('fixed');
    }
  });
});

2 个答案:

答案 0 :(得分:2)

每当你做一些修改“固定”div的基本位置的东西时,你需要重新快照它的基本位置。

例如,在您的演示代码中,重新测量toggle()来电中的顶部 请参阅下面的修改后的代码,或在http://jsbin.com/alibi3/8处查看它。

var GblTop;

function GetVertOffset (srchStr)
{
    GblTop = $(srchStr).offset().top - parseFloat($(srchStr).css('marginTop').replace(/auto/, 0));
}


$(document).ready(function ()
{
    $('#drawercontents').hide();

    $('#drawer a').click (function (e)
    {
        e.preventDefault();
        $('#drawercontents').toggle('fast', function() {GetVertOffset ('#nav'); } );
    });


    GetVertOffset ('#nav');     //-- Sets GblTop

    $(window).scroll (function ()
    {
        // what is the y position of the scroll?
        var y = $(window).scrollTop();
        // whether that's below the start of article?
        if (y >= GblTop)
        {
            // if so, add the fixed class
            $('#nav').addClass('fixed');
        }
        else
        {
            // otherwise, remove it
            $('#nav').removeClass('fixed');
        }
    });

});

答案 1 :(得分:0)

自己想出来!如果其他人有同样的问题并遇到这个问题,这里是我修改后的代码和最好的(虽然是业余的)解释我做了什么:

JS:

$(document).ready(function() {
  $('#drawercontents').hide();
  var top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0));
  $('#drawer a').click(function(e){
    e.preventDefault();
    $('#drawercontents').toggle('fast', function() {
      top =$('#nav').offset().top - parseFloat($('#nav').css('marginTop').replace(/auto/, 0));
    });                                              
  });
  $(window).scroll(function () {
    // what is the y position of the scroll?
    var y = $(window).scrollTop();    
    // whether that's below the start of article?
    if (y >= top) {
      // if so, add the fixed class
      $('#nav').addClass('fixed');
    } else {
      // otherwise, remove it
      $('#nav').removeClass('fixed');
    }
  });
});

HTML:

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  body { margin: 0; padding: 0; }
  #article { width: 400px; height: 1000px; float: left; margin-right: 20px; padding: 20px; background: #eee; }
  #nav { width: 200px; float: left; background: #ff0; padding: 20px; }
  #drawer { width: 660px; padding: 20px; color:#fff; background: #000; margin-bottom: 10px; }
  .fixed { position: fixed; left: 460px; top: 0px; }
</style>
</head>
<body>
  <div id="drawer">
    <a href="#">Click here to toggle drawer</a>
    <p id="drawercontents">Here is the stuff in the drawer. It is hidden by default. When it is shown there is a problem with the nav jumping to the top of the page when we scroll.</p>
  </div>
  <div id="article">
    <h2>This is a long article!</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit pellentesque sed egestas id, iaculis ut erat. Praesent ut neque vel dolor lacinia eleifend at sit amet sem, etc etc</p>

    <p>Div height has been set to 1000px to force scrolling without adding too much filler text</p>
  </div> <!-- end article -->
    <div id="nav">
     This should follow down the page once we scroll past the start of the article, and 'stick' back in place when we are back at the top.
  </div>
</body>
</html>​​

JS Bin上的工作示例 - http://jsbin.com/alibi3/9

top被设置为全局变量,因此可以在函数之间使用它。首先,它是在文档加载时设置的,然后每当运行抽屉切换功能$('#drawer a').click(function(e)时重新定义它。

现在,每当运行$(window).scroll时,它都具有正确的值top,并且行为与我想要的一样。