|
2 | 2 | ********************************************************** |
3 | 3 | * StickyStack.js |
4 | 4 | * |
5 | | -* Version: v1.0 |
| 5 | +* Version: v1.1.1 |
6 | 6 | * Author: Mike Zarandona |
7 | | -* Release: March 27 2014 |
8 | | -* Initial release. |
| 7 | +* Release: June 03 2014 |
| 8 | +* Added data-scrollto for section scrolling |
9 | 9 | * |
10 | 10 | * Reqs: jQuery |
11 | 11 | * |
|
24 | 24 | options = $.extend({}, $.fn.stickyStack.options, options); |
25 | 25 |
|
26 | 26 | // Variables setup |
27 | | - var $sections = $(options.containerElement + ' > ' + options.stackingElement); |
28 | | - var sectionsInfo = []; |
| 27 | + var $sections = $(options.containerElement + ' > ' + options.stackingElement), |
| 28 | + sectionsInfo = [], |
29 | 29 |
|
30 | 30 | // Build the styles |
31 | | - var styles = |
| 31 | + styles = |
32 | 32 | options.stackingElement + '{' + |
33 | 33 | 'box-sizing: border-box;' + |
34 | 34 | '-moz-box-sizing: border-box;' + |
|
54 | 54 | // Document ready() |
55 | 55 | $(document).ready(function() { |
56 | 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 | | - } |
| 57 | + buildSectionsInfo(); |
66 | 58 |
|
67 | 59 | // Fix the section width |
68 | 60 | var origWidth = $sections.eq(0).outerWidth(true); |
|
75 | 67 | $(window).on('scroll', function() { |
76 | 68 |
|
77 | 69 | // Current scroll position |
78 | | - var windowScrollPos = $(window).scrollTop(); |
| 70 | + var windowScrollPos = $(window).scrollTop(), |
79 | 71 |
|
80 | 72 | // Counter variable |
81 | | - var counter = 0; |
| 73 | + counter = 0; |
82 | 74 |
|
83 | 75 | // Count how many sections should be stuck |
84 | 76 | for (var t = 0; t < $sections.length; t++) { |
|
95 | 87 | // Resize event to keep the site width fluid |
96 | 88 | $(window).on('resize', function() { |
97 | 89 | $sections.css('width', $(options.containerElement).width() + 'px'); |
| 90 | + |
| 91 | + buildSectionsInfo(); |
98 | 92 | }); |
99 | 93 |
|
100 | 94 |
|
|
125 | 119 | } |
126 | 120 | } |
127 | 121 |
|
| 122 | + |
| 123 | + |
| 124 | + // Helper function which builds the array sectionsInfo[] which keeps track of all the section elements |
| 125 | + function buildSectionsInfo() { |
| 126 | + // Build an array of the sections |
| 127 | + // sectionsInfo[i][0] = Position from top of document |
| 128 | + // sectionsInfo[i][1] = Height of section |
| 129 | + var runningHeightCounter = 0; |
| 130 | + |
| 131 | + for (var i = 0; i < $sections.length; i++) { |
| 132 | + sectionsInfo[i] = []; |
| 133 | + |
| 134 | + // record the height of the section |
| 135 | + sectionsInfo[i][1] = $sections.eq(i).outerHeight(true); |
| 136 | + |
| 137 | + // write the data-scrollto |
| 138 | + $sections.eq(i).attr('data-scrollto', $sections.eq(i).offset().top); |
| 139 | + |
| 140 | + // if the section is stuck, calculate its .offset() based on preceeding section heights |
| 141 | + if ( $sections.eq(i).hasClass('stuck') ) { |
| 142 | + sectionsInfo[i][0] = runningHeightCounter; |
| 143 | + runningHeightCounter += sectionsInfo[i][1]; |
| 144 | + } |
| 145 | + else { |
| 146 | + sectionsInfo[i][0] = $sections.eq(i).offset().top; |
| 147 | + } |
| 148 | + |
| 149 | + // Attach a data attribute to be used to scroll to sections |
| 150 | + $sections.eq(i).attr('data-scrollto', sectionsInfo[i][0]); |
| 151 | + $sections.eq(i).attr('data-height', sectionsInfo[i][1]); |
| 152 | + } |
| 153 | + console.log(runningHeightCounter); |
| 154 | + } |
| 155 | + |
128 | 156 | }; |
129 | 157 |
|
130 | 158 |
|
|
133 | 161 | $.fn.stickyStack.options = { |
134 | 162 | containerElement: '.main-content-wrapper', |
135 | 163 | stackingElement: 'section', |
136 | | - boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)' |
| 164 | + boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)' |
137 | 165 | }; |
138 | 166 | })(jQuery); |
0 commit comments