css - 如何通过过渡元素在 CSS 中创建进度条
问题描述
您好如何通过过渡元素在 CSS 中创建进度条。
.pro {
height: 2px;
width: 100vw;
transition: width 2s ease-in-out;
background-color: red;
}
解决方案
你可以用keyframes
这个。
const btn = document.querySelector('.btn'), pro = document.querySelector('.pro')
btn.addEventListener('click', function() {
pro.classList.remove('progress');
addPro();
});
function addPro() {
setTimeout(function() {
pro.classList.add('progress');
}, 200)
}
*{
box-sizing: border-box;
}
.wrapper {
width: 95%;
margin: 50px auto;
}
.pro {
height: 2px;
width: 0;
max-width: 100%;
background-color: red;
}
.pro.progress {
animation: progress 1.75s ease-in-out forwards;
}
@keyframes progress {
from{
width: 0;
}to {
width: 100vw;
}
}
.btn {
display: inline-block;
padding: 10px 20px;
background: red;
color: #fff;
cursor: pointer;
}
<div class="wrapper">
<div class="pro progress"></div>
</div>
<div class="btn">show animation again</div>
推荐阅读
- python - 为什么不使用python的逻辑运算符或“x == 5 or 4”的形式抛出错误?
- javascript - 在高图中显示高于零而不是低于零的负 Y 值
- c++ - 为什么我们需要将标准库用于字符串而不是 int 数据类型?
- python - 熊猫检查系列值是否在区间内
- javascript - 将 Wrapper+Canvas+Div 匹配到图像的大小
- windows - 是否可以在 VS Code 中打开两个文件并在 Beyond Compare 中打开它们?
- elasticsearch - elasticsearch 搜索产品指数,但按价格指数排序
- json - 使用 actix_web 对部分结构进行 Serde 反序列化
- node.js - mongodb 按月+日期聚合并排序
- javascript - 为变量设置默认状态