Shopify themes, liquid, logos, and UX
I've implemented a Sticky add to cart via code, but the Sticky add to cart is already visible at the beginning of the page when loading. Is there any chance to delay the sticky add to cart so that id will be visible later on when scrolling down for example after the normal ATC button is visible?
Website: https://punchbuddy.co/
No PW needed.
The Add to cart code is in: main-product.liquid
Code below:
Thank you
Use this code
<!-- Add this in the <head> section of your website -->
<style>
/* Hide the sticky button initially */
.sticky-add-to-cart {
display: none;
}
</style>
<!-- Add this at the end of the <body> section, just before the closing </body> tag -->
<script>
document.addEventListener("DOMContentLoaded", function() {
var normalAddToCartButton = document.querySelector(".product__form-wrapper .product__form button[type='submit']");
var stickyAddToCartButton = document.querySelector(".sticky-add-to-cart");
function showStickyAddToCartButton() {
// Show the sticky button when the normal button is about to be out of view
var rect = normalAddToCartButton.getBoundingClientRect();
if (rect.bottom <= 0) {
stickyAddToCartButton.style.display = "block";
} else {
stickyAddToCartButton.style.display = "none";
}
}
// Call the function on page load
showStickyAddToCartButton();
// Call the function on scroll
window.addEventListener("scroll", showStickyAddToCartButton);
});
</script>
Hi,
Thank you for your help.
Can you please confirm where do I implement the code? In the main-product.liquid?
Also, do I need to implement all code together? Hoping you can please advise. Thank you
Here is the complete tutorial for sticky add to cart:
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024