Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stick fixed div of height more than viewport to body

Tags:

html

jquery

css

I know about the positioning of div (fixed, absolute and relative). I can attach a fixed div to body so that it will stick to the same position while scrolling body. Here I am asking a little bit different question.

I have a sidebar with height more than the height of viewport and I want it to be fixed to the body. While scrolling the body, it should also scroll but once the bottom of the fixed div visible, it should not scroll along with body.

For example, the right sidebar of Facebook wall scrolls along with body and stops scrolling with body once the bottom of the right sidebar visible (fixed).

like image 749
Miraj Avatar asked Sep 23 '14 13:09

Miraj


People also ask

How do you fixed the position of a div?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.

How do you make a Div stay at the top?

The vertical position of the element to be stuck can also be modified with the help of the 'top' property. It can be given a value of '0px' to make the element leave no space from the top of the viewport, or increased further to leave space from the top of the viewport.


2 Answers

That is possible by placing your sidebar absolute and change that to fixed as soon as the windows scroll position passes the bottom.

The CSS:

#sidebar {
    height: 120%;
    width: 100px;
    border: 2px solid blue;
    padding: 20px;
    margin: 20px;
    position: absolute;
    top: 0;
}

The jQuery:

$(document).ready(function() {
    var bottomPos = $('#sidebar').outerHeight(true) - $(window).height();
    $(window).scroll( function() {
        if ( $(window).scrollTop() > bottomPos ) {
            $('#sidebar').css({'position':'fixed','top':'auto','bottom':'0px'});
        } else {
            $('#sidebar').css({'position':'absolute','top':'0px'});
        }
    });
});

And a demo.

like image 149
LinkinTED Avatar answered Sep 23 '22 16:09

LinkinTED


Here you have three tutorials for the intended task (first results out of google with query: "fixed sidebar on scroll")

http://www.waypointarts.com/blog/2013/06/29/fixing-a-side-bar-while-scrolling-until-bottom

http://andrewhenderson.me/tutorial/jquery-sticky-sidebar/

http://css-tricks.com/scrollfollow-sidebar/

Here is the code for one of the approaches:

CSS

#page-wrap { 
  width: 600px; 
  margin: 15px auto; 
  position: relative; 
}

#sidebar { 
  width: 190px; 
  position: fixed; 
  margin-left: 410px; 
}

jQuery

$(function() {

    var $sidebar   = $("#sidebar"), 
        $window    = $(window),
        offset     = $sidebar.offset(),
        topPadding = 15;

    $window.scroll(function() {
        if ($window.scrollTop() > offset.top) {
            $sidebar.stop().animate({
                marginTop: $window.scrollTop() - offset.top + topPadding
            });
        } else {
            $sidebar.stop().animate({
                marginTop: 0
            });
        }
    });

});
like image 29
henser Avatar answered Sep 26 '22 16:09

henser