Skip to content

Commit b015f3c

Browse files
committed
Build scripts and update readme.
1 parent 39fd791 commit b015f3c

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ This will install all the necessary tools for compiling minified files.
102102

103103
__1.1.2__
104104

105+
- Improvement: Extend public method `getScrollPercent` to return scroll percentage for elements.
105106
- Fix position calculation to 2 decimals precision.
106107
- Fix scroll percent calculation based on containerSize.
107108

dist/smooth-parallax.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
return extended;
9999
};
100100

101+
102+
101103
/**
102104
* Get movable element container
103105
* @private
@@ -113,6 +115,8 @@
113115
return _container;
114116
};
115117

118+
119+
116120
/**
117121
* Calculate page percent scrolled.
118122
* @private
@@ -124,6 +128,8 @@
124128
return _scrollOffset / ( _height - documentElement.clientHeight );
125129
};
126130

131+
132+
127133
/**
128134
* Calculate variables used to determine elements position
129135
* @private
@@ -152,6 +158,25 @@
152158
}
153159
};
154160

161+
162+
163+
/**
164+
* Get position data object for the element.
165+
* @returns {Object} Position data object for element or false if not found.
166+
*/
167+
var getPositionDataByElement = function ( el ) {
168+
for (var i = 0; i < _positions.length; i++) {
169+
if ( _positions[i].element == el ) {
170+
return _positions[i];
171+
}
172+
}
173+
174+
// Return false if not found
175+
return false;
176+
}
177+
178+
179+
155180
/**
156181
* Initializes positions for each moving element.
157182
* @private
@@ -182,6 +207,7 @@
182207
}
183208

184209
var elementPosition = {
210+
element: _movingElements[i],
185211
container: getElementContainer( _movingElements[i] ),
186212
baseSizeOn: baseSizeOn,
187213
start: {
@@ -309,10 +335,30 @@
309335
};
310336

311337
/**
312-
* Exposes scroll percentage
338+
* Get scroll percentage for the element or page.
339+
* @param {string} el Target element css selector.
340+
* @return {float} Scroll percentage for the element or the page.
313341
*/
314-
publicMethods.getScrollPercent = function () {
315-
return calculatePageScrollPercent();
342+
publicMethods.getScrollPercent = function ( selector ) {
343+
// Calculate page scroll if no selector was passed
344+
if ( selector == undefined ) {
345+
return calculatePageScrollPercent();
346+
}
347+
348+
// Find element
349+
// Return false if not found
350+
var el = document.querySelector( selector );
351+
if ( el == null ) return false;
352+
353+
// Calculate element scroll percent
354+
var positionData = getPositionDataByElement( el );
355+
if ( positionData ) {
356+
calculatePercent( positionData );
357+
return _scrollPercent;
358+
}
359+
360+
// Return false otherwise
361+
return false;
316362
};
317363

318364

dist/smooth-parallax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)