Skip to content

Instantly share code, notes, and snippets.

@botmad
Created June 20, 2018 23:26
Show Gist options
  • Save botmad/e44018cc146cd90b814bf29981ae21d2 to your computer and use it in GitHub Desktop.
Save botmad/e44018cc146cd90b814bf29981ae21d2 to your computer and use it in GitHub Desktop.
Fold Scroll
<section id="info">
<header>
<hgroup>
<h1>FoldScroll</h1>
<h2>Experimental CSS 3D scroll behavior</h2>
</hgroup>
<a href="https://github.com/soulwire/FoldScroll/zipball/master" target="_blank">Download</a>
<a href="https://github.com/soulwire/FoldScroll" target="_blank">View on Github</a>
</header>
<aside>
<p>Quotes from <a href="http://http://butdoesitfloat.com/" target="_blank">http://butdoesitfloat.com/</a></p>
</aside>
</section>
<section class="quotes">
<article>
<p>Technique is just a means of arriving at a statement</p>
<em>Jackson Pollock</em>
</article>
<article>
<p>Perhaps a human language is possible in which the intent of meaning is actually beheld in three-dimensional space</p>
<em>Terence Mckenna</em>
</article>
<article>
<p>All the virgin eyes in the world are made of glass</p>
<em>Mina Loy</em>
</article>
<article>
<p>We are intensely aware of man as a machine and the body as a mechanism</p>
<em>Oskar Schlemmer</em>
</article>
<article>
<p>It is already too late, and it will go on being even later</p>
<em>Alison Lurie</em>
</article>
<article>
<p>Science cannot be stopped. Man will gather knowledge no matter what the consequences - and we cannot predict what they will be</p>
<em>Linus Pauling</em>
</article>
<article>
<p>No one can witness the fall of the ice palace. It takes place at night, after all the children are in bed.</p>
<em>Tarjei Vesaas</em>
</article>
<article>
<p>The biological object is made of time as much as it is made of space and matter</p>
<em>Terence McKenna</em>
</article>
<article>
<p>The union of the mathematician with the poet, fervor with measure, passion with correctness, this surely is the ideal</p>
<em>William James</em>
</article>
<article>
<p>Mind is a captive of the body</p>
<em>Camille Paglia</em>
</article>
<article>
<p>Your order is meaningless, my chaos is significant</p>
<em>Nathaniel West via Amos Vogel</em>
</article>
<article>
<p>Every thing existing on the physical plane is an exteriorization of thought, which must be balanced through the one who issued the thought, and in accordance with that one's responsibility, at the conjunction of time, condition, and place</p>
<em>Harold W</em>
</article>
<article>
<p>I wanted to be alone in quite an unusual, new way. The very opposite of what you are thinking: namely, without myself…</p>
<em>Pirandello</em>
</article>
<article>
<p>I love forms beyond my own, and regret the borders between us</p>
<em>Loren Eiseley</em>
</article>
<article>
<p>The bedrock basic stratum of reality is irreality; the universe is irrational because it is built not on mere shifting sand - but on that which is not</p>
<em>Philip K Dick</em>
</article>
</section>
(function($) {
$.fn.foldscroll = function( options ) {
// Constants
var PI = Math.PI;
var HALF_PI = PI / 2;
// Merge options & defaults
var opts = $.extend( {}, $.fn.foldscroll.defaults, options );
// Transformation template
var rot = 'perspective(' + opts.perspective + 'px) rotateX(θrad)';
// Main plugin loop
return this.each( function () {
var $this = $( this );
var $kids = $this.children();
var $item;
var $shading;
if ( opts.shading ) {
// Create an overlay for shading
$shading = $( '<span class="shading"/>' ).css({
background: opts.shading,
position: 'absolute',
opacity: 0.0,
height: '100%',
width: '100%',
left: 0,
top: 0
});
// Add shading to each child
$kids.each( function() {
$item = $(this);
$item.css( prefix({
'backface-visibility': 'hidden',
'transform-style': 'preserve-3d' // Fixes perspective in FF 10+
}));
// Make sure shading isn't already applied
if ( !$item.data( '_shading' ) ) {
$shading = $shading.clone();
// Prepare element
$item.css( 'position', 'relative' );
$item.data( '_shading', $shading );
$item.append( $shading );
}
});
}
// Prepare container
$this.css( prefix({ 'backface-visibility': 'hidden' }));
$this.css({ overflow: 'scroll' });
$this.on( 'scroll', function() {
// Store scroll amount
var st = $this.scrollTop();
// Store viewport properties
var vt = $this.offset().top - st;
var vh = $this.outerHeight();
var vb = vt + vh;
// Compute margin
var m = parseFloat( opts.margin );
m = m <= 1.0 ? Math.min( m, 0.5 ) : m / vh;
// Update children
$kids.each( function( index, el ) {
$item = $(this);
// Remove current transform
$item.css( prefix({ transform: 'none' }) );
// Cache shading element if it exists
$shading = $item.find( '.shading' ).hide();
// Store element properties
var et = $item.offset().top - st;
var eh = Math.max( m * vh, $item.outerHeight() );
var eb = et + eh;
// Highest start value
var a = Math.max( vt, et );
// Lowest end value
var b = Math.min( vb, eb );
// Do line segments overlap?
var show = a < b;
// If there's overlap
if ( show ) {
// compute overlap
var o = b - a;
var p = o / vh;
if ( p < m ) {
// normalise
p = p / m;
// direction
var d = et < vt ? 1 : -1;
// rotation
var t = ( 1 - p ) * HALF_PI * d;
// Contrain rotation
if ( Math.abs(t) <= HALF_PI ) {
// Apply rotation
$item.css( prefix({
'transform-origin': '50%' + ( et < vt ? '100%' : '0%' ),
'transform': rot.replace( 'θ', t )
}));
// Update shading overlay
if ( opts.shading )
$shading.css( 'opacity', 1.0 - p ).show();
} else {
show = false;
}
}
}
// Hide items outside of the viewport
$item.css( 'visibility', show ? 'visible' : 'hidden' );
});
});
// Set initial state
$this.trigger( 'scroll' );
});
};
// CSS3 vendor prefix helper
function prefix( obj ) {
var key, val;
for ( key in obj ) {
val = obj[ key ];
obj[ '-webkit-' + key ] = val;
obj[ '-moz-' + key ] = val;
obj[ '-ms-' + key ] = val;
obj[ '-o-' + key ] = val;
}
return obj;
}
// Default options
$.fn.foldscroll.defaults = {
// Perspective to apply to rotating elements
perspective: 600,
// Default shading to apply (null => no shading)
shading: 'rgba(0,0,0,0.2)',
// Area of rotation (fraction or pixel value)
margin: 0.2
};
})( jQuery );
$( '.quotes' ).foldscroll({
perspective: 900,
margin: '220px'
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html, body {
font-family: Consolas, monospace;
background: #f2f2f2;
padding: 0;
margin: 0;
}
/* Info */
#info {
font-size: 12px;
position: absolute;
z-index: 1;
color: #fff;
width: 260px;
left: 20px;
top: 20px;
}
#info a {
text-decoration: none;
border-bottom: 1px dotted rgba(255, 255, 255, 0.2);
color: rgba(255, 255, 255, 0.5);
}
#info a:hover {
color: #fff;
}
#info header, #info aside {
font-family: 'Play', sans-serif;
background: rgba(0, 0, 0, 0.8);
margin-bottom: 1px;
}
#info header {
padding: 12px 10px;
}
#info header hgroup h1 {
line-height: 22px;
font-weight: 300;
font-size: 18px;
margin: 0;
}
#info header hgroup h2 {
line-height: 14px;
font-weight: 300;
font-size: 12px;
color: rgba(255, 255, 255, 0.8);
margin: 0 0 6px 0;
}
#info header a {
text-transform: uppercase;
margin-right: 4px;
line-height: 20px;
font-size: 10px;
}
#info aside {
padding: 2px 10px;
}
/* Demo */
.quotes {
position: absolute;
bottom: 20px;
width: 100%;
left: 0;
top: 20px;
}
.quotes article {
border-bottom: 1px dashed #ddd;
text-align: justify;
line-height: 1.8;
background: #fff;
max-width: 620px;
font-size: 14px;
padding: 80px 40px;
margin: 0 auto;
width: 600px;
color: #333;
}
.quotes article em {
font-style: normal;
font-size: 12px;
color: #666;
}
.quotes article em:before {
content: '~';
margin: 0 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment