-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
93 lines (78 loc) · 2.6 KB
/
scripts.js
File metadata and controls
93 lines (78 loc) · 2.6 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
87
88
89
90
91
// Global variables
let allArticles = [];
// Initialize the page
document.addEventListener('DOMContentLoaded', function() {
// Load articles data
loadArticles();
// Initialize animation delays for sections
const sections = document.querySelectorAll('section');
sections.forEach((section, index) => {
section.style.animationDelay = `${index * 0.1}s`;
});
});
// Load articles from JSON file
function loadArticles() {
fetch('articles.json')
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Articles loaded successfully:", data);
allArticles = data.articles;
renderArticles();
})
.catch(error => {
console.error('Error loading articles:', error);
// Create fallback articles display if JSON loading fails
displayFallbackArticles();
});
}
// Fallback if JSON loading fails
function displayFallbackArticles() {
const container = document.getElementById('articles-container');
container.innerHTML = `Error loading articles.`;
}
// Render articles
function renderArticles() {
const articlesContainer = document.getElementById('articles-container');
articlesContainer.innerHTML = '';
allArticles.forEach(article => {
const articleElement = createArticleElement(article);
articlesContainer.appendChild(articleElement);
});
}
// Create HTML element for an article
function createArticleElement(article) {
const articleItem = document.createElement('div');
articleItem.className = 'article-item';
// Make entire item clickable
articleItem.addEventListener('click', function(e) {
// If clicking on the link itself, let it handle navigation normally
if (e.target.tagName === 'A') {
return;
}
// Otherwise, navigate using the article link in a new tab
window.open(article.link || '#', '_blank');
});
// Create content container
const content = document.createElement('div');
content.className = 'article-content';
// Add title as link
const title = document.createElement('h3');
title.className = 'article-title';
const titleLink = document.createElement('a');
titleLink.href = article.link || '#';
titleLink.target = '_blank';
titleLink.textContent = article.title;
title.appendChild(titleLink);
content.appendChild(title);
// Add description
const description = document.createElement('p');
description.textContent = article.description;
content.appendChild(description);
articleItem.appendChild(content);
return articleItem;
}