window.onscroll = function() { if ($('#sideBar').offset().top + $('#sideBar').height() >= $('#liveCommentsBlock').offset().top) { $('#sideBar').css("position", "absolute"); } }; #main { width: 70%; margin-left: 15%; } #cont { position: relative; margin-top: 30px; width: 100%; } #mainContent { position: relative; float: left; width: 80%; } #mainContentArt { width: 86%; margin-left: 7%; } .article { width: 100%; height: 800px; background: black; margin-bottom: 30px; } #sideBar { position: fixed; width: 18%; left: 50%; margin-left: 17%; } #liveCommentsBlock { position: relative; width: 100%; height: 500px; background: #FCEFFC; margin-bottom: 10px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main"> <div id="cont"> <div id="mainContent"> <div id="mainContentArt"> <div class="article"></div> <div class="article"></div> <div class="article"></div> </div> </div> <div id="sideBar"> <div id="sideBarMenu"></div> <div id="sideBarSub"></div> </div> <div class="spacer" style="clear: both;"></div> </div> <div id="liveCommentsBlock"> <p>##COMMENTS##</p> </div> </div> The bottom line is that the #sideBar block #sideBar after scrolling to a certain place, must “stop” there. However, for now, it stops, but moves to the upper left corner of the #cont block. How to fix this?
positionat the end infixedwith the required coordinates? ..... on somerelativeandabsolutethe wedge did not converge - Alexey Shimanskyfixed, makes it follow the scroll? - SashaliveCommentsBlockexampleliveCommentsBlockway? ......+660this is the type at which distance liveCommentsBlock?)) - Alexey Shimanskycontblock, and660is the height of thesideBarblock, since the values.offset().bottomdoes not exist. - Sasha660is replaced by a trite one atsomeElement.heightso there are no magical ones Numbers - Alexey Shimansky