dat.gui/docs/main.js

136 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2014-08-27 00:01:15 +00:00
Gui.ready( init );
function init() {
var elements = document.querySelectorAll( '#readme h3' );
for ( var i = 0; i < elements.length; i++ ) {
var initExample = examples[ elements[i].id ] || examples['default'];
elements[i].example = initExample();
}
sticky( elements );
}
2014-09-09 20:45:58 +00:00
2014-08-27 00:01:15 +00:00
// Sticky headers
2014-09-09 20:45:58 +00:00
// -------------------------------
2014-08-27 00:01:15 +00:00
function sticky( elements ) {
for ( var el, i = 0, l = elements.length; i < l; i++ ) {
el = elements[ i ];
el.sticky = el.cloneNode( true );
el.sticky.removeAttribute( 'id' );
el.sticky.style.position = 'fixed';
el.sticky.style.visibility = 'hidden';
el.sticky.style.zIndex = i;
el.sticky.classList.add( 'sticky' );
el.parentElement.insertBefore( el.sticky, el );
measure( el );
if ( i > 0 ) {
elements[ i - 1 ].next = el;
}
}
function measure( el ) {
el.top = el.offsetTop;
el.sticky.height = el.sticky.offsetHeight;
}
function resize() {
for ( var i = 0, l = elements.length; i < l; i++ ) {
2014-09-09 20:45:58 +00:00
2014-08-27 00:01:15 +00:00
measure( elements[ i ] );
}
}
function onScroll() {
for ( var el, i = 0, l = elements.length; i < l; i++ ) {
2014-09-09 20:45:58 +00:00
2014-08-27 00:01:15 +00:00
el = elements[ i ];
var sticky = window.scrollY > el.top && window.scrollY <= el.next.top;
el.sticky.style.visibility = sticky ? 'visible' : 'hidden';
2014-09-09 20:45:58 +00:00
el.example.panel.classList.toggle( 'sticky', sticky || el.bumped );
2014-08-27 00:01:15 +00:00
if ( el.next ) el.next.bumped = false;
if ( el.next && sticky ) {
var bottom = window.scrollY + el.sticky.height;
var bumped = bottom > el.next.top;
el.sticky.style.marginTop = Math.round( Math.min( 0, el.next.top - bottom ) ) + 'px';
el.sticky.classList.toggle( 'sticky-prev', bumped );
el.next.classList.toggle( 'sticky-next', bumped );
if ( bumped ) {
2014-09-09 20:45:58 +00:00
el.example.panel.classList.remove( 'sticky' );
el.next.example.panel.classList.add( 'sticky' );
2014-08-27 00:01:15 +00:00
el.next.bumped = true;
}
}
}
}
document.addEventListener( 'scroll', onScroll );
// should debounce
window.addEventListener( 'resize', function() {
2014-09-09 20:45:58 +00:00
2014-08-27 00:01:15 +00:00
resize();
onScroll();
2014-09-08 01:31:51 +00:00
} );
2014-08-27 00:01:15 +00:00
onScroll();
}
// Smooth scroll
2014-09-09 20:45:58 +00:00
( function() {
2014-09-08 01:31:51 +00:00
2014-08-27 00:01:15 +00:00
var body = document.body, timer;
2014-09-09 20:45:58 +00:00
window.addEventListener( 'scroll', function() {
2014-09-08 01:31:51 +00:00
clearTimeout( timer );
2014-09-09 20:45:58 +00:00
if ( !body.classList.contains( 'disable-hover' ) ) {
body.classList.add( 'disable-hover' )
2014-09-08 01:31:51 +00:00
}
2014-09-09 20:45:58 +00:00
timer = setTimeout( function() {
body.classList.remove( 'disable-hover' )
}, 150 );
2014-09-08 01:31:51 +00:00
2014-09-09 20:45:58 +00:00
}, false );
2014-08-27 00:01:15 +00:00
2014-09-09 20:45:58 +00:00
} )();