进度条动画跳过
<h1>JavaScript 百分比进度条</h1>
<div id="myProgress">
<div id="myBar">10%</div>
</div>
<br>
<button onclick="move()">点我</button>
#myProgress {
width: 100%;
background-color: #ddd;
}
#myBar {
width: 10%;
height: 30px;
background-color: #4CAF50;
text-align: center;
line-height: 30px;
color: white;
}
function move() {
var elem = document.getElementById("myBar");
var width = 10;
// 这里的定时器是关键,可通过修改20进行调整
var id = setInterval(frame, 20);
function frame() {
if (width >= 100) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';
}
}
}
实现数字的动画
function move() {
var elem = document.getElementById("myBar");
var width = 0;
var id = setInterval(frame, 1);
function frame() {
if (width >= 1000) {
clearInterval(id);
} else {
width++;
elem.innerHTML = width * 1 + '万';
}
}
}
JS改变css属性通过transition,animate,实现动画效果参考:
transition:js事件改变css属性,增加动画效果
animate:js点击页面空白处,实现操作