简体   繁体   English

javascript中平滑div宽度的变化

[英]Smooth div width change in javascript

I have a simple function which is increasing the width of a div but its not doing it smoothly its kinda of "juddery". 我有一个简单的功能,它增加了div的宽度,但它没有顺利地做它有点“颤抖”。

I'm using request animation frame to do this on Chrome.. and I decided not to round the numbers so I could get decimal width increments.. but I can't get it to be smooth at all was wondering how I can improve on my method. 我正在使用请求动画框架在Chrome上执行此操作..我决定不对数字进行舍入,这样我就可以获得十进制宽度增量..但我无法让它变得平滑,我想知道如何改进我的方法。

This is my function: 这是我的功能:

function test(data){


var start = parseInt(data.start);
var length = parseInt(data.length); //total length in minutes to complete
var dif = (new Date().getTime() / 1000) - start; //start holds seconds since epoch
var minutes = dif / 60; //convert seconds past into minutes past
var percentage = (minutes/length) * 100;

if(percentage > 100){ percentage = 100; }

    if( percentage != 100 ){
        document.getElementById('r').style.width = percentage+'%';
        document.getElementById('rt').innerHTML = Math.round(percentage)+'%';
    } else if (percentage == 100 ){
        document.getElementById('r').style.width = percentage+'%';
        document.getElementById('rt').innerHTML = 'COMPLETED';
    }
}

My function is called like this: 我的函数调用如下:

window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
          window.webkitRequestAnimationFrame || 
          window.mozRequestAnimationFrame    || 
          window.oRequestAnimationFrame      || 
          window.msRequestAnimationFrame     || 
          function( callback,  element){
            window.setTimeout(callback, 200 / 100);
          };
})();

function Update(){
    requestAnimFrame( Update ); 
    test();
}

JSFIddle: http://jsfiddle.net/RmXr9/7/ JSFIddle: http//jsfiddle.net/RmXr9/7/

Any suggestions on ways to improve the smoothness of div width incrementing ? 有关如何提高div宽度增加平滑度的建议吗?

try using css transitions. 尝试使用css过渡。 You get way smoother animations, but requires you to structure your code a bit differently. 您可以获得更流畅的动画,但需要您以不同的方式构建代码。 An example of a css transition property is this: css过渡属性的一个示例是:

transition:300ms linear;

then whatever property you change (the width for example) will make a smooth linear 300 millisecond transition towards it. 那么无论你改变什么属性(例如宽度)都会向它平滑线性300毫秒的过渡。

so making a smooth width change is as simple as setting up the transition then doing something like this in javascript: 所以进行平滑的宽度更改就像设置转换一样简单,然后在javascript中执行以下操作:

div.style.width="400px";

Here's a quick example i mocked up: 这是我嘲笑的一个简单例子:

http://jsfiddle.net/nTMsC/1/ http://jsfiddle.net/nTMsC/1/

here's a nice tutorial to get you started: http://www.css3.info/preview/css3-transitions/ 这是一个很好的教程,可以帮助您入门: http//www.css3.info/preview/css3-transitions/

One of the biggest causes of 'juddery' animations for me has always been frame rate. 对我来说,“颤抖”动画的最大原因之一就是帧速率。 If your frame rate is too slow, obviously the animation 'judders'. 如果您的帧速率太慢,显然动画“颤抖”。 But if it is too fast for the browser to handle, the browser gets confused, and you get a different kind of 'juddery'. 但是如果浏览器处理得太快,浏览器就会变得混乱,你会得到一种不同的“颤抖”。

I'd recommend a frame rate of between 13 and 30 milliseconds. 我建议帧速率在13到30毫秒之间。 JQuery is supposed to use 13ms, but I've found that that is sometimes still too fast. JQuery应该使用13ms,但我发现它有时候仍然太快。 I generally start with 16ms, and experiment from there. 我通常从16ms开始,并从那里进行实验。

The key is to ensure that you time it so that one frame starts as or after the previous frame is finished. 关键是要确保你计时,以便在前一帧结束时或之后开始一帧。 This will depend on the code you process. 这取决于您处理的代码。 I notice that you call the next frame before you begin processing the current frame, so it may be possible that you're still getting backed up. 我注意到在开始处理当前帧之前调用了下一帧,因此您可能仍在备份。 Perhaps try: 也许试试:

function Update(){
    test();
    requestAnimFrame( Update ); 
}

Your fallback function has a frame rate of 200 / 100 , which is 2ms. 您的退回功能具有的帧速率200 / 100 ,其为2ms。 It is extremely unlikely that your browser can complete the animation in 2ms, so it is likelyto get backed up. 您的浏览器极不可能在2ms内完成动画,因此可能会备份。 requestAnimationFrame uses a maximum frame rate of 16ms. requestAnimationFrame使用16ms的最大帧速率。

UPDATE : 更新

The problem you're having, according to your jsfiddle, is that, while you're calculating your percentage often, the changes to the percentage are very small, and they don't translate into a change in the width of the div. 根据你的jsfiddle,你所遇到的问题是,当你经常计算你的百分比时,百分比的变化非常小,并且它们不会转化为div宽度的变化。 This http://jsfiddle.net/RmXr9/13/ should demontrate the changes in the percentage, and show the corrsponding changes in actual width. 这个http://jsfiddle.net/RmXr9/13/应该删除百分比的变化,并显示实际宽度的相应变化。 So, although you do a calculation often (maybe 60 times a second), the actual visual change only happens once every 16 frames or so. 因此,虽然您经常进行计算(可能每秒60次),但实际的视觉变化只发生在每16帧左右一次。 So, your actual frame rate is only about 4 frames per second, which makes a 'juddery' animation. 因此,您的实际帧速率仅为每秒4帧左右,这就是“抖动”动画。 Your only options, I'm afraid, are to make the animation run faster (perhaps by decreasing your length variable), or to make the div much longer (or both). 我担心的唯一选择是让动画运行得更快(可能通过减少length变量),或者使div更长(或两者)。

As an aside, I notice you don't have a way to stop the animation at the end, and I've added that into the jsfiddle as well. 顺便说一句,我注意到你没有办法在最后停止动画,我也把它添加到了jsfiddle中。

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

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