I have a sidebar that has the position: sticky
added to it, but when I scroll past a certain point it stops being sticky?!
Tested in Chrome version: Version 61.0.3163.100 and Safari Version: 11.0
HTML:
<div class="sticky">
this should stick!
</div>
<div class="content">
this is content
</div>
CSS:
<style media="screen">
.sticky {
background-color: #ccc;
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
position: sticky;
top: 15px;
width: 100px;
float: left;
}
.content{
background-color: #eee;
height: 3000px;
width: 700px;
float: right;
}
</style>
Position sticky will most probably not work if overflow is set to hidden, scroll, or auto on any of the parents of the element. Position sticky may not work correctly if any parent element has a set height. Many browsers still do not support sticky positioning. Check out which browsers support position: sticky.
If the parent element has no height set then the sticky element won't have any area to stick to when scrolling. This happens because the sticky element is meant to stick/scroll within the height of a container.
An element with position: sticky; is positioned based on the user's scroll position. A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
position: sticky;
is not supported most of browsers http://caniuse.com/#feat=css-sticky
You can try something like this:
HTML:
<div class="sticky-block">
this should stick!
</div>
CSS:
.sticky {
position: fixed;
top: 15px;
}
JS:
var $stickyBlock = document.querySelector('.sticky-block');
var origOffsetY = $stickyBlock.offsetTop - 15; // 15 is your top margin
function onScroll() {
window.scrollY >= origOffsetY ? $stickyBlock.classList.add('sticky') :
$stickyBlock.classList.remove('sticky');
}
document.addEventListener('scroll', onScroll);
or jQuery:
var $stickyBlock = $('.sticky-block');
var origOffsetY = $stickyBlock.offset().top - 15; // 15 is your top margin
function onScroll() {
window.scrollY >= origOffsetY ? $stickyBlock.addClass('sticky') :
$stickyBlock.removeClass('sticky');
}
$(document).on('scroll', onScroll);
jsfiddle
Here, try this, I would say it's better for this rather than using "Sticky" and it doesn't use Jquery or anything just simple position fixed.
Html
<div class="sticky">
this should stick!
</div>
<div class="content">
this is content
</div>
Css
.sticky {
background-color: #ccc;
top: 15px;
width: 100px;
float: left;
position:fixed;
}
.content{
background-color: #eee;
height: 3000px;
width: 700px;
float: right;
}
https://jsfiddle.net/udxuh1qf/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With