Skip to content

Instantly share code, notes, and snippets.

View tpage99's full-sized avatar

Taylor Page tpage99

View GitHub Profile
@tpage99
tpage99 / inView.js
Last active June 22, 2022 16:56
Using intersectionObserver to display an element when NOT in viewport
// setting up options for IntersectionObserver
// setting threshold to .9, in this instance, means when 90% of the element is out of view
let options = {
root: null,
threshold: [.9]
}
// function with ternary operator to check if threshold is met then display none but if not then update display
function showToATC(entryArray){
let div = document.getElementById('scroll-to-atc');
@tpage99
tpage99 / sticky-add-to-cart.liquid
Last active June 22, 2022 16:56
Pin add to cart button when not in view
{% comment %}
Script to be included likely in product form snippet
Tried using intersection observer, and this may indeed be better, but issue with mobile and the button presenting too soon. Best solution at this time seems to be comparing scroll position and only showing when the offset is greater than the actual scroll position.
{% endcomment %}
<script>
const bodyRect = document.body.getBoundingClientRect();
const btnRect = document.getElementById('atc-btn').getBoundingClientRect();
const offset = btnRect.top - bodyRect.top;
window.addEventListener('scroll', showToATC, false);
@tpage99
tpage99 / load-on-scroll.txt
Last active June 22, 2022 16:57
An example of how to load a script on scroll in Shopify
<script type="text/javascript">
window.addEventListener('scroll', scrollyScroll, false);
function scrollyScroll() {
let scrollPosition = 0;
scrollPosition = window.scrollY;
if (scrollPosition > 10) {
(function() {
var tidScript = document.createElement('script');
tidScript.type = 'text/javascript';