Skip to content

Instantly share code, notes, and snippets.

@jackiewu
Created August 1, 2015 04:07
Show Gist options
  • Save jackiewu/3be663486ac97d120e26 to your computer and use it in GitHub Desktop.
Save jackiewu/3be663486ac97d120e26 to your computer and use it in GitHub Desktop.
StickyStack.js
<div class="main-content-wrapper">
<!-- section 1 -->
<section id="one">
<h1>StickyStack.js</h1>
<p class="helper">A jQuery plugin that creates a <em>stacking</em> effect by <em>sticking</em> panels as they reach the top of the viewport.</p>
<br />
<div class="button-wrapper">
<a href="https://github.com/mike-zarandona/StickyStack.js/archive/master.zip" class="btn">Download</a>
<a href="https://github.com/mike-zarandona/StickyStack.js" class="btn" target="_blank">GitHub</a>
</div><!--/.button-wrapper-->
</section>
<!-- section 2-->
<section id="two">
<div class="shadow">
<h1>Long pages feel like a stack of cards</h1>
</div><!--/.shadow-->
</section>
<!-- section 3 -->
<section>
<div class="shadow">
<h1>Usage</h1>
<p><code><pre>$('.main-content-wrapper').stickyStack();
</pre></code></p>
</div>
</section>
<!-- section 4 -->
<section>
<div class="shadow">
<h1>Options</h1>
<p><code><pre>$('.main-content-wrapper').stickyStack({
containerElement: '.main-content-wrapper',
stackingElement: 'section',
boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)'
});
</pre></code></p>
</div><!--/.shadow-->
</section>
<section>
<p>Enjoy.</p>
<div class="button-wrapper">
<a href="https://github.com/mike-zarandona/StickyStack.js/archive/master.zip" class="btn">Download</a>
<a href="https://github.com/mike-zarandona/StickyStack.js" class="btn" target="_blank">GitHub</a>
</div><!--/.button-wrapper-->
<small>by <a href="http://twitter.com/mikezarandona" target="_blank">mike zarandona</a></small>
</section>
</div><!--/.main-content-wrapper-->
/*
**********************************************************
* StickyStack.js
*
* Version: v1.1.1
* Author: Mike Zarandona
* Release: June 03 2014
* Added data-scrollto for section scrolling
*
* Reqs: jQuery
*
* Usage: $('.main-content-wrapper').stickyStack({
* containerElement: '.main-content-wrapper',
* stackingElement: 'section',
* boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)'
* });
**********************************************************
*/
!function(t){t.fn.stickyStack=function(e){function n(t){for(var e=0,n=0;t>n;n++)e+=s.eq(n).outerHeight(!0);s.eq(0).parent().css("padding-top",e);for(var o=0;o<s.length;o++)t>0?(s.eq(o).addClass("stuck"),t--):s.eq(o).removeClass("stuck")}function o(){for(var t=0,e=0;e<s.length;e++)i[e]=[],i[e][1]=s.eq(e).outerHeight(!0),s.eq(e).attr("data-scrollto",s.eq(e).offset().top),s.eq(e).hasClass("stuck")?(i[e][0]=t,t+=i[e][1]):i[e][0]=s.eq(e).offset().top,s.eq(e).attr("data-scrollto",i[e][0]),s.eq(e).attr("data-height",i[e][1]);console.log(t)}e=t.extend({},t.fn.stickyStack.options,e);var s=t(e.containerElement+" > "+e.stackingElement),i=[],a=e.stackingElement+"{box-sizing: border-box;-moz-box-sizing: border-box;position: relative;z-index: 100;}"+e.stackingElement+".stuck {position: fixed;top: 0;z-index: 0;}"+e.stackingElement+".stuck + "+e.stackingElement+":not(.stuck) {box-shadow: "+e.boxShadow+";}";t("head").append('<style type="text/css">'+a+"</style>"),t(document).ready(function(){o();var t=s.eq(0).outerWidth(!0);s.css("width",t+"px")}),t(window).on("scroll",function(){for(var e=t(window).scrollTop(),o=0,a=0;a<s.length;a++)e>=i[a][0]&&o++;n(o)}),t(window).on("resize",function(){s.css("width",t(e.containerElement).width()+"px"),o()})},t.fn.stickyStack.options={containerElement:".main-content-wrapper",stackingElement:"section",boxShadow:"0 -3px 20px rgba(0, 0, 0, 0.25)"}}(jQuery);
$(window).load(function() {
$('section').css('min-height', $(window).height());
$('.main-content-wrapper').stickyStack();
});
// analytics
(function(d,e,j,h,f,c,b){d.GoogleAnalyticsObject=f;d[f]=d[f]||function(){(d[f].q=d[f].q||[]).push(arguments)},d[f].l=1*new Date();c=e.createElement(j),b=e.getElementsByTagName(j)[0];c.async=1;c.src=h;b.parentNode.insertBefore(c,b)})(window,document,"script","//www.google-analytics.com/analytics.js","ga");ga("create","UA-53573814-1","auto");ga("send","pageview");
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//raw.githubusercontent.com/mike-zarandona/StickyStack.js/master/jquery.stickystack.min.js"></script>
@import "lesshat";
@open-sans: 'Open Sans', sans-serif;
body { font: 16px/1.5em 'Open Sans', @open-sans; }
a {
color: #fff;
.transition(all 300ms);
&:hover {
background-color: #fff;
color: #111;
}
}
small {
font-size: 10px;
clear: both;
display: block;
}
section {
background-color: #303030;
color: #fff;
padding: 1em 2em;
.box-sizing(border-box);
background-size: cover;
background-position: 50% 50%;
display: block;
&.stuck + section:not(.stuck) {
box-shadow: 0 -3px 20px rgba(0, 0, 0, 0.5);
}
&:before {
content: '';
display: block;
position: absolute;
background-color: rgba(0, 0, 0, 0.65);
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
&:nth-child(1) {
pading-top: 200px;
text-align: center;
background-image: url('http://i.imgur.com/CK4n7dV.jpg');
background-size: cover;
background-position: 50% 50%;
position: relative;
h1 {
margin-top: 160px;
margin-bottom: 0;
line-height: 1em;
font: 300 48px/1.2em @open-sans;
}
.helper {
font-size: 14px;
width: 50%;
margin: 1em auto;
line-height: 1.5em;
}
}
&:nth-child(2) {
background-image: url('http://i.imgur.com/l0kNMoI.jpg');
position: relative;
&:before { display: none; }
h1 {
font: 300 42px/1.3em @open-sans;
}
}
&:nth-child(3) {
background-image: url('http://i.imgur.com/T1zKXoY.jpg');
h1 { font-weight: 300; margin-bottom: 1em; line-height:1.2em; }
&:before { display: none; }
}
&:nth-child(4) {
background-image: url('http://i.imgur.com/a2iscIQ.jpg');
&:before { display: none; }
.shadow {
position: absolute;
bottom: 40px;
left: 20px;
}
h1 {
font-weight: 300;
line-height: 1.2em;
}
}
&:nth-child(5) {
background-image: url('http://i.imgur.com/Ajiqru4.jpg');
background-position: 50% 100%;
padding-top: 30%;
text-align: center;
}
}
/* Modular Styles */
h1 {
font-size: 3em;
}
.button-wrapper {
margin: 0 auto;
padding: 0px;
width: 320px;
position: relative;
clear: both;
.btn + .btn { margin-left: -4px; }
}
.btn {
padding: 10px;
font: bold 16px/1.2em sans-serif;
border: 2px solid #fff;
.border-radius(3px);
background-color: transparent;
text-decoration: none;
margin: 0.5em auto;
color: #fff;
.transition(300ms all);
display: inline-block;
box-sizing: border-box;
width: 49%;
&:hover {
background-color: fade(#fff, 33%);
color: #fff;
text-shadow: 0 1px 0 #111;
}
&:first-of-type {
margin-right: 2%;
}
&:last-of-type {
}
}
.shadow {
background-color: rgba(0, 0, 0, 0.8);
padding: 1em;
border-radius: 5px;
&:after { display: none; }
h1, h2, h3 {
&:first-of-type { margin-top: 0; margin-bottom: 10px; }
}
}
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,700" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment