Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

add tolerance option to begin testing. a value shifts the effective t… #4

Merged
merged 1 commit into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,48 @@ <h1 class="docs">Porthole Demos</h1>
<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-deactivate></div>
</div>



<p class="docs">The following blocks have data-scroll-activetolerance attributes with a value of 300, causing them to activate when they are above 300px from the bottom of the viewport. This is good for animations. A negative value causes elements to activate before they reach the bottom of the viewport, which is good for lazy-loading images.</p>

<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>


<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>


<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>


<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>

<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>

<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>

<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>

<div data-xrayhtml data-title="Sample Scroll Activate">
<div data-scroll-activate data-scroll-activetolerance="300"></div>
</div>
</div>



</body>
</html>
4 changes: 2 additions & 2 deletions dist/porthole.init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! porthole - v1.0.4 - 2015-09-22
/*! porthole - v0.1.0 - 2016-05-20
* https://github.com/filamentgroup/porthole
* Copyright (c) 2015 Filament Group; Licensed MIT */
* Copyright (c) 2016 Filament Group; Licensed MIT */
;(function( $ ) {

var selector = "[data-scroll-activate]";
Expand Down
26 changes: 21 additions & 5 deletions dist/porthole.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/*! porthole - v1.0.4 - 2015-09-22
/*! porthole - v0.1.0 - 2016-05-20
* https://github.com/filamentgroup/porthole
* Copyright (c) 2015 Filament Group; Licensed MIT */
* Copyright (c) 2016 Filament Group; Licensed MIT */
(function($){

var queue = [];
var listening;
var deActivateSelector = "[data-scroll-deactivate]";
var toleranceSelector = "data-scroll-activetolerance";
var activeClass = "inviewport";
var viewportSize;
var options = {
removeClass: false //remove class when out of viewport? default false
removeClass: false, //remove class when out of viewport? default false
activeTolerance: 0 // adjust the active window that is considered to be "in the viewport". Positive values make the window larger (good for prefetching images). Negative Values make it smaller (good for cueing animations)
};

// polyfill raf if needed
Expand Down Expand Up @@ -43,9 +45,23 @@
var isActive = $( this ).is( "." + activeClass );
var thisTop = this.getBoundingClientRect().top;
var thisBottom = thisTop + this.offsetHeight;
var tolerance = 0;
var toleranceAttr = $( this ).attr( toleranceSelector );
if( toleranceAttr ){
var tolVal = parseFloat( toleranceAttr );
if( !isNaN( tolVal ) ){
tolerance = tolVal;
}
}
var thisOptions = $.extend( options, {
removeClass: $( this ).is( deActivateSelector ) || false
removeClass: $( this ).is( deActivateSelector ) || false,
activeTolerance: tolerance
});

if( thisOptions.activeTolerance !== 0 ){
thisTop += thisOptions.activeTolerance;
}

if( thisTop >= 0 && thisTop <= viewportHeight ||
thisTop <= 0 && thisBottom >= viewportHeight ||
thisBottom >= 0 && thisBottom <= viewportHeight ){
Expand Down Expand Up @@ -81,4 +97,4 @@
};


}(jQuery));
}(jQuery));
22 changes: 19 additions & 3 deletions src/porthole.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
var queue = [];
var listening;
var deActivateSelector = "[data-scroll-deactivate]";
var toleranceSelector = "data-scroll-activetolerance";
var activeClass = "inviewport";
var viewportSize;
var options = {
removeClass: false //remove class when out of viewport? default false
removeClass: false, //remove class when out of viewport? default false
activeTolerance: 0 // adjust the active window that is considered to be "in the viewport". Positive values make the window larger (good for prefetching images). Negative Values make it smaller (good for cueing animations)
};

// polyfill raf if needed
Expand Down Expand Up @@ -45,9 +47,23 @@
var isActive = $( this ).is( "." + activeClass );
var thisTop = this.getBoundingClientRect().top;
var thisBottom = thisTop + this.offsetHeight;
var tolerance = 0;
var toleranceAttr = $( this ).attr( toleranceSelector );
if( toleranceAttr ){
var tolVal = parseFloat( toleranceAttr );
if( !isNaN( tolVal ) ){
tolerance = tolVal;
}
}
var thisOptions = $.extend( options, {
removeClass: $( this ).is( deActivateSelector ) || false
removeClass: $( this ).is( deActivateSelector ) || false,
activeTolerance: tolerance
});

if( thisOptions.activeTolerance !== 0 ){
thisTop += thisOptions.activeTolerance;
}

if( thisTop >= 0 && thisTop <= viewportHeight ||
thisTop <= 0 && thisBottom >= viewportHeight ||
thisBottom >= 0 && thisBottom <= viewportHeight ){
Expand Down Expand Up @@ -83,4 +99,4 @@
};


}(jQuery));
}(jQuery));