In the content area of my page, a dynamic number of rows are generated. Each row consists of two columns: a side block and a content area. The goal is to have the side block stick while the page scrolls down until the next block appears and pushes the previous one away, becoming sticky itself. Below is the HTML structure:
<div class="container">
<div class="row">
<div class="col-md-4 sticky">
<h2>Example</h2>
<p class="page-side-block">...</p>
<a href="#" class="action-button">...</a>
</div>
<div class="col-md-8 col-md-offset-4">
<p>...</p>
</div>
</div>
...
I've managed to make the first block sticky, but I'm unsure how to achieve the desired functionality for subsequent blocks. Any assistance would be appreciated.
Below is the JavaScript code (using mootools) that I am currently using. Please note that it may not work with the provided HTML markup, as I have made adjustments for clarity:
window.onscroll = function() {
var stickyBlock = $('sticky-block');
if (window.getScroll().y > 235) {
stickyBlock.setStyles({
position: 'fixed',
top: '100px',
width: "350px"
});
} else if (window.getScroll().y < 235) {
stickyBlock.setStyles({
position: 'absolute',
top: 0,
width: null
});
}
}
It's worth noting that the new w3c specs have introduced a position: sticky;
property, which could eliminate the need for JavaScript in this scenario. Unfortunately, browser support for this feature is currently limited, as indicated here.
If anyone has insight on resolving this issue, I would greatly appreciate your input. Thank you!