intersection-events
Advanced tools
Comparing version 0.2.6 to 0.2.7
/*! | ||
* intersection-events v0.2.6 | ||
* intersection-events v0.2.7 | ||
* https://github.com/ko-yelie/intersection-events#readme | ||
@@ -72,68 +72,62 @@ * | ||
var leaveThreshold = options.leaveThreshold; if ( leaveThreshold === void 0 ) leaveThreshold = 0; | ||
getElements(target).forEach(function (el) { | ||
var selfEnterThreshold = parseFloat(el.dataset.enterThreshold) || enterThreshold; | ||
var selfLeaveThreshold = parseFloat(el.dataset.leaveThreshold) || leaveThreshold; | ||
if (enterThreshold === 1) { | ||
enterThreshold = MAX_THRESHOLD; | ||
} | ||
if (selfEnterThreshold === 1) { | ||
selfEnterThreshold = MAX_THRESHOLD; | ||
} | ||
if (leaveThreshold === 1) { | ||
leaveThreshold = MAX_THRESHOLD; | ||
} | ||
if (selfLeaveThreshold === 1) { | ||
selfLeaveThreshold = MAX_THRESHOLD; | ||
} | ||
var isEnter; | ||
var isLeave; | ||
var checkEnter; | ||
var checkLeave; | ||
if (enterThreshold === leaveThreshold) { | ||
isEnter = function (entry) { return entry.isIntersecting; }; | ||
if (selfEnterThreshold === selfLeaveThreshold) { | ||
checkEnter = function (entry) { return entry.isIntersecting; }; | ||
isLeave = function (entry) { return leaveThreshold === 0 ? !entry.isIntersecting : entry.isIntersecting; }; | ||
} else { | ||
isEnter = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - enterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - leaveThreshold); | ||
return enterRatio <= leaveRatio; | ||
}; | ||
checkLeave = function (entry) { return selfLeaveThreshold === 0 ? !entry.isIntersecting : entry.isIntersecting; }; | ||
} else { | ||
checkEnter = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - selfEnterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - selfLeaveThreshold); | ||
return enterRatio <= leaveRatio; | ||
}; | ||
isLeave = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - enterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - leaveThreshold); | ||
return enterRatio >= leaveRatio; | ||
}; | ||
} | ||
checkLeave = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - selfEnterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - selfLeaveThreshold); | ||
return enterRatio >= leaveRatio; | ||
}; | ||
} | ||
var callback = function (entries) { | ||
entries.forEach(function (entry) { | ||
var el = entry.target; | ||
var isEnter = false; | ||
if (!el.intersectionEvents.isEnter && isEnter(entry)) { | ||
// enter | ||
el.intersectionEvents.isEnter = true; | ||
onEnter(el); | ||
isOnce && el.intersectionEvents.observer.unobserve(el); | ||
} else if (el.intersectionEvents.isEnter && isLeave(entry)) { | ||
// leave | ||
el.intersectionEvents.isEnter = false; | ||
onLeave(el); | ||
} | ||
}); | ||
}; | ||
var callback = function (entries) { | ||
entries.forEach(function (entry) { | ||
var el = entry.target; | ||
var observer = new IntersectionObserver(callback, { | ||
threshold: [leaveThreshold, enterThreshold] | ||
}); | ||
getElements(target).forEach(function (el) { | ||
el.intersectionEvents = {}; | ||
el.intersectionEvents.isEnter = false; | ||
var rate = window.innerHeight / el.offsetHeight; | ||
var isOverEnter = enterThreshold > rate; | ||
var isOverLeave = leaveThreshold > rate; | ||
el.intersectionEvents.observer = observer; | ||
if (isOverEnter || isOverLeave) { | ||
// When the height of the element is larger than the window, change threshold so that it fits within the window. | ||
el.intersectionEvents.observer = new IntersectionObserver(callback, { | ||
threshold: [isOverLeave ? leaveThreshold * rate : leaveThreshold, isOverEnter ? enterThreshold * rate : enterThreshold] | ||
if (!isEnter && checkEnter(entry)) { | ||
// enter | ||
isEnter = true; | ||
onEnter(el); | ||
isOnce && observer.unobserve(el); | ||
} else if (isEnter && checkLeave(entry)) { | ||
// leave | ||
isEnter = false; | ||
onLeave(el); | ||
} | ||
}); | ||
} | ||
}; // When the height of the element is larger than the window, change threshold so that it fits within the window. | ||
el.intersectionEvents.observer.observe(el); | ||
var rate = window.innerHeight / el.offsetHeight; | ||
var isOverEnter = selfEnterThreshold > rate; | ||
var isOverLeave = selfLeaveThreshold > rate; | ||
var observer = new IntersectionObserver(callback, { | ||
threshold: [isOverLeave ? selfLeaveThreshold * rate : selfLeaveThreshold, isOverEnter ? selfEnterThreshold * rate : selfEnterThreshold] | ||
}); | ||
observer.observe(el); | ||
}); | ||
@@ -140,0 +134,0 @@ }; |
/*! | ||
* intersection-events v0.2.6 | ||
* intersection-events v0.2.7 | ||
* https://github.com/ko-yelie/intersection-events#readme | ||
@@ -75,68 +75,62 @@ * | ||
var leaveThreshold = options.leaveThreshold; if ( leaveThreshold === void 0 ) leaveThreshold = 0; | ||
getElements(target).forEach(function (el) { | ||
var selfEnterThreshold = parseFloat(el.dataset.enterThreshold) || enterThreshold; | ||
var selfLeaveThreshold = parseFloat(el.dataset.leaveThreshold) || leaveThreshold; | ||
if (enterThreshold === 1) { | ||
enterThreshold = MAX_THRESHOLD; | ||
} | ||
if (selfEnterThreshold === 1) { | ||
selfEnterThreshold = MAX_THRESHOLD; | ||
} | ||
if (leaveThreshold === 1) { | ||
leaveThreshold = MAX_THRESHOLD; | ||
} | ||
if (selfLeaveThreshold === 1) { | ||
selfLeaveThreshold = MAX_THRESHOLD; | ||
} | ||
var isEnter; | ||
var isLeave; | ||
var checkEnter; | ||
var checkLeave; | ||
if (enterThreshold === leaveThreshold) { | ||
isEnter = function (entry) { return entry.isIntersecting; }; | ||
if (selfEnterThreshold === selfLeaveThreshold) { | ||
checkEnter = function (entry) { return entry.isIntersecting; }; | ||
isLeave = function (entry) { return leaveThreshold === 0 ? !entry.isIntersecting : entry.isIntersecting; }; | ||
} else { | ||
isEnter = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - enterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - leaveThreshold); | ||
return enterRatio <= leaveRatio; | ||
}; | ||
checkLeave = function (entry) { return selfLeaveThreshold === 0 ? !entry.isIntersecting : entry.isIntersecting; }; | ||
} else { | ||
checkEnter = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - selfEnterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - selfLeaveThreshold); | ||
return enterRatio <= leaveRatio; | ||
}; | ||
isLeave = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - enterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - leaveThreshold); | ||
return enterRatio >= leaveRatio; | ||
}; | ||
} | ||
checkLeave = function (entry) { | ||
var enterRatio = Math.abs(entry.intersectionRatio - selfEnterThreshold); | ||
var leaveRatio = Math.abs(entry.intersectionRatio - selfLeaveThreshold); | ||
return enterRatio >= leaveRatio; | ||
}; | ||
} | ||
var callback = function (entries) { | ||
entries.forEach(function (entry) { | ||
var el = entry.target; | ||
var isEnter = false; | ||
if (!el.intersectionEvents.isEnter && isEnter(entry)) { | ||
// enter | ||
el.intersectionEvents.isEnter = true; | ||
onEnter(el); | ||
isOnce && el.intersectionEvents.observer.unobserve(el); | ||
} else if (el.intersectionEvents.isEnter && isLeave(entry)) { | ||
// leave | ||
el.intersectionEvents.isEnter = false; | ||
onLeave(el); | ||
} | ||
}); | ||
}; | ||
var callback = function (entries) { | ||
entries.forEach(function (entry) { | ||
var el = entry.target; | ||
var observer = new IntersectionObserver(callback, { | ||
threshold: [leaveThreshold, enterThreshold] | ||
}); | ||
getElements(target).forEach(function (el) { | ||
el.intersectionEvents = {}; | ||
el.intersectionEvents.isEnter = false; | ||
var rate = window.innerHeight / el.offsetHeight; | ||
var isOverEnter = enterThreshold > rate; | ||
var isOverLeave = leaveThreshold > rate; | ||
el.intersectionEvents.observer = observer; | ||
if (isOverEnter || isOverLeave) { | ||
// When the height of the element is larger than the window, change threshold so that it fits within the window. | ||
el.intersectionEvents.observer = new IntersectionObserver(callback, { | ||
threshold: [isOverLeave ? leaveThreshold * rate : leaveThreshold, isOverEnter ? enterThreshold * rate : enterThreshold] | ||
if (!isEnter && checkEnter(entry)) { | ||
// enter | ||
isEnter = true; | ||
onEnter(el); | ||
isOnce && observer.unobserve(el); | ||
} else if (isEnter && checkLeave(entry)) { | ||
// leave | ||
isEnter = false; | ||
onLeave(el); | ||
} | ||
}); | ||
} | ||
}; // When the height of the element is larger than the window, change threshold so that it fits within the window. | ||
el.intersectionEvents.observer.observe(el); | ||
var rate = window.innerHeight / el.offsetHeight; | ||
var isOverEnter = selfEnterThreshold > rate; | ||
var isOverLeave = selfLeaveThreshold > rate; | ||
var observer = new IntersectionObserver(callback, { | ||
threshold: [isOverLeave ? selfLeaveThreshold * rate : selfLeaveThreshold, isOverEnter ? selfEnterThreshold * rate : selfEnterThreshold] | ||
}); | ||
observer.observe(el); | ||
}); | ||
@@ -143,0 +137,0 @@ }; |
/*! | ||
* intersection-events v0.2.6 | ||
* intersection-events v0.2.7 | ||
* https://github.com/ko-yelie/intersection-events#readme | ||
@@ -9,2 +9,2 @@ * | ||
*/ | ||
var IntersectionEvents=function(){"use strict";function e(){}function n(e){return e?Array.prototype.slice.call(e,0):[]}function t(e,t){return void 0===t&&(t=document),"string"==typeof e?n(t.querySelectorAll(e)):e.length?e.map?e:n(e):[e]}return function(n,r){void 0===r&&(r={});var i=r.onEnter,o=r.onLeave;void 0===o&&(o=e);var s=r.isOnce;if(void 0===s&&(s=!1),"IntersectionObserver"in window){var c=r.enterThreshold;void 0===c&&(c=.99);var v,a,u=r.leaveThreshold;void 0===u&&(u=0),1===c&&(c=.99),1===u&&(u=.99),c===u?(v=function(e){return e.isIntersecting},a=function(e){return 0===u?!e.isIntersecting:e.isIntersecting}):(v=function(e){return Math.abs(e.intersectionRatio-c)<=Math.abs(e.intersectionRatio-u)},a=function(e){return Math.abs(e.intersectionRatio-c)>=Math.abs(e.intersectionRatio-u)});var f=function(e){e.forEach(function(e){var n=e.target;!n.intersectionEvents.isEnter&&v(e)?(n.intersectionEvents.isEnter=!0,i(n),s&&n.intersectionEvents.observer.unobserve(n)):n.intersectionEvents.isEnter&&a(e)&&(n.intersectionEvents.isEnter=!1,o(n))})},E=new IntersectionObserver(f,{threshold:[u,c]});t(n).forEach(function(e){e.intersectionEvents={},e.intersectionEvents.isEnter=!1;var n=window.innerHeight/e.offsetHeight,t=c>n,r=u>n;e.intersectionEvents.observer=E,(t||r)&&(e.intersectionEvents.observer=new IntersectionObserver(f,{threshold:[r?u*n:u,t?c*n:c]})),e.intersectionEvents.observer.observe(e)})}else t(n).forEach(function(e){i(e)})}}(); | ||
var IntersectionEvents=function(){"use strict";function e(){}function t(e){return e?Array.prototype.slice.call(e,0):[]}function n(e,n){return void 0===n&&(n=document),"string"==typeof e?t(n.querySelectorAll(e)):e.length?e.map?e:t(e):[e]}return function(t,r){void 0===r&&(r={});var o=r.onEnter,i=r.onLeave;void 0===i&&(i=e);var a=r.isOnce;if(void 0===a&&(a=!1),"IntersectionObserver"in window){var s=r.enterThreshold;void 0===s&&(s=.99);var c=r.leaveThreshold;void 0===c&&(c=0),n(t).forEach(function(e){var t,n,r=parseFloat(e.dataset.enterThreshold)||s,u=parseFloat(e.dataset.leaveThreshold)||c;1===r&&(r=.99),1===u&&(u=.99),r===u?(t=function(e){return e.isIntersecting},n=function(e){return 0===u?!e.isIntersecting:e.isIntersecting}):(t=function(e){return Math.abs(e.intersectionRatio-r)<=Math.abs(e.intersectionRatio-u)},n=function(e){return Math.abs(e.intersectionRatio-r)>=Math.abs(e.intersectionRatio-u)});var v=!1,f=window.innerHeight/e.offsetHeight,h=new IntersectionObserver(function(e){e.forEach(function(e){var r=e.target;!v&&t(e)?(v=!0,o(r),a&&h.unobserve(r)):v&&n(e)&&(v=!1,i(r))})},{threshold:[u>f?u*f:u,r>f?r*f:r]});h.observe(e)})}else n(t).forEach(function(e){o(e)})}}(); |
{ | ||
"name": "intersection-events", | ||
"moduleName": "IntersectionEvents", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Wrapper of IntersectionObserver", | ||
@@ -6,0 +6,0 @@ "main": "dist/intersection-events.es.js", |
@@ -22,3 +22,3 @@ # intersection-events | ||
[unpkg](https://unpkg.com/intersection-events) (1.6 KB) | ||
[unpkg](https://unpkg.com/intersection-events) (1.33 KB) | ||
@@ -44,2 +44,9 @@ ```html | ||
```html | ||
<div class="js-target"></div> | ||
<!-- Set threshold only for this element --> | ||
<div class="js-target" data-enter-threshold="0.5" data-leave-threshold="0.5"></div> | ||
``` | ||
If you want to detect enter only once, set `isOnce` option to `true`. | ||
@@ -46,0 +53,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82
15385
250