Interactive Product Slider
Use of VajroSDK getProducts helpers to create an interactive product slider
<div class="shop-section">
<div class="tab-container">
<div class="tab active" data-handle="flours-mixes"><t4>Flours</t4></div>
<div class="tab" data-handle="sweetener-salt"><t4>Sweetener & Salt</t4></div>
<div class="tab" data-handle="cookies-snacks-sweets"><t4>Snacks & Sweets</t4></div>
</div>
<div id="products-container" class="horizontal-product-grid"></div>
<div id="loader" class="loader hidden">
<div class="spinner"></div>
</div>
</div>:root {
--color-primary-font: #07004b;
--color-secondary-font: #646262;
--color-tertiary-font: #989898;
--color-primary-brand: #07004b;
--color-secondary-brand: #EBEBEB;
--color-tertiary-brand: #000000;
}
/* General section padding */
.shop-section {
padding: 4px 16px;
}
/* Make tabs scrollable horizontally */
.tab-container {
display: flex;
overflow-x: auto;
gap: 10px;
padding: 12px;
scroll-behavior: smooth; /* Smooth for programmatic scroll */
-webkit-overflow-scrolling: touch; /* Momentum scroll on iOS */
scrollbar-width: none; /* Hide scrollbar (Firefox) */
}
.tab-container::-webkit-scrollbar {
display: none; /* Hide scrollbar (WebKit) */
}
.tab {
padding: 10px 16px;
background: var(--color-secondary-brand);
border-radius: 8px;
cursor: pointer;
font-size: 14px;
white-space: nowrap;
color: var(--color-primary-font);
transition: background 0.3s, color 0.3s;
font-family: var(--font-secondary);
flex-shrink: 0; /* Prevent shrinking */
}
.tab.active {
background: var(--color-primary-brand);
font-weight: bold;
color: #fff;
}
/* Horizontal scroll for products */
.horizontal-product-grid {
display: flex;
overflow-x: auto;
gap: 16px;
padding: 16px;
/* Smooth scrolling + snapping */
scroll-behavior: smooth;
scroll-snap-type: x mandatory;
/* Mobile optimizations */
-webkit-overflow-scrolling: touch;
will-change: scroll-position;
backface-visibility: hidden;
transform: translateZ(0);
}
.horizontal-product-grid::-webkit-scrollbar {
display: none;
}
.product-card {
flex: 0 0 70%; /* Card width (~70% of viewport) */
scroll-snap-align: center; /* Snap to center */
border-radius: 20px;
background: #fff;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
/* Active card zoom effect */
.product-card.active {
transform: scale(1.05);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Left/right inactive cards */
.product-card.inactive-left,
.product-card.inactive-right {
transform: scale(0.9);
opacity: 0.7;
}
.product-image {
width: 100%;
height: 180px;
object-fit: cover;
border-radius: 20px 20px 0 0;
}
.product-info {
padding: 10px;
text-align: center;
}
.product-name {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
line-height: 1.2em;
max-height: 2.4em;
font-size: 14px;
font-weight: 300;
color: var(--color-primary-font);
}
.product-price-block {
display: flex;
justify-content: center;
align-items: baseline;
flex-wrap: wrap;
gap: 6px;
margin-top: 6px;
}
.product-price-current {
font-weight: bold;
font-size: 14px;
color: var(--color-primary-font);
}
.product-price-original {
font-size: 12px;
text-decoration: line-through;
color: var(--color-tertiary-font);
}
/* Loader */
.loader {
display: flex;
justify-content: center;
align-items: center;
padding: 40px 0;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid var(--color-secondary-brand);
border-top: 4px solid var(--color-primary-brand);
border-radius: 50%;
animation: spin 1s linear infinite;
}
.hidden {
display: none;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* View all button */
.view-all-wrapper {
display: flex;
justify-content: center;
align-items: center;
}
.view-all-button {
background: var(--color-primary-brand);
color: white;
border: none;
padding: 12px 24px;
font-size: 14px;
height: fit-content;
font-weight: 600;
border-radius: 8px;
cursor: pointer;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transition: background 0.3s ease, transform 0.2s ease;
}
.view-all-button:hover {
background: #07004b;
transform: scale(1.05);
}
/* Smoothing across devices */
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
overflow-x: hidden;
}const tabs = document.querySelectorAll('.tab');
const container = document.getElementById('products-container');
function renderProducts(products, collectionHandle, collectionId) {
if (!container) return;
container.replaceChildren();
const fragment = document.createDocumentFragment();
const limitedProducts = products.slice(0, 10);
limitedProducts.forEach((product, index) => {
const defaultVariant = product.variants?.[0] || {};
const imageUrl = product.images?.[0] || '';
const price = parseFloat(defaultVariant.price || 0);
const compareAtPrice = parseFloat(defaultVariant.compareAtPrice || 0);
const hasDiscount = compareAtPrice > price;
const card = document.createElement('div');
card.className = 'product-card';
card.dataset.index = index;
card.addEventListener('click', () => {
VajroSDK.actions.openProduct({ productHandle: product.handle });
});
card.innerHTML = `
<div class="image-container">
<img src="${imageUrl}" alt="${product.title}" class="product-image" />
</div>
<div class="product-info">
<t4 class="product-name">${product.title}</t4>
<div class="product-price-block">
<t4 class="product-price-current">Rs. ${price.toLocaleString("en-IN")}</t4>
${hasDiscount ? `<div class="product-price-original">RS. ${compareAtPrice.toLocaleString("en-IN")}</div>` : ''}
</div>
</div>
`;
fragment.appendChild(card);
});
const viewAllBtn = document.createElement('button');
viewAllBtn.className = 'view-all-button';
viewAllBtn.textContent = 'View All';
viewAllBtn.addEventListener('click', () => {
VajroSDK.actions.openCollection({
collectionHandle: collectionHandle,
collectionId: collectionId
});
});
const viewAllWrapper = document.createElement('div');
viewAllWrapper.className = 'view-all-wrapper';
viewAllWrapper.appendChild(viewAllBtn);
fragment.appendChild(viewAllWrapper);
container.appendChild(fragment);
updateCardStyles();
container.addEventListener('scroll', updateCardStyles);
}
function updateCardStyles() {
const cards = container.querySelectorAll('.product-card');
const containerRect = container.getBoundingClientRect();
const center = containerRect.left + containerRect.width / 2;
cards.forEach(card => {
const rect = card.getBoundingClientRect();
const cardCenter = rect.left + rect.width / 2;
const distance = Math.abs(center - cardCenter);
card.classList.remove('active', 'inactive-left', 'inactive-right');
if (distance < rect.width / 2) {
card.classList.add('active');
} else if (cardCenter < center) {
card.classList.add('inactive-left');
} else {
card.classList.add('inactive-right');
}
});
}
function fetchProductsByCollectionId(collectionId) {
VajroSDK.helpers.getProducts({
collectionId,
count: 10,
sortOrder: "created-descending"
})
.then((data) => {
const products = data.products || data;
if (!products?.length) {
container.innerHTML = '<p>No products found.</p>';
return;
}
const collectionHandle = Object.keys(collectionMap).find(
key => collectionMap[key] === collectionId
);
renderProducts(products, collectionHandle, collectionId);
})
.catch((err) => {
console.error(`❌ Error fetching products:`, err);
container.innerHTML = '<p>Error loading products.</p>';
VajroSDK.actions.showToast({
title: "Fetch Error",
message: "Failed to load products.",
});
});
}
const collectionMap = {
"flours-mixes": "429413892405",
"sweetener-salt": "430018724149",
"cookies-snacks-sweets": "429413925173"
};
tabs.forEach(tab => {
tab.addEventListener('click', () => {
document.querySelector('.tab.active')?.classList.remove('active');
tab.classList.add('active');
const handle = tab.dataset.handle;
const collectionId = collectionMap[handle];
if (collectionId) {
fetchProductsByCollectionId(collectionId);
} else {
console.warn("Unknown tab handle:", handle);
}
});
});
// Initial load
fetchProductsByCollectionId("429413892405");Updated 4 months ago