Gny's picture
a single page for news with a picture, a carrousel for the articles with anchor links for each article in the bottom. the page must resize to every format.
3e0e17d verified
Raw History Blame Contribute Delete
7.5 kB
// Carousel functionality
class NewsCarousel {
constructor() {
this.carousel = document.getElementById('carousel');
this.items = document.querySelectorAll('.carousel-item');
this.prevBtn = document.getElementById('prevBtn');
this.nextBtn = document.getElementById('nextBtn');
this.prevBtnMobile = document.getElementById('prevBtnMobile');
this.nextBtnMobile = document.getElementById('nextBtnMobile');
this.anchors = document.querySelectorAll('.article-anchor');
this.currentIndex = 0;
this.itemWidth = 0;
this.gap = 24; // 6 * 4px = 24px
this.visibleItems = 1;
this.init();
}
init() {
this.calculateDimensions();
this.bindEvents();
this.updateCarousel(false);
this.observeArticles();
// Recalculate on resize
window.addEventListener('resize', () => {
this.calculateDimensions();
this.updateCarousel(false);
});
}
calculateDimensions() {
const containerWidth = this.carousel.parentElement.offsetWidth;
const width = window.innerWidth;
if (width >= 1024) {
this.visibleItems = 3;
this.itemWidth = (containerWidth - (this.gap * 2)) / 3;
} else if (width >= 640) {
this.visibleItems = 2;
this.itemWidth = (containerWidth - this.gap) / 2;
} else {
this.visibleItems = 1;
this.itemWidth = containerWidth;
}
}
bindEvents() {
// Desktop buttons
this.prevBtn?.addEventListener('click', () => this.prev());
this.nextBtn?.addEventListener('click', () => this.next());
// Mobile buttons
this.prevBtnMobile?.addEventListener('click', () => this.prev());
this.nextBtnMobile?.addEventListener('click', () => this.next());
// Anchor links
this.anchors.forEach(anchor => {
anchor.addEventListener('click', (e) => {
e.preventDefault();
const index = parseInt(anchor.dataset.index);
this.goTo(index);
});
});
// Keyboard navigation
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') this.prev();
if (e.key === 'ArrowRight') this.next();
});
// Touch/swipe support
let startX = 0;
let isDragging = false;
this.carousel.addEventListener('touchstart', (e) => {
startX = e.touches[0].clientX;
isDragging = true;
}, { passive: true });
this.carousel.addEventListener('touchmove', (e) => {
if (!isDragging) return;
}, { passive: true });
this.carousel.addEventListener('touchend', (e) => {
if (!isDragging) return;
isDragging = false;
const endX = e.changedTouches[0].clientX;
const diff = startX - endX;
if (Math.abs(diff) > 50) {
if (diff > 0) this.next();
else this.prev();
}
});
// Scroll to top
document.getElementById('scrollTop')?.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
prev() {
this.currentIndex = Math.max(0, this.currentIndex - 1);
this.updateCarousel();
}
next() {
const maxIndex = this.items.length - this.visibleItems;
this.currentIndex = Math.min(maxIndex, this.currentIndex + 1);
this.updateCarousel();
}
goTo(index) {
const maxIndex = this.items.length - this.visibleItems;
this.currentIndex = Math.max(0, Math.min(maxIndex, index));
this.updateCarousel();
// Scroll to carousel section
document.getElementById('latest').scrollIntoView({ behavior: 'smooth', block: 'start' });
}
updateCarousel(animate = true) {
const offset = -(this.currentIndex * (this.itemWidth + this.gap));
if (animate) {
this.carousel.style.transform = `translateX(${offset}px)`;
} else {
this.carousel.style.transition = 'none';
this.carousel.style.transform = `translateX(${offset}px)`;
setTimeout(() => {
this.carousel.style.transition = '';
}, 50);
}
this.updateAnchors();
this.updateButtonStates();
}
updateAnchors() {
this.anchors.forEach((anchor, index) => {
anchor.classList.toggle('active', index === this.currentIndex);
});
}
updateButtonStates() {
const maxIndex = this.items.length - this.visibleItems;
[this.prevBtn, this.prevBtnMobile].forEach(btn => {
if (btn) btn.style.opacity = this.currentIndex === 0 ? '0.3' : '1';
});
[this.nextBtn, this.nextBtnMobile].forEach(btn => {
if (btn) btn.style.opacity = this.currentIndex >= maxIndex ? '0.3' : '1';
});
}
// Intersection Observer to update anchors based on scroll
observeArticles() {
const options = {
root: null,
rootMargin: '-20% 0px -60% 0px',
threshold: 0
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const index = parseInt(entry.target.id.replace('article-', ''));
if (!isNaN(index)) {
this.currentIndex = index;
this.updateAnchors();
this.updateButtonStates();
}
}
});
}, options);
this.items.forEach(item => observer.observe(item));
}
}
// Header scroll effect
class HeaderEffect {
constructor() {
this.header = document.querySelector('header');
this.lastScroll = 0;
this.init();
}
init() {
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
// Add/remove background blur intensity
if (currentScroll > 100) {
this.header.classList.add('shadow-lg', 'shadow-black/20');
} else {
this.header.classList.remove('shadow-lg', 'shadow-black/20');
}
this.lastScroll = currentScroll;
});
}
}
// Initialize on DOM ready
document.addEventListener('DOMContentLoaded', () => {
new NewsCarousel();
new HeaderEffect();
// Re-initialize feather icons for dynamic content
if (typeof feather !== 'undefined') {
feather.replace();
}
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
if (href !== '#') {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
});
});