50 天学习 50 个项目 - HTMLCSS and JavaScript
day18-Background Slider(背景轮播图)
效果
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- 引入 Font Awesome 字体图标库的 CSS 样式表 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"
integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog=="
crossorigin="anonymous" />
<title>Background Slider</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<!-- 背景轮播图 -->
<div class="slider-container">
<!-- 每一张背景 -->
<div class="slide active" style="
background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
"></div>
<div class="slide" style="
background-image: url('https://images.unsplash.com/photo-1511593358241-7eea1f3c84e5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1934&q=80');
"></div>
<div class="slide" style="
background-image: url('https://images.unsplash.com/photo-1495467033336-2effd8753d51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
"></div>
<div class="slide" style="
background-image: url('https://images.unsplash.com/photo-1522735338363-cc7313be0ae0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2689&q=80');
"></div>
<div class="slide" style="
background-image: url('https://images.unsplash.com/photo-1559087867-ce4c91325525?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
"></div>
<!-- 左边按钮 -->
<button class="arrow left-arrow" id="left">
<i class="fas fa-arrow-left"></i>
</button>
<!-- 右边按钮 -->
<button class="arrow right-arrow" id="right">
<i class="fas fa-arrow-right"></i>
</button>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
/* 子元素居中 */
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
background-position: center center;
background-size: cover;
transition: 0.4s;
}
/* 此处伪元素的作用是遮罩层,是背景图变暗 */
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
z-index: -1;
}
/* 轮播图容器 */
.slider-container {
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
height: 70vh;
width: 70vw;
position: relative;
overflow: hidden;
}
/* 轮播图 */
.slide {
opacity: 0;
height: 100vh;
width: 100vw;
/* 显示图片居中部分 */
background-position: center center;
background-size: cover;
position: absolute;
top: -15vh;
left: -15vw;
transition: 0.4s ease;
z-index: 1;
}
/* 显示 */
.slide.active {
opacity: 1;
}
/* 按钮 */
.arrow {
/* 固定定位 */
position: fixed;
background-color: transparent;
color: #fff;
padding: 20px;
font-size: 30px;
border: 1px solid rgb(0, 255, 247);
/* 垂直方向居中 */
top: 50%;
transform: translateY(-50%);
border-radius: 50% 0 0 50%;
cursor: pointer;
outline: 0;
}
.left-arrow {
/* calc 计算 */
left: calc(15vw - 65px);
}
.right-arrow {
border-radius:0 50% 50% 0;
right: calc(15vw - 65px);
}
script.js
// 重点是 flex transform transition fixed 的应用
// 注意:在css中的相关位置不要变化,否则中间的图与body背景图不会完美拼接
// 1.获取元素节点
const body = document.body;
const slides = document.querySelectorAll('.slide');//所有的轮播图
const leftBtn = document.getElementById('left');//左按钮
const rightBtn = document.getElementById('right');//右按钮
let activeSlide = 0;//当前显示轮播图的索引
// 右按钮点击事件
rightBtn.addEventListener('click', () => {
// +1
activeSlide++
//越界 置0
if (activeSlide > slides.length - 1) {
activeSlide = 0
}
// 设置body背景
setBgToBody()
// 切换需要显示的轮播图
setActiveSlide()
})
//左按钮点击事件
leftBtn.addEventListener('click', () => {
// -1
activeSlide--
// 越界 置于最好一张图的索引
if (activeSlide < 0) {
activeSlide = slides.length - 1
}
setBgToBody()
setActiveSlide()
})
// 初次设置body背景
setBgToBody()
// 函数:设置背景图
function setBgToBody() {
body.style.backgroundImage = slides[activeSlide].style.backgroundImage
}
// 函数:设置轮播图
function setActiveSlide() {
// 排他
slides.forEach((slide) => slide.classList.remove('active'))
// 自己
slides[activeSlide].classList.add('active')
}