-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (75 loc) · 2.91 KB
/
script.js
File metadata and controls
86 lines (75 loc) · 2.91 KB
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
import { setCurrentYear } from './js/utils.js';
import { initNav, initSmoothScroll, initScrollNav, initScrollHeader } from './js/nav.js';
import { enforceDarkMode } from './js/theme.js';
import { initChatbot } from './js/chatbot.js';
document.addEventListener('DOMContentLoaded', () => {
enforceDarkMode();
initNav();
initSmoothScroll();
initScrollNav();
initScrollHeader();
setCurrentYear();
initChatbot();
initScrollTop();
// --- Animate Elements on Scroll ---
const scrollElements = document.querySelectorAll('.animate-on-scroll');
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const elementInView = (el, dividend = 1) => {
const elementTop = el.getBoundingClientRect().top;
return elementTop <= (window.innerHeight || document.documentElement.clientHeight) / dividend;
};
const handleScrollAnimation = () => {
scrollElements.forEach(el => {
if (elementInView(el, 1.15)) {
if (!prefersReducedMotion) {
el.classList.add('visible');
} else {
el.style.opacity = 1;
el.style.transform = 'translateY(0)';
el.classList.add('visible');
}
}
});
};
handleScrollAnimation();
if (!prefersReducedMotion) {
window.addEventListener('scroll', handleScrollAnimation);
}
// --- RSS News Ticker ---
fetchRSSFeed();
setInterval(fetchRSSFeed, 300000);
});
function initScrollTop() {
const btn = document.getElementById('scroll-top-btn');
if (!btn) return;
window.addEventListener('scroll', () => {
btn.classList.toggle('visible', window.pageYOffset > 400);
}, { passive: true });
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
async function fetchRSSFeed() {
try {
const corsProxy = 'https://api.allorigins.win/raw?url=';
const response = await fetch(corsProxy + encodeURIComponent('https://www.channelnewsasia.com/api/v1/rss-outbound-feed?_format=xml'));
const data = await response.text();
const parser = new DOMParser();
const xml = parser.parseFromString(data, 'application/xml');
const items = xml.querySelectorAll('item');
const tickerList = document.getElementById('tickerList');
if (!tickerList) return;
tickerList.innerHTML = '';
items.forEach(item => {
const title = item.querySelector('title')?.textContent;
if (!title) return;
const li = document.createElement('li');
li.textContent = title;
tickerList.appendChild(li);
});
// Clone items for smooth infinite scroll
tickerList.innerHTML += tickerList.innerHTML;
} catch (error) {
console.error('Error fetching RSS feed:', error);
}
}