jQuery animate() 显示动画进度的百分比

发布于 2024-12-28 11:30:21 字数 198 浏览 4 评论 0原文

使用以下代码片段: http://jsfiddle.net/sylouuu/V7a3Y/2/ 我想在动画的 #log 中显示从 0% 到 100% 的进度百分比,通过回调很容易达到 100%...

可以这样做吗?

问候

With that following snippet: http://jsfiddle.net/sylouuu/V7a3Y/2/
I would like to show the % progress in #log of the animation from 0% to 100%, the 100% is easy with the callback...

Is it possible to do that?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2025-01-04 11:30:21

jQuery 动画进度回调在 1.8 中引入:

$('#a').animate({
    opacity: 1,
    width: 400,
    percent: 100
}, {
    progress: function(animation, progress, msRemaining) {
        $('#log').html(100 * progress + "%");
    }
});

The jQuery animate progress callback was introduced in 1.8:

$('#a').animate({
    opacity: 1,
    width: 400,
    percent: 100
}, {
    progress: function(animation, progress, msRemaining) {
        $('#log').html(100 * progress + "%");
    }
});
中性美 2025-01-04 11:30:21

我相信您可以通过使用步骤功能来做到这一点

 $('myelementid').animate({
   opacity: 1,
   height: 100,
   percent: 100
 },
 {
   step: function(now, fx) {
      //not sure if this is 100% percent accurate 
      //but at least you have a value at every step of the animation
      console.log( this.percent );          
   },
   complete: function(){
       //do not forget to reset percent at the end of the animaton
       //so on the next animation it can be calculated from starting value of 0 again
       this.percent = 0;
   }
 });

希望这会有所帮助。

I believe you can do that by using the step function

 $('myelementid').animate({
   opacity: 1,
   height: 100,
   percent: 100
 },
 {
   step: function(now, fx) {
      //not sure if this is 100% percent accurate 
      //but at least you have a value at every step of the animation
      console.log( this.percent );          
   },
   complete: function(){
       //do not forget to reset percent at the end of the animaton
       //so on the next animation it can be calculated from starting value of 0 again
       this.percent = 0;
   }
 });

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文