j7n*_*n7k 42 javascript jquery twitter-bootstrap
我有一个页面有几个bootstrap进度条.最初设置它们的值工作正常.虽然我希望进度条在用户打开页面时动画/转换到其特定状态.
当你点击其中一个栏时,这个JS工作正常.我会在酒吧的"onload"事件中需要类似的东西.但是"onload"事件不适用于s
//animate progress bars
$('.progress .bar').on("click", function(event) {
var me = $(this);
perc = me.attr("data-percentage");
me.css('width', perc+'%');
});
Run Code Online (Sandbox Code Playgroud)
如何在页面加载页面上的所有进度条上实现此行为?
Tat*_*nit 72
编辑
bar为progress-barHTML
<div class="container">
<div class="progress progress-striped active">
<div class="bar" style="width: 0%;"></div>
</div>
</div>?
Run Code Online (Sandbox Code Playgroud)
CSS
@import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
.container {
margin-top: 30px;
width: 400px;
}?
Run Code Online (Sandbox Code Playgroud)
jQuery用在下面和下面的小提琴中document.ready
$(document).ready(function(){
var progress = setInterval(function() {
var $bar = $('.bar');
if ($bar.width()>=400) {
clearInterval(progress);
$('.progress').removeClass('active');
} else {
$bar.width($bar.width()+40);
}
$bar.text($bar.width()/4 + "%");
}, 800);
});?
Run Code Online (Sandbox Code Playgroud)
演示
j7n*_*n7k 27
虽然Tats_innit的答案有很好的解决方法,但由于我在页面上有多个进度条,因此我必须采用不同的方式.
这是我的解决方案:
JSfiddle:http://jsfiddle.net/vacNJ/
HTML(示例):
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="60"></div>
</div>
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="50"></div>
</div>
<div class="progress progress-success">
<div class="bar" style="float: left; width: 0%; " data-percentage="40"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
setTimeout(function(){
$('.progress .bar').each(function() {
var me = $(this);
var perc = me.attr("data-percentage");
var current_perc = 0;
var progress = setInterval(function() {
if (current_perc>=perc) {
clearInterval(progress);
} else {
current_perc +=1;
me.css('width', (current_perc)+'%');
}
me.text((current_perc)+'%');
}, 50);
});
},300);
Run Code Online (Sandbox Code Playgroud)
@Tats_innit:使用setInterval()动态重新计算进度是一个很好的解决方案,他们的伙计!;)
编辑:
我的一个朋友为自定义twitter bootstrap进度条写了一个很好的jquery插件.这是一个演示:http: //minddust.github.com/bootstrap-progressbar/
这是Github回购:https: //github.com/minddust/bootstrap-progressbar
ell*_*uty 19
Bootstrap使用CSS3过渡,因此当您通过javascript/jQuery设置.bar的宽度时,进度条会自动生成动画.
http://jsfiddle.net/3j5Je/ ..see?
小智 10
为ellabeauty的答案做出贡献.您也可以使用此动态百分比值
$('.bar').css('width', function(){ return ($(this).attr('data-percentage')+'%')});
Run Code Online (Sandbox Code Playgroud)
并且可能会为您的CSS添加自定义缓动
.bar {
-webkit-transition: width 2.50s ease !important;
-moz-transition: width 2.50s ease !important;
-o-transition: width 2.50s ease !important;
transition: width 2.50s ease !important;
}
Run Code Online (Sandbox Code Playgroud)
这是一个跨浏览器的纯 CSS 解决方案。希望能帮助到你!
.progress .progress-bar {
-moz-animation-name: animateBar;
-moz-animation-iteration-count: 1;
-moz-animation-timing-function: ease-in;
-moz-animation-duration: .4s;
-webkit-animation-name: animateBar;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in;
-webkit-animation-duration: .4s;
animation-name: animateBar;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: .4s;
}
@-moz-keyframes animateBar {
0% {-moz-transform: translateX(-100%);}
100% {-moz-transform: translateX(0);}
}
@-webkit-keyframes animateBar {
0% {-webkit-transform: translateX(-100%);}
100% {-webkit-transform: translateX(0);}
}
@keyframes animateBar {
0% {transform: translateX(-100%);}
100% {transform: translateX(0);}
}Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h3>Progress bar animation on load</h3>
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: 75%;"></div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)