Back to Shopify Liquid

Shopify Liquid
3 info sliders with images
Drop this Shopify Liquid snippet into your theme to add the 3 info sliders with images block. Copy the code below, paste it into your theme files, save and you're done.
255 lines · 9.2 KB
Shopify Liquid
1<!DOCTYPE html>2<html lang="en">34<head>5 <meta charset="UTF-8">6 <meta name="viewport" content="width=device-width, initial-scale=1.0">7 <title>Responsive Slider</title>8 <style>9 #v3-slider-container {10 overflow: hidden;11 position: relative;12 padding: 20px 5px;13 max-width: 1500px;14 margin-right: auto;15 margin-left: auto;16 }1718 @media only screen and (min-width: 768px) {19 #v3-slider-container {20 width: max-content;21 }22 }2324 #v3-slider {25 display: flex;26 transition: transform 0.5s ease-in-out;27 padding: 5px;28 }2930 .v3-slider-card {31 flex: 0 0 80%;32 max-width: 300px;33 height: max-content;34 display: flex;35 flex-direction: column;36 background-color: #fff;37 border: 1px solid #e0e0e0;38 border-radius: 10px;39 box-sizing: border-box;40 gap: 10px;41 transition: transform 0.3s ease;42 justify-content: space-around;43 margin-right: 10px;44 }4546 #v3-pagination {47 display: flex;48 justify-content: center;49 margin-top: 10px;50 display: none;51 }5253 .v3-pagination-dot {54 width: 18px;55 height: 6px;56 background-color: #0d437d29;57 cursor: pointer;58 }5960 .v3-pagination-dot.active {61 background-color: #f05454;62 width: 22px;63 border-radius: 5px;64 }6566 .content-container {67 padding: 10px;68 }6970 .feature-title {71 margin: 0px;72 font-weight: 700;73 color: #203E5C;74 }7576 .feature-image {77 border-top-right-radius: 10px;78 border-top-left-radius: 10px;79 }8081 .feature-description {82 font-size: 15px;83 font-weight: 500;84 }85 </style>86</head>8788<body>89 <div id="v3-slider-container">90 <div id="v3-slider">91 <div class="v3-slider-card">9293 <img src="//www.moonpod.co/cdn/shop/files/1_16.jpg?v=1720630615" class="feature-image">94 <div class="content-container">95 <h3 class="feature-title"><span>Relaxing Comfort</span></h3>96 <p class="feature-description"><span>Thousands of high density beads conform to every inch of your body, mimicking floatation therapy, a known treatment for reducing stress and anxiety.</span></p>97 </div>9899 </div>100 <div class="v3-slider-card">101102 <img src="//www.moonpod.co/cdn/shop/files/2_13.jpg?v=1720630634" class="feature-image">103 <div class="content-container">104 <h3 class="feature-title"><span>Upgraded Leisure</span></h3>105 <p class="feature-description"><span>People love to Moon Pod together. Our beads do the work, so it's easier to with loved ones. Reading, napping and gaming are all better on a Moon Pod.</span></p>106 </div>107108109 </div>110 <div class="v3-slider-card">111112 <img src="//www.moonpod.co/cdn/shop/files/3_14.jpg?v=1720630634" class="feature-image">113 <div class="content-container">114 <h3 class="feature-title"><span class="metafield-single_line_text_field">Elevated Relaxation</span></h3>115 <p class="feature-description"><span class="metafield-multi_line_text_field">Unparalleled support meets adaptive flexibility, relieving tension and joint pain while you relax. the perfect amount of structure for all shapes and sizes.</span></p>116 </div>117118119 </div>120 </div>121 <div id="v3-pagination"></div>122 </div>123124 <script>125 let sliderInitialized = false;126127 class ResponsiveSlider {128 constructor(containerId, sliderId, paginationId) {129 this.container = document.getElementById(containerId);130 this.slider = document.getElementById(sliderId);131 this.pagination = document.getElementById(paginationId);132 this.cards = Array.from(this.slider.children);133 this.cardWidth = this.cards[0].offsetWidth + 10; // Includes margin134 this.totalWidth = this.cardWidth * this.cards.length - 10; // Adjust total width135 this.containerWidth = this.slider.parentElement.offsetWidth;136 this.currentIndex = 0;137 this.isDragging = false;138 this.startX = 0;139 this.currentTranslate = 0;140 this.prevTranslate = 0;141 this.animationID = null;142143 this.init();144 }145146 init() {147 this.createPagination();148 this.addEventListeners();149 this.startAutoplay();150 }151152 updatePagination() {153 this.pagination.querySelectorAll('.v3-pagination-dot').forEach((dot, index) => {154 dot.classList.toggle('active', index === this.currentIndex);155 });156 }157158 goToSlide(index) {159 this.currentIndex = index;160 const maxOffset = Math.max(this.totalWidth - this.containerWidth, 0); // Prevent gaps161 const offset = Math.min(this.currentIndex * this.cardWidth, maxOffset);162 this.slider.style.transition = 'transform 0.5s ease';163 this.slider.style.transform = `translateX(-${offset}px)`;164 this.currentTranslate = -offset;165 this.prevTranslate = this.currentTranslate;166 this.updatePagination();167 }168169 createPagination() {170 const totalSlides = this.cards.length;171 this.pagination.innerHTML = '';172 for (let i = 0; i < totalSlides; i++) {173 const dot = document.createElement('div');174 dot.classList.add('v3-pagination-dot');175 dot.innerHTML = '​'; // Add zero-width space to prevent being treated as empty176 if (i === 0) dot.classList.add('active');177 dot.addEventListener('click', () => this.goToSlide(i));178 this.pagination.appendChild(dot);179 }180 }181182 autoplay() {183 this.currentIndex = (this.currentIndex + 1) % this.cards.length;184 this.goToSlide(this.currentIndex);185 }186187 startAutoplay() {188 setInterval(() => this.autoplay(), 3000);189 }190191 startDrag(event) {192 this.isDragging = true;193 this.startX = this.getPositionX(event);194 this.slider.style.transition = 'none';195 cancelAnimationFrame(this.animationID);196 }197198 endDrag() {199 this.isDragging = false;200 const movedBy = this.currentTranslate - this.prevTranslate;201 if (movedBy < -50 && this.currentIndex < this.cards.length - 1) {202 this.currentIndex++;203 }204 if (movedBy > 50 && this.currentIndex > 0) {205 this.currentIndex--;206 }207 this.goToSlide(this.currentIndex);208 }209210 drag(event) {211 if (this.isDragging) {212 const currentPosition = this.getPositionX(event);213 this.currentTranslate = this.prevTranslate + currentPosition - this.startX;214 this.slider.style.transform = `translateX(${this.currentTranslate}px)`;215 }216 }217218 getPositionX(event) {219 return event.type.includes('mouse') ? event.pageX : event.touches[0].clientX;220 }221222 addEventListeners() {223 this.slider.addEventListener('mousedown', (e) => this.startDrag(e));224 this.slider.addEventListener('touchstart', (e) => this.startDrag(e));225 this.slider.addEventListener('mouseup', () => this.endDrag());226 this.slider.addEventListener('touchend', () => this.endDrag());227 this.slider.addEventListener('mousemove', (e) => this.drag(e));228 this.slider.addEventListener('touchmove', (e) => this.drag(e));229 window.addEventListener('resize', () => {230 this.slider.style.transform = 'translateX(0)';231 this.currentIndex = 0;232 this.updatePagination();233 });234 }235 }236237 function initializeSlider() {238 if (!sliderInitialized && window.innerWidth < 768) {239 new ResponsiveSlider('v3-slider-container', 'v3-slider', 'v3-pagination');240 sliderInitialized = true;241 } else if (sliderInitialized && window.innerWidth >= 768) {242 sliderInitialized = false;243 document.getElementById('v3-slider').style.transform = 'translateX(0)';244 document.getElementById('v3-pagination').innerHTML = '';245 }246 }247248 initializeSlider();249250 window.addEventListener('resize', initializeSlider);251 </script>252253</body>254255</html>



