Skip to content

Commit 6b9f743

Browse files
Added a fix for sections that are taller than the viewport
1 parent 759600f commit 6b9f743

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

jquery.stickystack.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
**********************************************************
33
* StickyStack.js
44
*
5-
* Version: v1.1.1
5+
* Version: v1.1.2
66
* Author: Mike Zarandona
7-
* Release: June 03 2014
8-
* Added data-scrollto for section scrolling
7+
* Release: June 24 2014
8+
* Added a fix for sections that are taller than the screen
99
*
1010
* Reqs: jQuery
1111
*
@@ -44,10 +44,15 @@
4444

4545
options.stackingElement + '.stuck + ' + options.stackingElement + ':not(.stuck) {' +
4646
'box-shadow: ' + options.boxShadow + ';' +
47+
'}' +
48+
49+
options.stackingElement + '.stuck.align-bottom {' +
50+
'top: auto !important;' +
51+
'bottom: 0 !important;' +
4752
'}';
4853

4954
// Append the styles to the <head>
50-
$('head').append('<style type="text/css">' + styles + '</style>');
55+
$('head').append('<style id="sticky-stack-styles" type="text/css">' + styles + '</style>');
5156

5257

5358

@@ -74,8 +79,16 @@
7479

7580
// Count how many sections should be stuck
7681
for (var t = 0; t < $sections.length; t++) {
77-
if ( windowScrollPos >= sectionsInfo[t][0] ) {
78-
counter++;
82+
// if this section has an offset, use that instead
83+
if ( $sections.eq(t).attr('data-offset') != 0 ) {
84+
if ( windowScrollPos >= sectionsInfo[t][2] + sectionsInfo[t][0] ) {
85+
counter++;
86+
}
87+
}
88+
else {
89+
if ( windowScrollPos >= sectionsInfo[t][0] ) {
90+
counter++;
91+
}
7992
}
8093
}
8194

@@ -126,14 +139,26 @@
126139
// Build an array of the sections
127140
// sectionsInfo[i][0] = Position from top of document
128141
// sectionsInfo[i][1] = Height of section
129-
var runningHeightCounter = 0;
142+
// sectionsInfo[i][2] = Scroll offset (if taller than viewport)
143+
var runningHeightCounter = 0,
144+
viewportHeight = $(window).outerHeight(true);
130145

131146
for (var i = 0; i < $sections.length; i++) {
132147
sectionsInfo[i] = [];
133148

134149
// record the height of the section
135150
sectionsInfo[i][1] = $sections.eq(i).outerHeight(true);
136151

152+
// test this section height against viewport
153+
if ( sectionsInfo[i][1] > viewportHeight ) {
154+
sectionsInfo[i][2] = sectionsInfo[i][1] - viewportHeight;
155+
$sections.eq(i).addClass('align-bottom');
156+
}
157+
else {
158+
sectionsInfo[i][2] = 0;
159+
$sections.eq(i).removeClass('align-bottom');
160+
}
161+
137162
// write the data-scrollto
138163
$sections.eq(i).attr('data-scrollto', $sections.eq(i).offset().top);
139164

@@ -146,11 +171,13 @@
146171
sectionsInfo[i][0] = $sections.eq(i).offset().top;
147172
}
148173

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]);
174+
// Attach data attributes
175+
$sections.eq(i)
176+
.attr('data-scrollto', sectionsInfo[i][0])
177+
.attr('data-height', sectionsInfo[i][1])
178+
.attr('data-offset', sectionsInfo[i][2]);
179+
152180
}
153-
console.log(runningHeightCounter);
154181
}
155182

156183
};
@@ -161,6 +188,6 @@
161188
$.fn.stickyStack.options = {
162189
containerElement: '.main-content-wrapper',
163190
stackingElement: 'section',
164-
boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)'
191+
boxShadow: '0 -3px 20px rgba(0, 0, 0, 0.25)'
165192
};
166193
})(jQuery);

0 commit comments

Comments
 (0)