-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.js
101 lines (85 loc) · 2.75 KB
/
news.js
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
const countries = ["us", "in", "gb", "se"];
const url = `https://newsdata.io/api/1/news?apikey=pub_40951b3dfdef003f3645cfd336d57f5007875&q=weather&country=us&category=environment`;
const newsContainer = document.getElementsByClassName("news-container");
let content = "<h2>Today's News</h2>";
fetchNews = async () => {
try{
const newsdata = await fetch(url);
const data = await newsdata.json();
renderNews(data);
}
catch( e) {
}
};
renderNews = (data) => {
console.log(data.results);
// Example: Using matchMedia to apply JavaScript based on media queries
const mq = window.matchMedia("(max-width: 470px)");
if (mq.matches) {
data.results.map((article) => {
content += `
<div class="news">
<div class="news-details">
<h3><a href="${article.link}" target="_blank">${article.title} </a>
</h3>
<div class="article-image">
<img
src="${article.image_url}"
alt="image"
/>
</div>
<p class="desc">
${article.description} <span><a target="_blank" href="${article.link}">Read More</a></span>
</p>
</div>
</div>
<hr/>
`;
});
} else {
data.results.map((article, index) => {
if (article.image_url === null || article.description === null) return;
if (index % 2 == 0) {
content += `
<div class="news">
<div class="article-image">
<img
src="${article.image_url}"
alt="image"
/>
</div>
<div class="news-details">
<h3><a href="${article.link}" target="_blank">${article.title} </a>
</h3>
<p class="desc">
${article.description} <span><a target="_blank" href="${article.link}">Read More</a></span>
</p>
</div>
</div>
<hr/>
`;
} else {
content += `
<div class="news">
<div class="news-details">
<h3><a href="${article.link}" target="_blank">${article.title} </a>
</h3>
<p class="desc">
${article.description} <span><a target="_blank" href="${article.link}">Read More</a></span>
</p>
</div>
<div class="article-image">
<img
src="${article.image_url}"
alt="image"
/>
</div>
</div>
<hr/>
`;
}
});
}
newsContainer[0].innerHTML = content;
};
fetchNews();