u11310019's picture
Upload folder using huggingface_hub
3c778ff verified
Raw History Blame Contribute Delete
5.49 kB
/* ==========================================================================
Apple Design System Interactive Logic
Handles nav toggles, configurator chip selection, accessory search & sticky bar
========================================================================== */
document.addEventListener('DOMContentLoaded', () => {
/* ------------------------------------------------------------------------
1. Configurator Storage & Color Chip Selection
------------------------------------------------------------------------ */
const storageChips = document.querySelectorAll('.configurator-chips-storage .configurator-option-chip');
const colorChips = document.querySelectorAll('.configurator-chips-color .configurator-option-chip');
const totalPriceEl = document.getElementById('configurator-total-price');
const stickyPriceEl = document.getElementById('sticky-price');
const configuratorImage = document.getElementById('configurator-preview-img');
let basePrice = 48900;
let storageExtra = 0;
// Storage selection
storageChips.forEach(chip => {
chip.addEventListener('click', () => {
storageChips.forEach(c => c.classList.remove('selected'));
chip.classList.add('selected');
storageExtra = parseInt(chip.dataset.priceExtra || '0', 10);
updatePrice();
});
});
// Color selection
colorChips.forEach(chip => {
chip.addEventListener('click', () => {
colorChips.forEach(c => c.classList.remove('selected'));
chip.classList.add('selected');
// Subtle feedback animation on preview image
if (configuratorImage) {
configuratorImage.style.opacity = '0.7';
setTimeout(() => {
configuratorImage.style.opacity = '1';
}, 150);
}
});
});
function updatePrice() {
const total = basePrice + storageExtra;
const formatted = 'NT$ ' + total.toLocaleString();
if (totalPriceEl) totalPriceEl.textContent = formatted;
if (stickyPriceEl) stickyPriceEl.textContent = formatted;
}
/* ------------------------------------------------------------------------
2. Accessories Grid Search Filtering
------------------------------------------------------------------------ */
const searchInput = document.getElementById('accessory-search-input');
const accessoryCards = document.querySelectorAll('.store-utility-card');
if (searchInput) {
searchInput.addEventListener('input', (e) => {
const term = e.target.value.toLowerCase().trim();
accessoryCards.forEach(card => {
const title = card.querySelector('.t-body-strong')?.textContent.toLowerCase() || '';
const desc = card.querySelector('.t-caption')?.textContent.toLowerCase() || '';
if (title.includes(term) || desc.includes(term)) {
card.style.display = 'flex';
} else {
card.style.display = 'none';
}
});
});
}
/* ------------------------------------------------------------------------
3. Floating Sticky Bar Scroll Trigger
------------------------------------------------------------------------ */
const stickyBar = document.getElementById('floating-sticky-bar');
const configuratorSection = document.getElementById('configurator-section');
if (stickyBar && configuratorSection) {
window.addEventListener('scroll', () => {
const rect = configuratorSection.getBoundingClientRect();
// Show sticky bar when user scrolls past top of configurator section
if (rect.top <= window.innerHeight && rect.bottom >= 0) {
stickyBar.classList.remove('hidden');
} else {
stickyBar.classList.add('hidden');
}
});
}
/* ------------------------------------------------------------------------
4. Mobile Hamburger Menu Toggle
------------------------------------------------------------------------ */
const hamburgerBtn = document.getElementById('hamburger-btn');
const globalNavLinks = document.querySelector('.global-nav-links');
if (hamburgerBtn && globalNavLinks) {
hamburgerBtn.addEventListener('click', () => {
const isExpanded = globalNavLinks.style.display === 'flex';
if (isExpanded) {
globalNavLinks.style.display = 'none';
} else {
globalNavLinks.style.display = 'flex';
globalNavLinks.style.flexDirection = 'column';
globalNavLinks.style.position = 'absolute';
globalNavLinks.style.top = '44px';
globalNavLinks.style.left = '0';
globalNavLinks.style.width = '100%';
globalNavLinks.style.backgroundColor = '#000000';
globalNavLinks.style.padding = '24px';
globalNavLinks.style.gap = '16px';
}
});
}
/* ------------------------------------------------------------------------
5. Add to Bag Interactive Feedback
------------------------------------------------------------------------ */
const buyButtons = document.querySelectorAll('.btn-primary, #add-to-bag-btn');
buyButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
if (btn.tagName === 'A' && btn.getAttribute('href') !== '#') return;
e.preventDefault();
const originalText = btn.textContent;
btn.textContent = '已加入購物袋 ✓';
btn.style.backgroundColor = '#34c759'; // Apple Success Green
setTimeout(() => {
btn.textContent = originalText;
btn.style.backgroundColor = '';
}, 2000);
});
});
});