Muhammad Muhammad - 1 month ago 8
Javascript Question

How to make left and right divs sticky to the top on scroll at some point using JavaScript or jQuery?

In the following example I am able to stick the right div to the top on scrolling on a certain point but the same method is not working for left div and it spoils whole layout because I have to make the div fixed and when it becomes fixed it flows over the page!

JSFiddle Here http://jsfiddle.net/xt9y7hqu/2/

Any help will be greatly appreciated. Thank



$(function() {
var ele = "#rightPanel";
$(window).scroll(
function(){
stick_right(ele);
});
$( window ).resize(function() {
stick_right(ele);
});
stick_right(ele);
});

function stick_right(ele){
$(ele).css("top", Math.max(0,($('#sticky-anchor').offset().top) - $(this).scrollTop()));
$(ele).css("left", $('#centerPanel').position().left + $('#centerPanel').outerWidth());
}

@import url('http://getbootstrap.com/dist/css/bootstrap.css');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class='navbar navbar-default'>
<h3>
Navbar
</h3>
</nav>
<div class='container' id='sticky-anchor'>
<div class='col-xs-3' id='leftPanel' style='background: pink'>
<h3>
Sticky Left div
</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</div>
<div class='col-xs-5' id='centerPanel' style='background: lightblue'>
<h3>
None Sticky center div
</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<div class='col-xs-4' id='rightPanel' style='background: lightgreen; position: fixed'>
<h3>
Sticky Right div
</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
</p>
</div>
</div>
<br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>




Answer

Here You go. Check out the WORKING FIDDLE of your example.

 $(function() {
    var ele = "#rightPanel";
    $(window).scroll(
        function(){
        stick_right(ele);
            var windowTop = $(window).scrollTop();
        var panelTop = $('#leftPanel').offset().top;

        if(windowTop > panelTop){
           $('#leftPanel').offset({'top': windowTop });
        }else{
           $('#leftPanel').offset({'top':$('#rightPanel').offset().top});
        }

        });
      $( window ).resize(function() {
        stick_right(ele);
      });
    stick_right(ele);
});

function stick_right(ele){
    $(ele).css("top", Math.max(0,($('#sticky-anchor').offset().top) - $(this).scrollTop()));
    $(ele).css("left", $('#centerPanel').position().left + $('#centerPanel').outerWidth());
}