1/* 2* HeadsUp 1.5.6 3* @author Kyle Foster (@hkfoster) 4* @license MIT 5*/ 6;(function( window, document, undefined ) { 7 8 'use strict'; 9 10 // Extend function 11 function extend( a, b ) { 12 for( var key in b ) { 13 if( b.hasOwnProperty( key ) ) { 14 a[ key ] = b[ key ]; 15 } 16 } 17 return a; 18 } 19 20 // Throttle function (https://bit.ly/1eJxOqL) 21 function throttle( fn, threshhold, scope ) { 22 threshhold || ( threshhold = 250 ); 23 var previous, deferTimer; 24 return function () { 25 var context = scope || this, 26 current = Date.now(), 27 args = arguments; 28 if ( previous && current < previous + threshhold ) { 29 clearTimeout( deferTimer ); 30 deferTimer = setTimeout( function () { 31 previous = current; 32 fn.apply( context, args ); 33 }, threshhold ); 34 } else { 35 previous = current; 36 fn.apply( context, args ); 37 } 38 }; 39 } 40 41 // Class management functions 42 function classReg( className ) { 43 return new RegExp( '(^|\\s+)' + className + '(\\s+|$)' ); 44 } 45 46 function hasClass( el, cl ) { 47 return classReg( cl ).test( el.className ); 48 } 49 50 function addClass( el, cl ) { 51 if ( !hasClass( el, cl ) ) { 52 el.className = el.className + ' ' + cl; 53 } 54 } 55 56 function removeClass( el, cl ) { 57 el.className = el.className.replace( classReg( cl ), ' ' ); 58 } 59 60 // Main function definition 61 function headsUp( selector, options ) { 62 this.selector = document.querySelector( selector ); 63 this.options = extend( this.defaults, options ); 64 this.init(); 65 } 66 67 // Overridable defaults 68 headsUp.prototype = { 69 defaults : { 70 delay : 0, 71 sensitivity : 20 72 }, 73 74 // Init function 75 init : function( selector ) { 76 77 var self = this, 78 options = self.options, 79 selector = self.selector, 80 oldScrollY = 0, 81 winHeight; 82 83 // Resize handler function 84 function resizeHandler() { 85 winHeight = window.innerHeight; 86 return winHeight; 87 } 88 89 // Scroll handler function 90 function scrollHandler() { 91 92 // Scoped variables 93 var newScrollY = window.pageYOffset, 94 docHeight = document.body.scrollHeight, 95 pastDelay = newScrollY > options.delay, 96 goingDown = newScrollY > oldScrollY, 97 fastEnough = newScrollY < oldScrollY - options.sensitivity, 98 rockBottom = newScrollY < 0 || newScrollY + winHeight >= docHeight; 99 100 // Where the magic happens 101 if ( pastDelay && goingDown ) { 102 addClass( selector, 'heads-up' ); 103 } else if ( !goingDown && fastEnough && !rockBottom || !pastDelay ) { 104 removeClass( selector, 'heads-up' ); 105 } 106 107 // Keep on keeping on 108 oldScrollY = newScrollY; 109 } 110 111 // Attach listeners 112 if ( selector ) { 113 114 // Trigger initial resize 115 resizeHandler(); 116 117 // Resize function listener 118 window.addEventListener( 'resize', throttle( resizeHandler ), false ); 119 120 // Scroll function listener 121 window.addEventListener( 'scroll', throttle( scrollHandler, 100 ), false ); 122 } 123 } 124 }; 125 126 window.headsUp = headsUp; 127 128})( window, document ); 129 130// Instantiate HeadsUp 131new headsUp( '.smartenble' );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.