|
| 1 | +/* |
| 2 | +********************************************************** |
| 3 | +* StickyStack.js |
| 4 | +* |
| 5 | +* Version: v1.0 |
| 6 | +* Author: Mike Zarandona |
| 7 | +* Release: March 27 2014 |
| 8 | +* Initial release. |
| 9 | +* |
| 10 | +* Reqs: jQuery |
| 11 | +* |
| 12 | +* Usage: $('.main-content-wrapper').stickyStack({ |
| 13 | +* containerElement: '.main-content-wrapper', |
| 14 | +* stackingElement: 'section', |
| 15 | +* boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)' |
| 16 | +* }); |
| 17 | +********************************************************** |
| 18 | +*/ |
| 19 | + |
| 20 | +(function ($, undefined) { |
| 21 | + $.fn.stickyStack = function (options) { |
| 22 | + |
| 23 | + /* Override defaults with specified options. */ |
| 24 | + options = $.extend({}, $.fn.stickyStack.options, options); |
| 25 | + |
| 26 | + // Variables setup |
| 27 | + var $sections = $(options.containerElement + ' > ' + options.stackingElement); |
| 28 | + var sectionsInfo = []; |
| 29 | + |
| 30 | + // Build the styles |
| 31 | + var styles = |
| 32 | + options.stackingElement + '{' + |
| 33 | + 'box-sizing: border-box;' + |
| 34 | + '-moz-box-sizing: border-box;' + |
| 35 | + 'position: relative;' + |
| 36 | + 'z-index: 100;' + |
| 37 | + '}' + |
| 38 | + |
| 39 | + options.stackingElement + '.stuck {' + |
| 40 | + 'position: fixed;' + |
| 41 | + 'top: 0;' + |
| 42 | + 'z-index: 0;' + |
| 43 | + '}' + |
| 44 | + |
| 45 | + options.stackingElement + '.stuck + ' + options.stackingElement + ':not(.stuck) {' + |
| 46 | + 'box-shadow: ' + options.boxShadow + ';' + |
| 47 | + '}'; |
| 48 | + |
| 49 | + // Append the styles to the <head> |
| 50 | + $('head').append('<style type="text/css">' + styles + '</style>'); |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + // Document ready() |
| 55 | + $(document).ready(function() { |
| 56 | + |
| 57 | + // Build an array of the sections |
| 58 | + // sectionsInfo[i][0] = Position from top of document |
| 59 | + // sectionsInfo[i][1] = Height of section |
| 60 | + for (var i = 0; i < $sections.length; i++) { |
| 61 | + sectionsInfo[i] = []; |
| 62 | + |
| 63 | + sectionsInfo[i][0] = $sections.eq(i).offset().top; |
| 64 | + sectionsInfo[i][1] = $sections.eq(i).outerHeight(true); |
| 65 | + } |
| 66 | + |
| 67 | + // Fix the section width |
| 68 | + var origWidth = $sections.eq(0).outerWidth(true); |
| 69 | + $sections.css('width', origWidth + 'px'); |
| 70 | + }); |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + // Window scroll() |
| 75 | + $(window).on('scroll', function() { |
| 76 | + |
| 77 | + // Current scroll position |
| 78 | + var windowScrollPos = $(window).scrollTop(); |
| 79 | + |
| 80 | + // Counter variable |
| 81 | + var counter = 0; |
| 82 | + |
| 83 | + // Count how many sections should be stuck |
| 84 | + for (var t = 0; t < $sections.length; t++) { |
| 85 | + if ( windowScrollPos >= sectionsInfo[t][0] ) { |
| 86 | + counter++; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + setStickies(counter); |
| 91 | + }); |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + // Resize event to keep the site width fluid |
| 96 | + $(window).on('resize', function() { |
| 97 | + $sections.css('width', $(options.containerElement).width() + 'px'); |
| 98 | + }); |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + function setStickies(howMany) { |
| 103 | + |
| 104 | + // Step 1: Calculate how much padding the parent container should get |
| 105 | + var paddingTop = 0; |
| 106 | + |
| 107 | + // Total the amount of padding of stuck sections |
| 108 | + for (var p = 0; p < howMany; p++) { |
| 109 | + paddingTop += $sections.eq(p).outerHeight(true); |
| 110 | + } |
| 111 | + |
| 112 | + // Append that height to the parent wrapper |
| 113 | + $sections.eq(0).parent().css('padding-top', paddingTop); |
| 114 | + |
| 115 | + |
| 116 | + // Step 2: Stick the sections to be stuck (heh) |
| 117 | + for (var s = 0; s < $sections.length; s++) { |
| 118 | + if (howMany > 0) { |
| 119 | + $sections.eq(s).addClass('stuck'); |
| 120 | + howMany--; |
| 121 | + } |
| 122 | + else { |
| 123 | + $sections.eq(s).removeClass('stuck'); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + }; |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + // Default the defaults |
| 133 | + $.fn.stickyStack.options = { |
| 134 | + containerElement: '.main-content-wrapper', |
| 135 | + stackingElement: 'section', |
| 136 | + boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)' |
| 137 | + }; |
| 138 | +})(jQuery); |
0 commit comments