Spaces:
Running
Running
File size: 7,501 Bytes
3e0e17d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | // 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'
});
}
}
});
}); |