var isAnimating = false; function scrollLeftAnimate(elem, unit) { if (!elem || isAnimating) { //if element not found / if animating, do not execute slide return; } var time = 300; // animation duration in MS, the smaller the faster. var from = elem.scrollLeft; // to continue the frame posistion var aframe = 10; //fraction of frame frequency , set 1 for smoothest ~ set 10++ for lower FPS (reduce CPU usage) isAnimating = true; //if animating prevent double trigger animation var start = new Date().getTime(), timer = setInterval(function () { var step = Math.min(1, (new Date().getTime() - start) / time); elem.scrollLeft = ((step * unit) + from); if (step === 1) { clearInterval(timer); isAnimating = false; } }, aframe); } function initDealCarrousel(dealCarrouselID) { var target = document.querySelector("#" + dealCarrouselID + " .cfs-carrousel-flexbox"); var cardOutterWidth; var maxCarrouselScroll; if(!target) return; function updateUpaCarrouselInfo() { cardOutterWidth = document.querySelector("#" + dealCarrouselID + " .cfs-card").offsetWidth; //you can define how far the scroll maxCarrouselScroll = (document.querySelectorAll("#" + dealCarrouselID + " .cfs-card").length * cardOutterWidth) - document.querySelector("#" + dealCarrouselID + " .cfs-carrousel-flexbox") .clientWidth; } document.querySelector("#" + dealCarrouselID + " .deals-scroll-left").addEventListener("click", function () { updateUpaCarrouselInfo(); //in case window resized, will get new info if (target.scrollLeft > 0) { scrollLeftAnimate(target, -cardOutterWidth * 2); } } ); document.querySelector("#" + dealCarrouselID + " .deals-scroll-right").addEventListener("click", function () { updateUpaCarrouselInfo(); //in case window resized, will get new info if (target.scrollLeft < maxCarrouselScroll) { scrollLeftAnimate(target, cardOutterWidth * 2); } } ); } function initBuyNow() { const classes = document.getElementsByClassName("single-gallery"); if(classes.length > 0) { const elem = classes[0]; var cfs = document.createElement('div'); cfs.setAttribute("id", "cfs-buy-now"); cfs.innerHTML = '
Buy now.
'; //let parentDiv = elem.parentNode; //parentDiv.insertBefore(cfs, elem); //elem.after(cfs); elem.appendChild(cfs); } } // Initiate the container with ID initDealCarrousel('cfs_container'); //carrousel ID initBuyNow();