新增第226个小实例:全屏背景切换动画

This commit is contained in:
DESKTOP-BM6RJU5\wyanh 2023-07-18 18:11:24 +08:00
parent d80797940b
commit 1e5ad2793e
4 changed files with 186 additions and 0 deletions

View File

@ -233,6 +233,7 @@
223. HTML5+CSS3+JS小实例暗紫色Tabbar
224. HTML5+CSS3小实例按钮边框动效
225. HTML5+CSS3+JS小实例全屏范围滑块
226. HTML5+CSS3+JS小实例全屏背景切换动画
#### 赞赏作者
![image](https://gitee.com/wyanhui02/html_css_demo/raw/master/images/%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85/%E8%B5%9E%E8%B5%8F%E7%A0%81.jpg)

130
code/226/226.css Normal file
View File

@ -0,0 +1,130 @@
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
}
.container{
width: 100%;
height: 100%;
/* 相对定位 */
position: relative;
/* 溢出隐藏 */
overflow: hidden;
}
img{
/* 绝对定位 */
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 0;
width: 100vw;
height: 100%;
/* 模糊滤镜 默认不模糊 */
filter: blur(0px);
}
.active + img{
transform: translateX(100%);
}
/* .transition-start .right这两个样式在后面的js中用到 */
img.transition-start.right{
/* 执行动画:动画名 时长 线性的 停留在最后一帧 */
animation: transition-right-start 0.5s linear forwards;
}
/* 定义动画 */
/* 开始切换的动画 */
@keyframes transition-right-start {
0%{
width: 100vw;
height: 100%;
filter: blur(0px);
transform: translateX(-50%);
}
3%{
width: 300vw;
height: 100%;
filter: blur(3px);
transform: translateX(-50%);
}
5%{
width: 600vw;
height: 100%;
filter: blur(3px);
transform: translateX(-50%);
}
95%{
width: 6000vw;
height: 100%;
filter: blur(3px);
transform: translateX(-100%);
}
100%{
width: 100vw;
height: 100%;
filter: blur(0px);
transform: translateX(-150%);
}
}
/* .transition-end .right这两个样式在后面的js中用到 */
img.transition-end.right{
/* 执行动画:动画名 时长 线性的 停留在最后一帧 */
animation: transition-right-end 0.5s linear forwards;
}
/* 定义动画 */
/* 切换结束的动画 */
@keyframes transition-right-end {
0%{
width: 100vw;
height: 100%;
filter: blur(0px);
transform: translateX(100%);
}
3%{
width: 300vw;
height: 100%;
filter: blur(3px);
transform: translateX(100%);
}
5%{
width: 600vw;
height: 100%;
filter: blur(3px);
transform: translateX(100%);
}
95%{
width: 6000vw;
height: 100%;
filter: blur(3px);
transform: translateX(-50%);
}
100%{
width: 100vw;
height: 100%;
filter: blur(0px);
transform: translateX(-50%);
}
}
/* NEXT按钮 */
.btn-next{
--color:rgba(255,255,255,0.5);
border: 1px solid var(--color);
width: 120px;
height: 40px;
line-height: 40px;
text-align: center;
position: absolute;
z-index: 999;
left: 50%;
transform: translateX(-50%);
bottom: 30px;
color: var(--color);
border-radius: 20px;
background: rgba(0,0,0,0.15);
cursor: pointer;
}
.btn-next:hover{
--color:rgba(255,255,255,0.75);
background: rgba(0,0,0,0.25);
}

19
code/226/226.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全屏背景切换动画</title>
<link rel="stylesheet" href="226.css">
</head>
<body>
<div class="container">
<div>
<img src="https://picsum.photos/1920/1080?random=1" alt="" id="img-0" class="active">
<img src="https://picsum.photos/1920/1080?random=2" alt="" id="img-1">
</div>
<div class="btn-next">NEXT</div>
</div>
</body>
</html>
<script src="226.js"></script>

36
code/226/226.js Normal file
View File

@ -0,0 +1,36 @@
// next按钮
const button=document.querySelector('.btn-next');
// 图片数量
const totalElements=2;
// 图片下标
let index=0;
// 绑定next按钮点击事件
button.onclick=function(){
// 当前图片
var currentImage=document.getElementById(`img-${index}`);
index++;
if(index>=totalElements){
// 下标超出,回到第一张
index=0;
}
// 下一张图片
var nextImage=document.getElementById(`img-${index}`);
currentImage.classList.add('transition-start');
currentImage.classList.add('right');
nextImage.classList.add('transition-end');
nextImage.classList.add('right');
// 当前图片动画结束事件
currentImage.onanimationend=function(){
currentImage.classList.remove('active');
currentImage.classList.remove('transition-start');
currentImage.classList.remove('right');
}
// 下一张图片动画结束事件
nextImage.onanimationend=function(){
nextImage.classList.add('active');
nextImage.classList.remove('transition-end');
nextImage.classList.remove('right');
}
}