简体   繁体   English

你如何顺利地为twitter-bootstrap进度条制作动画?

[英]How do you animate a twitter-bootstrap progress bar smoothly?

I have multi-player game with a 30 second timer at the bottom of the screen. 我在屏幕底部有一个30秒计时器的多人游戏。 If none of the players make a move for 30 seconds, the form submits. 如果没有一个玩家进行30秒的移动,表单提交。

var ProgressValue = 0;
function showProgress() {
    ProgressValue += 100/30;
    if (ProgressValue > 100) {
        $('form').submit();
    }
    // Ajax is done here to see if anyone has made a move.
    $('.progress .bar').css('width',ProgressValue + '%');
    setTimeout(showProgress, 1000);
}

setTimeout(showProgress, 1000);

Each second, I check the Application scope to see if anyone has changed the value of 每一秒,我检查应用程序范围,看看是否有人更改了值

Application.LastMove

I want the progress bar to animate smoothly, but I don't want to do it by reducing the timeout value. 我想让进度条平滑动画,但我不想通过减少超时值来实现。 I think that checking to see if anyone has taken a move every second is enough load on the server already. 我认为检查是否有人每秒都进行一次移动已经足够加载服务器了。

I've heard of WebSockets, but my current server is on ColdFusion 8, so (I think) I'm satisfied with doing an ajax call every second, unless you feel that ajax is not as elegant and from a less civilized age. 我听说过WebSockets,但是我当前的服务器是在ColdFusion 8上,所以(我认为)我很满意每秒进行一次ajax调用,除非你觉得ajax不那么优雅,而且文明年龄较小。

Q: How do you animate a twitter-bootstrap progress bar smoothly from 3.3% to 6.6%? 问:你如何将twitter-bootstrap进度条平滑地从3.3%动画到6.6%?

Don't animate using jQuery, prefer CSS animation, unless you have to support old browsers. 不要使用jQuery动画,更喜欢CSS动画,除非你必须支持旧的浏览器。

I've made this copying from Bootstrap style: 我从Bootstrap样式中复制了这个:

.bar {
  -webkit-transition: width 30.0s ease !important;
     -moz-transition: width 30.0s ease !important;
       -o-transition: width 30.0s ease !important;
          transition: width 30.0s ease !important;
}

For so long transition, I suggest you to try different animations: http://www.the-art-of-web.com/css/timing-function/ 对于这么长时间的过渡,我建议你尝试不同的动画: http//www.the-art-of-web.com/css/timing-function/

In my example I've added two things that could be usefull: 在我的例子中,我添加了两个可能有用的东西:

  1. Button text changes when the animation starts and when it ends (just to check animation timings) 动画开始时和结束时按钮文本会发生变化(只是为了检查动画时序)
  2. Check if the browser support this animation: you can use your jQuery code as fallback mode 检查浏览器是否支持此动画:您可以将jQuery代码用作后备模式

For more information about how to detect CSS animation support: https://developer.mozilla.org/en-US/docs/CSS/CSS_animations/Detecting_CSS_animation_support 有关如何检测CSS动画支持的更多信息: https//developer.mozilla.org/en-US/docs/CSS/CSS_animations/Detecting_CSS_animation_support

Example: http://jsfiddle.net/CUbgr/5/ 示例: http//jsfiddle.net/CUbgr/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM