I was trying to animate with jQuery. But when I scroll back and forth, it keeps animating, and I want the width to be width: 50%
but it keeps incrementing the width.
JS Fiddle : http://jsfiddle.net/pPf7N/310/
var target = $('.target').offset().top + 1;
$(document).scroll(function(){
if($(this).scrollTop() > target ){
$('.progress-bar').animate({
'width': '50%'
});
}
});
.progress {
margin-top: 600px;
}
.target {
height: 1000px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
<div class="target">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">60% Complete</span>
</div>
</div>
</div>
3
Answers
Why you want that to be greater than target offsetTop value, when you set that it start your animation when you scroll back from bottom to top.
Please try this:
Demo: http://jsfiddle.net/s7egm818/
Bootstrap3 progress bar component already comes with animation. You can simply change
animate()
tocss()
when changing the width.If you want to have more control in the animation with jQuery’s
animate()
then one option is to simply add an indicator that you’ve finished animating the element.