I have a master form for my website, and I want to dock a div to the top of the content area inside the master form. This div should always be visible despite scroll position. Is this possible?
A picture would explain it better.
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.
Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.
To make an element sticky, do: make_sticky('#sticky-elem-id'); When the element becomes sticky, the code manages the position of the remaining content to keep it from jumping into the gap left by the sticky element. It also returns the sticky element to its original non-sticky position when scrolling back above it.
I posted a sample as a comment, so I suppose I'll write out a full answer to this.
The markup is pretty straight-forward, but there are some important notes for each section.
<div id="page">
<div id="header">
<div id="header-inner"> <!-- Note #1 -->
<img src="http://placehold.it/300x100" />
</div>
</div>
<div id="content">
<!-- Some Content Here -->
</div>
</div>
#page {
padding: 100px;
width: 300px;
}
#header,
#header-inner { /* Note #1 */
height: 100px;
width: 300px;
}
.scrolling { /* Note #2 */
position: fixed;
top: 0;
}
//jQuery used for simplicity
$(window).scroll(function(){
$('#header-inner').toggleClass('scrolling', $(window).scrollTop() > $('#header').offset().top);
//can be rewritten long form as:
var scrollPosition, headerOffset, isScrolling;
scrollPosition = $(window).scrollTop();
headerOffset = $('#header').offset().top;
isScrolling = scrollPosition > headerOffset;
$('#header-inner').toggleClass('scrolling', isScrolling);
});
The scrolling header will be attached to the top of the page using position: fixed
, but this style will remove the content from content flow, which will cause the content to jump unless its container maintains the original height.
Styles belong in CSS, however it may be difficult to properly align some elements with their original position. You may need to dynamically set the left
or right
css property via JavaScript.
You'll need jQuery or the like, see my fiddle here
Edit
jQuery function, where .form
is the content div and .banner
is the div that is docked
$(window).scroll(function() {
t = $('.form').offset();
t = t.top;
s = $(window).scrollTop();
d = t-s;
if (d < 0) {
$('.banner').addClass('fixed');
$('.banner').addClass('paddingTop');
} else {
$('.banner').removeClass('fixed');
$('.banner').removeClass('paddingTop');
}
});
.fixed {
position:fixed;
top:0px;
}
.paddingTop{
padding-top:110px;
}
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