如何用js实现电影院的分页
时间: 2023-07-16 17:16:46 浏览: 50
要实现电影院的分页,可以使用JavaScript和HTML来完成。以下是一些实现步骤:
1. 创建一个电影列表的容器,比如一个div元素,用来展示电影列表。
2. 通过Ajax或其他方式获取电影数据,并将数据渲染到电影列表容器中。
3. 创建一个分页容器,比如一个ul元素,用来展示分页器。
4. 根据电影数据的数量计算出需要显示的分页数。可以通过 Math.ceil() 函数来进行向上取整。
5. 使用循环语句来创建分页器的每个按钮,每个按钮代表一个分页。
6. 给每个分页按钮添加点击事件,点击按钮时根据按钮所代表的分页数重新渲染电影列表。
下面是一个示例代码,仅供参考:
HTML代码:
```html
<div id="movieList"></div>
<ul id="pagination"></ul>
```
JavaScript代码:
```javascript
// 电影数据
const movies = [
{ title: '电影1', director: '导演1' },
{ title: '电影2', director: '导演2' },
{ title: '电影3', director: '导演3' },
{ title: '电影4', director: '导演4' },
{ title: '电影5', director: '导演5' },
{ title: '电影6', director: '导演6' },
{ title: '电影7', director: '导演7' },
{ title: '电影8', director: '导演8' },
{ title: '电影9', director: '导演9' },
{ title: '电影10', director: '导演10' },
];
// 每页显示的电影数量
const pageSize = 3;
// 当前显示的分页
let currentPage = 1;
// 获取电影列表容器和分页容器
const movieList = document.getElementById('movieList');
const pagination = document.getElementById('pagination');
// 渲染电影列表
function renderMovieList() {
// 计算需要显示的电影数据
const startIndex = (currentPage - 1) * pageSize;
const endIndex = startIndex + pageSize;
const currentMovies = movies.slice(startIndex, endIndex);
// 渲染电影列表
let html = '';
currentMovies.forEach((movie) => {
html += `
<div>
<h2>${movie.title}</h2>
<p>导演:${movie.director}</p>
</div>
`;
});
movieList.innerHTML = html;
}
// 渲染分页器
function renderPagination() {
// 计算需要显示的分页数
const pageCount = Math.ceil(movies.length / pageSize);
// 渲染分页器
let html = '';
for (let i = 1; i <= pageCount; i++) {
html += `<li><a href="#" data-page="${i}">${i}</a></li>`;
}
pagination.innerHTML = html;
// 给分页按钮添加点击事件
const paginationLinks = pagination.querySelectorAll('a');
paginationLinks.forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault();
currentPage = parseInt(link.getAttribute('data-page'));
renderMovieList();
renderPagination();
});
});
// 高亮当前分页按钮
paginationLinks.forEach((link) => {
if (parseInt(link.getAttribute('data-page')) === currentPage) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
});
}
// 初始化
renderMovieList();
renderPagination();
```
这段代码将电影数据分成每页3条,然后根据当前分页数渲染电影列表和分页器。当用户点击分页按钮时,会重新渲染电影列表和分页器,并高亮当前分页按钮。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)