Sticky Element
by skydivematy
HTML
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<div id="outer-wrap">
<div id="inner-wrap">
<div id="container">
<div class="wrapper cf">
<header class="header">
</header>
<div class="content">
<h2>Scroll Down!</h2>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae
est. Mauris placerat eleifend leo.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque vero, maxime nulla dolore iure voluptatem itaque. Nisi commodi ut consectetur beatae mollitia. Ab, maxime quisquam ipsa, quibusdam itaque et eius.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, molestiae.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus suscipit blanditiis delectus quos, soluta fuga voluptatem, facere inventore neque voluptate quaerat unde laboriosam molestiae repellat, sapiente accusamus cumque! Ipsam, illo!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita, molestiae.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus suscipit blanditiis delectus quos, soluta fuga voluptatem, facere inventore neque voluptate quaerat unde laboriosam molestiae repellat, sapiente accusamus cumque! Ipsam, illo!</p>
</div>
<div id="sidebar" class="sidebar">
<h3>Sidebar</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab maxime fugiat perspiciatis.</p>
</div>
</div>
</div>
</div>
<span id="bound"></span>
</div>
CSS
/* apply a natural box layout model to all elements, but allowing components to change */
html {
box-sizing: border-box;
}
body {
background: lightblue!important;
}
*,
*:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #F5F7FA;
line-height: 1.2;
}
.cf:before,
.cf:after {
content: " ";
/* 1 */
display: table;
/* 2 */
}
.cf:after {
clear: both;
}
.cf {
*zoom: 1;
}
#inner-wrap {
background: none;
background-color: transparent;
height: 5000px;
}
#container {
width: 90%;
width: 90vw;
height: 100%;
max-width: 126.000em;
margin: 0px auto;
}
.container-wrapper {
-webkit-box-shadow: 0px 0px 10px 2px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 10px 2px rgba(0, 0, 0, 0.3);
background: white;
}
.wrapper {
width: 100%;
}
.header {
width: 100%;
height: 200px;
display: block;
position: relative;
display: block;
Background: #ccc;
}
h2 {
font-size: 1.2em!important;
font-weight: bold!important;
text-align: center;
}
.content {
padding: 0 15px;
background: white;
width: 66.666%;
}
.content h1 {
margin-top: 0;
}
.content p,
.sidebar p {
font-family: "BLOKKNeue-Regular";
}
.sidebar {
padding: 20px;
width: 33.333%;
background-color: #434A54;
color: #fff;
}
.sidebar h3 {
margin: 0;
}
.content,
.sidebar {
float: left;
}
/* The sticky */
/*.sidebar {
position: -webkit-sticky;
position: sticky;
top: 15px;
}
*/
header.sticky {
/* -webkit-transform: translateZ(0);*/
position: fixed;
top: 0;
width: 90vw;
}
.animated #outer-wrap {
-webkit-transition: height 500ms cubic-bezier(0.085, 0.745, 0.33, 0.935);
-moz-transition: height 500ms cubic-bezier(0.085, 0.745, 0.33, 0.935);
transition: height 500ms cubic-bezier(0.085, 0.745, 0.33, 0.935);
}
.animated #outer-wrap {
-webkit-animation: animLoadedOuterWrap 1s cubic-bezier(0.7, 0, 0.3, 1) both;
-moz-animation: animLoadedOuterWrap...
JavaScript
$(document).ready(function() {
var body = $('body');
body.addClass('animated');
});
$(".animated#outer-wrap").animate({
scrollTop: 500
}, "slow", function() {
// $window.disablescroll("undo");
// niceScroll_init();
// Enable user scrolling again when animated scrolling completes
});
//var sticky = new Waypoint.Sticky({
// element: $('header')[0]
//})
$('#outer-wrap').scroll(function() {
}, function() {
console.clear();
console.log("Inner ID: " + $('#bound')[0].scrollTop);
console.log("Window: " + $(window).scrollTop());
console.log("Inner ID Bound: " + $('#bound')[0].getBoundingClientRect().top);
})
$(window).scroll(function() {
}, function() {
console.clear();
console.log("React ID: " + $('#sidebar')[0].scrollTop);
console.log("Window: " + $(window).scrollTop());
console.log("React ID Bound: " + $('#sidebar')[0].getBoundingClientRect().top);
})
function ipContainerAnimationEvent() {
var t,
el = document.createElement("fakeelement");
var animations = {
"animation": "animationend",
"OAnimation": "oAnimationEnd",
"MozAnimation": "animationend",
"WebkitAnimation": "webkitAnimationEnd"
};
for (t in animations) {
if (el.style[t] !== undefined) {
return animations[t];
}
}
}
var body = $('body');
var header = $('.header');
var loadingContainer = $('#outer-wrap');
var ipContainerEvent = ipContainerAnimationEvent();
loadingContainer.one(ipContainerEvent,
function(event) {
// Do something when the animation ends
console.log('ipContainerEvent End');
body.removeClass('animated');
body.addClass('show');
header.addClass('sticky');
event.preventDefault();
return false;
});