天天看點

【每日一練】67—通過擷取電影資源API來實作一個電影網站頁面

【每日一練】67—通過擷取電影資源API來實作一個電影網站頁面

寫在前面

今天這個練習案例是通過擷取電影網站TMDB的API來實作,練習頁面的影片資源均來源這個TMDB網站,它的網址:​​https://www.themoviedb.org/movie​​

這個網站的API對于個人開發者是可以免費使用,而商業應用是需要額外付費的,而我們作為個人練習使用,我們隻要注冊一個個人免費賬号,即可申請使用它們的API了。

現在,我們一起來看一下它的最終效果:

【每日一練】67—通過擷取電影資源API來實作一個電影網站頁面

另外,如果你還想了解更多的影視資源網站的話,我已經把它整理在了我們的【部落格網站】裡的【影視資源導航】裡了。

影視導航資源位址:​​http://www.webqdkf.com/webdh/videodh.html​​

有興趣的夥伴可以去了解一些,有的影視資源網站打開的話,需要自備梯子,所有影視資源網站裡的各類廣告,請大家不要随意點選參與,如果造成錢财人才損失,後果自負。

影視網站資源,我們大家在上面就看看免費的電影電視劇就好了。

接下來,我們再來看一下今天練習的詳細源碼。

HTML代碼:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>【每日一練】67—通過API來實作一個電影網站頁面</title>
  </head>
  <body>
    <header>
      <form id="form">
        <input type="text" id="search" class="search" placeholder="Search">
      </form>
    </header>
    <main id="main"></main>
  </body>
</html>      

CSS代碼:

:root {
  --primary-color: #22254b;
  --secondary-color: #373b69;
}


* {
  box-sizing: border-box;
}


body {
  background-color: var(--primary-color);
  font-family: 'Poppins', sans-serif;
  margin: 0;
}


header {
  padding: 1rem;
  display: flex;
  justify-content: flex-end;
  background-color: var(--secondary-color);
}


.search {
  background-color: transparent;
  border: 2px solid var(--primary-color);
  border-radius: 50px;
  font-family: inherit;
  font-size: 1rem;
  padding: 0.5rem 1rem;
  color: #fff;
}


.search::placeholder {
  color: #7378c5;
}


.search:focus {
  outline: none;
  background-color: var(--primary-color);
}


main {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}


.movie {
  width: 300px;
  margin: 1rem;
  background-color: var(--secondary-color);
  box-shadow: 0 4px 5px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
  border-radius: 3px;
}


.movie img {
  width: 100%;
}


.movie-info {
  color: #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap:0.2rem;
  padding: 0.5rem 1rem 1rem;
  letter-spacing: 0.5px;
}


.movie-info h3 {
  margin-top: 0;
}


.movie-info span {
  background-color: var(--primary-color);
  padding: 0.25rem 0.5rem;
  border-radius: 3px;
  font-weight: bold;
}


.movie-info span.green {
  color: lightgreen;
}


.movie-info span.orange {
  color: orange;
}


.movie-info span.red {
  color: red;
}


.overview {
  background-color: #fff;
  padding: 2rem;
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  max-height: 100%;
  transform: translateY(101%);
  overflow-y: auto;
  transition: transform 0.3s ease-in;
}


.movie:hover .overview {
  transform: translateY(0);
}      

JS代碼:

const API_URL = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=3fd2be6f0c70a2a598f084ddfb75487c&page=1'
const IMG_PATH = 'https://image.tmdb.org/t/p/w1280'
const SEARCH_API = 'https://api.themoviedb.org/3/search/movie?api_key=3fd2be6f0c70a2a598f084ddfb75487c&query="'


const main = document.getElementById('main')
const form = document.getElementById('form')
const search = document.getElementById('search')


// 擷取初始電影位址
getMovies(API_URL)


async function getMovies(url) {
    const res = await fetch(url)
    const data = await res.json()


    showMovies(data.results)
}


function showMovies(movies) {
    main.innerHTML = ''


    movies.forEach((movie) => {
        const { title, poster_path, vote_average, overview } = movie


        const movieEl = document.createElement('div')
        movieEl.classList.add('movie')


        movieEl.innerHTML = `
            <img src="${IMG_PATH + poster_path}" alt="${title}">
            <div class="movie-info">
          <h3>${title}</h3>
          <span class="${getClassByRate(vote_average)}">${vote_average}</span>
            </div>
            <div class="overview">
          <h3>Overview</h3>
          ${overview}
        </div>
        `
        main.appendChild(movieEl)
    })
}


function getClassByRate(vote) {
    if(vote >= 8) {
        return 'green'
    } else if(vote >= 5) {
        return 'orange'
    } else {
        return 'red'
    }
}


form.addEventListener('submit', (e) => {
    e.preventDefault()


    const searchTerm = search.value


    if(searchTerm && searchTerm !== '') {
        getMovies(SEARCH_API + searchTerm)


        search.value = ''
    } else {
        window.location.reload()
    }
})      

寫在最後

以上就是今天【每日一練】的全部内容,希望今天的小練習對你有用,如果你覺得有幫助的話,請點贊我,關注我,并将它分享給你身邊做開發的朋友,也許能夠幫助到他。

我是楊小愛,我們明天見。

【每日一練】67—通過擷取電影資源API來實作一個電影網站頁面