Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position: sticky stops half way?

Tags:

css

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>
like image 867
Hanna Stone Avatar asked Oct 06 '17 10:10

Hanna Stone


People also ask

Why does my position sticky not work?

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.

Why position sticky is not working in Chrome?

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.

What is the difference between position sticky and fixed?

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).


2 Answers

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

like image 192
xixe Avatar answered Oct 12 '22 19:10

xixe


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/

like image 41
Shaun Moore Avatar answered Oct 12 '22 21:10

Shaun Moore