Skip to content

Instantly share code, notes, and snippets.

@ian-whitestone
Created January 17, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-whitestone/d1b39fba142853c109674abbbe30a399 to your computer and use it in GitHub Desktop.
Save ian-whitestone/d1b39fba142853c109674abbbe30a399 to your computer and use it in GitHub Desktop.
Give the people confetti πŸŽ‰
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.4.0/dist/confetti.browser.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="bottom_of_page">Thanks for scrolling to the bottom πŸ™Œ</div>
<div>Enjoy some confetti πŸŽ‰</div>
<script>
/*
Show confetti if they scroll to the bottom of the report
*/
function showConfetti() {
// Docs: https://www.npmjs.com/package/canvas-confetti
confetti({
particleCount: 150,
spread: 150,
origin: {
y: 1
}
});
}
// From stack overflow:
// https://stackoverflow.com/questions/123999/how-can-i-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433
$.fn.inView = function() {
if (!this.length)
return false;
var rect = this.get(0).getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
window.didTheyGetConfetti = false;
$(window).on('scroll', function() {
// if( $('#text_c2e50381-0d2e-4596-9324-8082b0498732').inView() ) {
if ($('#bottom_of_page').inView()) {
if (!window.didTheyGetConfetti) {
showConfetti()
window.didTheyGetConfetti = true;
}
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment