开发者

jQuery – Adding a class at/after a specific point in scroll, with variable-length content above

开发者 https://www.devze.com 2023-01-12 03:25 出处:网络
Fairly new to jQuery and JavaScript in general. I mocked up an example of my problem at http://jsbin.com/alibi3/2/ - with an explanation below.

Fairly new to jQuery and JavaScript in general. I mocked up an example of my problem at http://jsbin.com/alibi3/2/ - with an explanation below.

I have a div that, after a user scrolls past a certain point on the page, is assigned a class of "fixed" so it follows the user down the page. This works fine on its own.

Problem is, the content above that div can be toggled to show/hide - and when it is shown, the fixed class is still being applied at the point it would have been if it was hid开发者_StackOverflowden, so it appears to 'jump'.

How do I tell my fixed-class-adding function that the div above has been shown/hidden, and so adjust the point at which the 'fixed' class is added?

Thanks.

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');
    }
  });
});


Whenever, you do something that modifies the base position of that "fixed" div, you need to re-snapshot its base position.

For example, in your demo code, remeasure the top inside the toggle() call.
See the modified code, below, or see it in action at 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');
        }
    });

});


Figured it out myself! In case anyone else has the same problem and comes across this, here's my revised code and best (albeit amateurish) explanation of what I did:

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>​​

Working example on JS Bin – http://jsbin.com/alibi3/9

top is set as a global variable so it can be used between functions. First it is set on document load, then it is redefined whenever the drawer-toggling function $('#drawer a').click(function(e) is run.

So now, whenever $(window).scroll is run, it has the right value of top to work with and behaves like i want it to.

0

精彩评论

暂无评论...
验证码 换一张
取 消