在小间隔内通过javascript更新css时,会出现断断续续的动态观察动画
Stuttering orbit animation when updating css through javascript over small interval
我正试图让一些DOM元素围绕一个固定点平滑旋转。我是用jQuery从头开始写这篇文章的,无论我为setInterval选择什么更新速度,或者我在每个循环中轨道前进的度数有多小,我都会得到这种刺耳的楼梯动画效果。我试过用jquery的.animate代替.css,希望它能让事情顺利进行,但我似乎无法让它发挥作用。感谢您的帮助。
换句话说,它不像在HTML5画布中旋转图像那样平滑。我想让它更光滑。
这里有一个jsFiddle演示这个问题。
注意到动画不太平滑吗?
供参考,这里是代码:
HTML
<div id="div"></div>
<div class="dot"></div>
<button class="stop">STOP</button>
<button class="start">START</button>
CSS
#div{
position:absolute;
height: 20px;
width: 20px;
background-color: #000;
}
.dot{
position:absolute;
width: 5px;
height: 5px;
background-color: #000;
}
button{
position:absolute;
}
.stop{
top:200px;
}
.start{
top:225px;
}
所有重要的Java脚本
$(document).ready(function(){
$('#div').data('angle', 90);
var interval;
$('.stop').on('click', function(){
if(interval){
clearInterval(interval);
interval = undefined;
}
});
$('.start').on('click', function(){
if(!interval){
interval = setBoxInterval();
}
});
interval = setBoxInterval();
});
function drawOrbitingBox(degrees){
var centerX = 100,
centerY = 100,
div = $('#div'),
orbitRadius = 50;
//dot might not be perfectly centered
$('.dot').css({left:centerX, top:centerY});
//given degrees (in degrees, not radians), return the next x and y coords
function coords(degrees){
return {left:centerX + (orbitRadius * Math.cos((degrees*Math.PI)/180)),
top :centerY - (orbitRadius * Math.sin((degrees*Math.PI)/180))};
}
//increment the angle of the object and return new coords through coords()
function addDegrees(jqObj, degreeIncrement){
var newAngle = jqObj.data('angle') + degreeIncrement;
jqObj.data('angle', newAngle);
return coords(newAngle);
}
//change the left and top css property to simulate movement
// I've tried changing this to .animate() and using the difference
// between current and last position to no avail
div.css(addDegrees(div, degrees), 1);
}
function setBoxInterval(){
var interval = window.setInterval(function(){
drawOrbitingBox(-0.2); //This is the degree increment
}, 10); //This is the amount of time it takes to increment position by the degree increment
return interval;
}
我宁愿不使用外部库/插件,但如果这是公认的做这类事情的方式,我会的。谢谢你抽出时间。
这是因为您为顶部和左侧属性设置的值是四舍五入的。您应该尝试使用CSS转换。结合CSS动画/转换和CSS转换,您也应该能够在没有JavaScript的情况下获得动画。
哦,我自己也碰到过!实际上你无能为力,你看到的口吃是像素大小。像素是基于css的动画的最小步骤,不能做"半像素"或"0.2像素"。您将看到css3动画中也会出现同样的情况。
恐怕唯一的解决办法就是加快你的动画制作速度。
此外,cosndsider使用rquestAnimationFrame而不是interval:https://developer.mozilla.org/en-US/docs/Web/API/window.requestAnimationFrame
相关文章:
- 如何设置html元素填充的动画
- 如何使用动画实现纸张推车
- 如何在生成下载文件时显示加载动画
- 剑道网格jQuery动画()问题
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- jQuery Lazy加载动画滚动
- jquery动画可以通过编程链接吗
- Javascript将数学动画化
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 动画.CSS重播
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 如何使用jQuery在动画中期加速动画
- css动画和jquery.load()在移动设备上断断续续
- Firefox中的自述卷展动画断断续续
- 在小间隔内通过javascript更新css时,会出现断断续续的动态观察动画
- 为什么 Javascript 动画离线流畅,但在线放置时却断断续续?(AWS)
- 为什么动画长时间抖动/断断续续
- 使用setTimeout的交错动画是断断续续的
- 快速扫描,动画断断续续
- HTML画布动画断断续续