jquery - How do I scroll an element with the page, but confine it to it's parent DIV? -


i using method described here: http://www.wduffy.co.uk/blog/keep-element-in-view-while-scrolling-using-jquery/

initially, can working (current cdn loaded jquery) when scrolling bottom of page sidebar element carries on scrolling down, makes page longer, scrolls down again creating infinite scroll.

i want confine sidebar within parent element - in case main-container. when element hits bottom of div, want stop scrolling. i've attempted manipulate answer here: jquery scrolling div - prevent entering site footer little success. clues?

#html structure  <!doctype html> <html> <body>     <div id="container">             <div id="content-container">             <script>                 $().ready(function() {                           var $scrollingdiv = $("#sidebar");                      $(window).scroll(function(){                                     var y = $(this).scrolltop(),                             maxy = $('#secondary-container').offset().top,                             scrollheight = $scrollingdiv.height(),                             offset = 30;                          if(y< maxy-scrollheight-offset ){                             $scrollingdiv                             .stop()                             .animate({"margintop": ($(window).scrolltop()+offset ) + "px"}, "slow" );                                 }                         });                 });             </script>              <h1>header</h1>             <div id="intro-text">                 <h3>sub header</h3>                 <p>                      blah blah blah                 </p>             </div>             <div id="main-container">                 <div id="main"> <!-- repeat x times -->                     <div class="listing">                         <p class="listing-description">                             blah                         </p>                         <p class="listing-response">                             blah                         </p>                         <br />                         <a href="#">linky</a>                     </div> <!-- end repeat -->                 </div>                 <div id="sidebar">                     <h3>floaty box content</h3>                     blah                 </div>             </div>             <div id="secondary-container">                 <div id="secondary-left">                 </div>                 <div id="secondary-right">                 </div>             </div>             <div id="footer">             </div>         </div>     </div> </body> </html>   # css   #container {     padding:10px;     margin:10px;     margin:auto;     width:1000px;     overflow:auto; }   .listing {     margin-bottom:5px;     position:relative;     clear:both;     overflow:auto;     width:800px; }  .listing-description {     float:left;     width: 49%;     font-weight:bold; }  .listing-response {     float:left;     width:50%;     padding-left:5px; }  #main-container {     clear:both;     overflow:auto; }  #main {     float:left; }  #sidebar {     float:left;     padding-left:10px; }  #secondary-container {     clear:both;     overflow:auto;     border: 1px solid; }  #secondary-left {     width:50%;     float:left; }  #secondary-right {     width:50%;     float:left; } 

i've ended this:

$(window).scroll(function(){               var y = $(this).scrolltop(),   maxy = ($('#secondary-container').offset().top - $('#sidebar').height() - 110),   scrollheight = $scrollingdiv.height();    if(y< maxy-scrollheight){     $scrollingdiv     .stop()     .animate({"margintop": $(window).scrolltop() + "px"}, "slow" );           }     }); 

subtracting #sidebar hight maxy variable, plus magic 110 number. i'm not 100% on magic number comes from, after playing around it, fits needs. mileage may vary.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -