我有 Bootstrap 的进度条,这是 html:
$(".progress-bar").each(function () { // .each(function (i, item) work too!
var progressBar = $(this); // $(this) would work too
progressBar.animate({
width: progressBar.data('width') + '%'
}, 1500);
});
<body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class='jumbotron' style='height:1400px' width='auto'>
<h1>please scroll down</h1>
</div>
<div class="alert alert-success">
<div class="skill-name">ON PROGRESS 357/487</div>
<div class="progress">
<div class="progress-bar progress-bar-primary" role="progressbar" data-width="70">
</div>
</div>
</div>
<div class="alert alert-danger">
<div class="skill-name">FAILED 30/487</div>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" data-width="30">
</div>
</div>
</div>
</body>
当我加载 HTML 时动画正在运行,因此动画进度条不起作用。 当我向下滚动查看进度条时如何设置动画?
最佳答案
这是一些东西:
Bootstrap :http://www.bootply.com/lRLMiaE7Gy
Jquery :
$(function(){
$(window).scroll(function(){
$(".progress-bar:not(.animated)").each(function(){
if ($(this).is(':visible')) {
var progressBar = $(this); // $(this) would work too
progressBar.animate({
width: progressBar.data('width') + '%'
}, 1500);
progressBar.addClass('animated');
}
});
});
});
片段
$(function(){
$(window).scroll(function(){
$(".progress-bar:not(.animated)").each(function(){
if ($(this).is(':visible')) {
var progressBar = $(this); // $(this) would work too
progressBar.animate({
width: progressBar.data('width') + '%'
}, 1500);
progressBar.addClass('animated');
}
});
});
});
<body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class='jumbotron' style='height:1400px' width='auto'>
<h1>please scroll down</h1>
</div>
<div class="alert alert-success">
<div class="skill-name">ON PROGRESS 357/487</div>
<div class="progress">
<div class="progress-bar progress-bar-primary" role="progressbar" data-width="70">
</div>
</div>
</div>
<div class="alert alert-danger">
<div class="skill-name">FAILED 30/487</div>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" data-width="30">
</div>
</div>
</div>
</body>
关于javascript - 滚动时执行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089114/