演示地址
Blurry Loading (50projects50days.com)
代码
html结构
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>模糊加载</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section class="bg"></section>
<div class="loading-text">100%</div>
<script src="./script.js"></script>
</body>
</html>
css样式
** {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
/* 弹性布局,垂直居中 */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.bg {
position: absolute;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
background: url("./th.jpg") no-repeat center;
z-index: -1;
}
.loading-text {
color: white;
font-size: 64px;
}
js行为
const loadText = document.querySelector(".loading-text")
const bg = document.querySelector('.bg')
let load = 0
let init = setInterval(bluring, 25)
function bluring() {
load++
if (load > 99) {
clearInterval(init)
}
loadText.innerHTML = `${load}%`
loadText.style.opacity = scale(load, 0, 100, 1, 0)
b
bg.style.filter = `blur(${scale(load, 0, 100, 30, 0)}px)`
}
const scale = (num, inMin, inMax, outMin, outMax) => {
return ((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin
}
参考资料:50 Projects 50 Days | Traversy Media
50 unique mini-projects to sharpen your HTML, CSS & JavaScript skills
网友评论