HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<body>
<!-- navigation -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/index.html">Movie List</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="">Home
<span class="sr-only">(current)</span>
</a>
</li>
<!-- <li class="nav-item">
<a class="nav-link" href="favorite.html">favorite</a>
</li> -->
</ul>
</div>
</nav>
<div class="container mt-5">
<!--search bar-->
<div class="row" id="search-bar">
<form id="search" class="col-10 form-inline">
<label class="sr-only" for="inlineFormInputName2">Name</label>
<input type="text" class="form-control mb-2 mr-sm-2" id="search-input" placeholder="search name ...">
<button type="submit" class="btn btn-primary mb-2">Search</button>
</form>
<!-- 新增切換顯示模式區塊 -->
<!-- view mode -->
<div class="col-2 text-right mt-auto mb-auto" id="view-mode">
<i class="fa fa-th fa-lg mr-2" aria-hidden="true" id="card-mode"></i>
<i class="fa fa-bars fa-lg" aria-hidden="true" id="list-mode"></i>
</div>
</div>
<!-- data-panel -->
<div class="container mt-5">
<div class="row" id="data-panel">
<!-- print movie list here -->
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="show-movie-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle"
aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="show-movie-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="show-movie-body">
<div class="row">
<div class="col-sm-8" id="show-movie-image">
</div>
<div class="col-sm-4">
<p><em id="show-movie-date"></em></p>
<p id="show-movie-description"></p>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center" id="pagination">
<li class="page-item">
<a class="page-link" href="#">1</a>
</li>
</ul>
</nav>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"
integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"
integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm"
crossorigin="anonymous"></script>
<script src="index.js"></script>
</body>
(function () {
const BASE_URL = 'https://movie-list.alphacamp.io'
const INDEX_URL = BASE_URL + '/api/v1/movies/'
const POSTER_URL = BASE_URL + '/posters/'
const data = []
const searchForm = document.getElementById('search')
const searchInput = document.getElementById('search-input')
const dataPanel = document.getElementById('data-panel')
const pagination = document.getElementById('pagination')
const ITEM_PER_PAGE = 12
const viewMode = document.getElementById('view-mode') // view-mode 元素
let mode = 'card-mode' // 預設顯示模式為card-mode
let currentPage = 1 // 目前所在頁面
let paginationData = []
// get api data and show the first page
axios.get(INDEX_URL).then((response) => {
data.push(...response.data.results)
// displayDataList(data)
getTotalPages(data)
getPageData(1, data)
}).catch((err) => console.log(err))
// listen to data panel
dataPanel.addEventListener('click', (event) => {
if (event.target.matches('.btn-show-movie')) {
showMovie(event.target.dataset.id)
} else if (event.target.matches('.btn-add-favorite')) {
addFavoriteItem(event.target.dataset.id)
}
})
// listen to search form submit event
searchForm.addEventListener('submit', event => {
event.preventDefault()
let results = []
const regex = new RegExp(searchInput.value, 'i')
results = data.filter(movie => movie.title.match(regex))
console.log(results)
// displayDataList(results)
getTotalPages(results)
getPageData(1, results)
})
// listen to pagination click event
pagination.addEventListener('click', event => {
console.log(event.target.dataset.page)
if (event.target.tagName === 'A') {
currentPage = event.target.dataset.page
getPageData(currentPage)
}
})
// listen to view mode button
viewMode.addEventListener('click', event => {
event.preventDefault()
console.log(event.target.id)
if (event.target.id === 'list-mode') {
mode = 'list-mode' // 模式改為list-mode
getTotalPages(data)
getPageData(currentPage, data) // 撈取目前所在頁面資料
} else if (event.target.id === 'card-mode') {
mode = 'card-mode' // 模式改為card-mode
getTotalPages(data)
getPageData(currentPage, data)
}
})
// list-mode HTML
function dispalyListMode(data) {
let htmlContent = `
<table class="table">
<tbody>
`
data.forEach(function (item, index) {
htmlContent += `
<tr>
<td>${item.title}</td>
<td>
<button class="btn btn-primary btn-show-movie" data-toggle="modal" data-target="#show-movie-modal"
data-id="${item.id}">More</button>
<button class="btn btn-info btn-add-favorite" data-id="${item.id}">+</button>
</td>
</tr>
`
})
htmlContent += `
</tbody>
</table>
`
dataPanel.innerHTML = htmlContent
}
// card-mode HTML
function displayDataList(data) {
let htmlContent = ''
data.forEach(function (item, index) {
htmlContent += `
<div class="col-sm-3">
<div class="card mb-2">
<img class="card-img-top " src="${POSTER_URL}${item.image}" alt="Card image cap">
<div class="card-body movie-item-body">
<h6 class="card-title">${item.title}</h5>
</div>
<!-- "More" button -->
<div class="card-footer">
<button class="btn btn-primary btn-show-movie" data-toggle="modal" data-target="#show-movie-modal" data-id="${item.id}">More</button>
<button class="btn btn-info btn-add-favorite" data-id="${item.id}">+</button>
</div>
</div>
</div>
`
})
dataPanel.innerHTML = htmlContent
}
function showMovie(id) {
// get elements
const modalTitle = document.getElementById('show-movie-title')
const modalImage = document.getElementById('show-movie-image')
const modalDate = document.getElementById('show-movie-date')
const modalDescription = document.getElementById('show-movie-description')
// set request url
const url = INDEX_URL + id
console.log(url)
// send request to show api
axios.get(url).then(response => {
const data = response.data.results
console.log(data)
// insert data into modal ui
modalTitle.textContent = data.title
modalImage.innerHTML = `<img src="${POSTER_URL}${data.image}" class="img-fluid" alt="Responsive image">`
modalDate.textContent = `release at : ${data.release_date}`
modalDescription.textContent = `${data.description}`
})
}
function addFavoriteItem(id) {
const list = JSON.parse(localStorage.getItem('favoriteMovies')) || []
const movie = data.find(item => item.id === Number(id))
if (list.some(item => item.id === Number(id))) {
alert(`${movie.title} is already in your favorite list.`)
} else {
list.push(movie)
alert(`Added ${movie.title} to your favorite list!`)
}
localStorage.setItem('favoriteMovies', JSON.stringify(list))
}
function getTotalPages(data) {
let totalPages = Math.ceil(data.length / ITEM_PER_PAGE) || 1
let pageItemContent = ''
for (let i = 0; i < totalPages; i++) {
pageItemContent += `
<li class="page-item">
<a class="page-link" href="javascript:;" data-page="${i + 1}">${i + 1}</a>
</li>
`
}
pagination.innerHTML = pageItemContent
}
function getPageData(pageNum, data) {
paginationData = data || paginationData
let offset = (pageNum - 1) * ITEM_PER_PAGE
let pageData = paginationData.slice(offset, offset + ITEM_PER_PAGE)
if (mode === 'list-mode') {
dispalyListMode(pageData) // show list-mode
} else if (mode === 'card-mode') {
displayDataList(pageData) // show card-mode
}
}
})()
Also see: Tab Triggers