-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
101 lines (90 loc) · 3.07 KB
/
scripts.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
document.addEventListener("DOMContentLoaded", function () {
document
.getElementById("searchForm")
.addEventListener("submit", function (e) {
e.preventDefault();
let searchText = document.getElementById("searchText").value;
fetchMovies(searchText);
});
animateMovies();
function fetchMovies(searchText) {
fetch(
`https://www.omdbapi.com/?s=${encodeURIComponent(
searchText
)}&apikey=834bdcbd`
)
.then((response) => response.json())
.then(onMoviesReceived)
.catch((error) => {
alert("Erreur lors de la récupération des données: " + error);
});
}
function onMoviesReceived(response) {
if (response.Response === "True") {
displayMovies(response.Search);
} else {
document.getElementById("movies").innerHTML =
'<div class="col">Film non trouvé.</div>';
}
}
function displayMovies(movies) {
let moviesContainer = document.getElementById("movies");
moviesContainer.innerHTML = ""; // Clear existing movies
movies.forEach((movie) => {
let movieElement = document.createElement("div");
movieElement.classList.add("col-md-4", "mb-3");
movieElement.innerHTML = `
<div class="card movie-card">
<img src="${movie.Poster}" class="card-img-top" alt="${movie.Title}" />
<div class="card-body">
<h5 class="card-title">${movie.Title}</h5>
<p class="card-text">${movie.Year}</p>
<button class="btn btn-primary read-more" data-id="${movie.imdbID}">
En savoir plus
</button>
</div>
</div>`;
moviesContainer.appendChild(movieElement);
});
animateMovies();
}
function animateMovies() {
let movies = document.querySelectorAll(".movie-card");
let windowHeight = window.innerHeight;
window.addEventListener("scroll", function () {
let windowTop = window.scrollY;
movies.forEach((movie) => {
let movieTop = movie.getBoundingClientRect().top + windowTop;
if (windowTop + windowHeight > movieTop) {
movie.classList.add("visible");
}
});
});
}
document.addEventListener("click", function (e) {
if (e.target && e.target.classList.contains("read-more")) {
let movieId = e.target.getAttribute("data-id");
fetchMovieDetails(movieId);
}
});
function fetchMovieDetails(movieId) {
fetch(`https://www.omdbapi.com/?i=${movieId}&apikey=834bdcbd`)
.then((response) => response.json())
.then(displayMovieDetails)
.catch((error) => {
alert("Erreur lors de la récupération des détails du film: " + error);
});
}
function displayMovieDetails(details) {
let modalBody = document.querySelector(".modal-body");
modalBody.innerHTML = `
<strong>Date de sortie:</strong> ${details.Released}<br>
<strong>Genre:</strong> ${details.Genre}<br>
<strong>Scénario:</strong> ${details.Plot}`;
let movieModal = new bootstrap.Modal(
document.getElementById("movieModal"),
{}
);
movieModal.show();
}
});