Skip to content

Commit

Permalink
Add observer to typing effect so it stops if not in viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlaki committed Mar 15, 2024
1 parent 0417bd5 commit 51c4c48
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/js/typewriter-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@
if (!document.querySelector('#typewriter')) return;

const typewriter = document.querySelector('[data-typewriter]').dataset.typewriter.split(', ');
const heroTitle = document.querySelector('.hero__title');

const options = {
root: null,
rootMargin: '0px',
threshold: 0.5

Check failure on line 10 in src/js/typewriter-init.js

View workflow job for this annotation

GitHub Actions / Lint

Missing trailing comma
};

if (typewriter.length <= 1) return;

new Typewriter('#typewriter', {
const typing = new Typewriter('#typewriter', {
strings: typewriter,
autoStart: true,
cursor: '',
loop: true,
pauseFor: 2500,
});

const callback = (entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
typing.start();
} else {
typing.pause();
}
});
};

const observer = new IntersectionObserver(callback, options);
observer.observe(heroTitle);
})();

0 comments on commit 51c4c48

Please sign in to comment.