Skip to content

Commit 7719d37

Browse files
committed
ScrollReveal ✨ 3.0.1
1 parent 288728b commit 7719d37

File tree

3 files changed

+83
-34
lines changed

3 files changed

+83
-34
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
***
66

7-
[![ScrollReveal version](http://img.shields.io/badge/scrollreveal.js-v3.0.0-1a2434.svg)](https://scrollrevealjs.org) [![License](http://img.shields.io/badge/License-MIT-1a2434.svg)](http://opensource.org/licenses/MIT)
7+
[![ScrollReveal version](http://img.shields.io/badge/scrollreveal.js-v3.0.1-1a2434.svg)](https://scrollrevealjs.org) [![License](http://img.shields.io/badge/License-MIT-1a2434.svg)](http://opensource.org/licenses/MIT)
88

9-
- 2.7KB minified and Gzipped
9+
- 2.8KB minified and Gzipped
1010
- No dependencies
1111
- From the ![heart](http://i.imgur.com/oXJmdtz.gif) of [@jlmakes](https://jlmak.es)
1212

@@ -19,7 +19,7 @@
1919
The simplest method is to copy paste this snippet just before your closing `</body>` tag (thanks to jsDelivr)
2020

2121
```html
22-
<script src="https://cdn.jsdelivr.net/scrollreveal.js/3.0.0/scrollreveal.min.js"></script>
22+
<script src="https://cdn.jsdelivr.net/scrollreveal.js/3.0.1/scrollreveal.min.js"></script>
2323
```
2424

2525
But you can also:

dist/scrollreveal.js

+79-30
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ ______________________________________________________________________________*/
232232

233233
sr.tools.forOwn( sr.store.elements, function( elemId ) {
234234
elem = sr.store.elements[ elemId ];
235-
visible = sr.isVisible( elem );
235+
visible = sr.isElemVisible( elem );
236236
if ( visible && !elem.revealed ) {
237237

238238
if ( elem.config.useDelay === 'always'
@@ -296,36 +296,89 @@ ______________________________________________________________________________*/
296296
}
297297
};
298298

299-
ScrollReveal.prototype.isVisible = function( elem ) {
300-
var config, rect, viewable;
301-
var viewport = {
302-
width : window.document.documentElement.clientWidth,
303-
height : window.document.documentElement.clientHeight
299+
ScrollReveal.prototype.getContainerSize = function( container ){
300+
if ( !container ) {
301+
container = window.document.documentElement;
302+
}
303+
var w = container['clientWidth'] || 0;
304+
var h = container['clientHeight'] || 0;
305+
return {
306+
width: w,
307+
height: h
304308
};
305-
if ( elem.config.container ) {
306-
var container = elem.config.container.getBoundingClientRect();
307-
viewable = {
308-
top : sr.tools.clamp( 0, container.top, viewport.height ),
309-
right : sr.tools.clamp( 0, container.right, viewport.width ),
310-
bottom : sr.tools.clamp( 0, container.bottom, viewport.height ),
311-
left : sr.tools.clamp( 0, container.left, viewport.width )
309+
};
310+
311+
ScrollReveal.prototype.getScrolled = function( container ){
312+
if ( !container ) {
313+
return {
314+
x: window.pageXOffset,
315+
y: window.pageYOffset
312316
};
313317
} else {
314-
viewable = {
315-
top : 0,
316-
right : viewport.width,
317-
bottom : viewport.height,
318-
left : 0
318+
var offset = sr.getOffset( container );
319+
return {
320+
x: container.scrollLeft + offset.left,
321+
y: container.scrollTop + offset.top
322+
};
323+
}
324+
};
325+
326+
ScrollReveal.prototype.getOffset = function( domEl ) {
327+
var offsetTop = 0;
328+
var offsetLeft = 0;
329+
330+
do {
331+
if ( !isNaN( domEl.offsetTop ) ) {
332+
offsetTop += domEl.offsetTop;
333+
}
334+
if ( !isNaN( domEl.offsetLeft ) ) {
335+
offsetLeft += domEl.offsetLeft;
319336
}
337+
} while ( domEl = domEl.offsetParent );
338+
339+
return {
340+
top: offsetTop,
341+
left: offsetLeft
342+
};
343+
};
344+
345+
ScrollReveal.prototype.isElemVisible = function( elem ){
346+
347+
var offset = sr.getOffset( elem.domEl );
348+
var container = sr.getContainerSize( elem.config.container );
349+
var scrolled = sr.getScrolled( elem.config.container );
350+
var vF = elem.config.viewFactor;
351+
352+
var elemHeight = elem.domEl.offsetHeight;
353+
var elemWidth = elem.domEl.offsetWidth;
354+
var elemTop = offset.top;
355+
var elemBottom = elemTop + elemHeight;
356+
var elemLeft = offset.left;
357+
var elemRight = elemLeft + elemWidth;
358+
359+
return ( confirmBounds() || isPositionFixed() );
360+
361+
function confirmBounds(){
362+
363+
var top = elemTop + elemHeight * vF;
364+
var bottom = elemBottom - elemHeight * vF;
365+
var left = elemLeft + elemWidth * vF;
366+
var right = elemRight - elemWidth * vF;
367+
368+
var viewTop = scrolled.y + elem.config.viewOffset.top;
369+
var viewBottom = scrolled.y - elem.config.viewOffset.bottom + container.height;
370+
var viewLeft = scrolled.x + elem.config.viewOffset.left;
371+
var viewRight = scrolled.x - elem.config.viewOffset.right + container.width;
372+
373+
return ( top < viewBottom )
374+
&& ( bottom > viewTop )
375+
&& ( left > viewLeft )
376+
&& ( right < viewRight );
377+
}
378+
379+
function isPositionFixed(){
380+
return ( window.getComputedStyle( elem.domEl ).position === 'fixed' );
320381
}
321-
rect = elem.domEl.getBoundingClientRect();
322-
config = elem.config;
323-
return (
324-
rect.top + ( rect.height * config.viewFactor ) < viewable.bottom - config.viewOffset.bottom &&
325-
rect.right - ( rect.width * config.viewFactor ) > viewable.left + config.viewOffset.left &&
326-
rect.bottom - ( rect.height * config.viewFactor ) > viewable.top + config.viewOffset.top &&
327-
rect.left + ( rect.width * config.viewFactor ) < viewable.right - config.viewOffset.right
328-
);
329382
};
330383

331384
ScrollReveal.prototype.sync = function() {
@@ -342,10 +395,6 @@ ______________________________________________________________________________*/
342395

343396
var Tools = (function() {
344397

345-
Tools.prototype.clamp = function( min, num, max ) {
346-
return Math.min( Math.max( min, num ), max );
347-
};
348-
349398
Tools.prototype.isObject = function( object ) {
350399
return object !== null && typeof object === 'object' && object.constructor == Object;
351400
};

dist/scrollreveal.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)