Skip to content

Commit

Permalink
IntersectionObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
chwchw committed Jul 30, 2021
1 parent 8fe7a7a commit d959162
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 44 deletions.
60 changes: 19 additions & 41 deletions assets/js/countUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,29 @@ import { CountUp } from 'countup.js';
countUpItems[el.id] = new CountUp(el.id, el.dataset.endval, options);
});

function destroyCountUp(elID)
{
delete countUpItems.elID;
}

var latestKnownScrollY = 0;
var ticking = false;

function onScroll()
{
latestKnownScrollY = window.scrollY;
requestTick();
}

function requestTick()
{
if (!ticking) {
requestAnimationFrame(update);
}
ticking = true;
}

function update()
{
ticking = false;

Array.prototype.forEach.call(elements, function (el,i) {
if (true === isInViewport(el)) {
countUpItems[el.id].start(destroyCountUp(el.id));
let options = {
root: null,
rootMargin: '0px',
threshold: 1.0
};

const callback = function (entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
countUpItems[entry.target.id].start(destroyCountUp(entry.target));
}
});
}
};

requestAnimationFrame(update);
window.addEventListener('scroll', onScroll, false);
let observer = new IntersectionObserver(callback, options);

function isInViewport(el)
{
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
Array.prototype.forEach.call(elements, function (el,i) {
observer.observe(el);
});

);
function destroyCountUp(el)
{
delete countUpItems[el.id];
observer.unobserve(el);
}
})();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"entrypoints": {
"countup": {
"js": [
"/bundles/contaocountup/countup.67ac75d9.js"
"/bundles/contaocountup/countup.4c790889.js"
]
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"contaocountup/countup.js": "/bundles/contaocountup/countup.67ac75d9.js"
"contaocountup/countup.js": "/bundles/contaocountup/countup.4c790889.js"
}

0 comments on commit d959162

Please sign in to comment.