Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (82 loc) · 2.65 KB
/
Copy pathindex.js
File metadata and controls
101 lines (82 loc) · 2.65 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
92
93
94
95
96
97
98
99
100
101
/*
Pipe Mario
Script encargado de que la pequeña animación del separador funcione.
*/
const pipe = document.getElementById("pipe");
pipe.addEventListener("mouseover", changeMarioDisplay);
pipe.addEventListener("mouseout", changeMarioDisplay);
function changeMarioDisplay() {
const marioBehindPipe = document.getElementById("marioBehindPipe");
marioBehindPipe.classList.toggle("show-mario");
}
/* FETCH DATA FROM API */
// option 1
// function fetchNews(obj) {
// let options = {
// method: "GET",
// headers: {
// "x-api-key": "jZLWQ8iNkSZ4ev8ZiQPBmipPMQihSxxAQz8pshUehIQ"
// }
// }
// fetch("https://api.newscatcherapi.com/v2/search?q='nintendo + mario'&page_size=8&lang=es", options)
// .then(response => response.json())
// .then(data => obj(data.articles))
// .catch(error => console.log(error));
// }
// option 2 (in case option 1 doesn't work)
function fetchNews(obj) {
let options = {
method: "GET",
headers: {
"X-Api-Key": "eb6297ad56ac4b898639f7b92a7a0243",
}
}
fetch("https://newsapi.org/v2/everything?q='nintendo + mario'&pageSize=9&sortBy=publishedAt&language=es", options)
.then(response => response.json())
.then(data => obj(data.articles))
.catch(error => console.log(error));
}
// Here we pass the arguments to the fetchNews function
fetchNews((data) => { // here we pass the data to the function
let jsondata = data;
console.log(jsondata);
const newsContainer = document.getElementById("marioNews");
Object.keys(jsondata).map(function (key) {
let news = jsondata[key];
let newsTitle = news.title;
// let summary = news.summary;
let link = news.link || news.url;
let newsImage = news.media || news.urlToImage;
newsContainer.innerHTML += `
<div class="container-news__image">
<a href="${link}" target="_blank">
<img src="${newsImage}" alt="${newsTitle}">
<p>${newsTitle}</p>
</a>
</div>
`;
});
});
/*Burger menu*/
// Selector
var menu = document.querySelector(".hamburger");
// Method
function toggleMenu(event) {
this.classList.toggle("is-active");
document.querySelector(".navbar").classList.toggle("is_active");
event.preventDefault();
}
// Event
menu.addEventListener("click", toggleMenu, false);
const submitBtn = document.getElementById("submit");
submitBtn.addEventListener("click", submitForm);
/*
Function para el mensaje de feedback para saber que tu mensaje ha sido enviado.
*/
function submitForm(e) {
e.preventDefault();
alert("Your message has been sent to Mario");
}
/* Copyright year */
document.getElementById("copyright-year").textContent =
new Date().getFullYear();