I've made some progress on my code:
HTML
<div id="header"></div>
<div id="content">
<div class="sidebar-wrapper"></div>
</div>
<div class="session-wrapper"></div>
<div id="footer"></div>
CSS
#header {
height: 600px;
width: 100%;
background: black;
}
#content {
height: 2000px;
width: 100%;
background: #eeeeee;
}
.sidebar-wrapper {
width: 350px;
height: 350px; /*depends on content*/
background: rgba(0,0,0,0.2);
border-radius: 20px;
padding: 20px;
margin-top: 50px;
}
.session-wrapper {
height: 200px;
width: 100%;
background: #000000;
}
#footer {
width: 100%;
height: 500px;
background: #888;
}
jQuery
jQuery(window).scroll(function(e){
var sidebar = jQuery('.sidebar-wrapper');
var sessions = jQuery('.session-wrapper');
var scrollTop = jQuery(window).scrollTop(),
elementOffset = sidebar.offset().top,
distance = (elementOffset - scrollTop),
sessionOffset = sessions.offset().top;
var isPositionFixed = (sidebar.css('position') == 'fixed');
if(distance < 60 && (sessionOffset - elementOffset) > 800 && !isPositionFixed) {
sidebar.css({'position': 'fixed', 'top': '100px', 'width': '350px'});
}
if((sessionOffset - elementOffset) < 800 && isPositionFixed || scrollTop > 1900) {
sidebar.css({'position': 'static', 'top': '0px', 'width': '100%'});
}
});
To clarify, I am working on a solution where the grey box (sidebar-wrapper
) sticks to a certain position while scrolling down the page until it's close to .sessions-wrapper
, then it should hold that position until scrolling back up resumes. I believe I'm close to achieving this effect but may need some guidance...
Appreciate any help provided!