jquery 动画极其缓慢且断断续续
所以我一直在创建一个简单的动画来复制 div 并将其扩展到窗口上。我正在尝试优化它,使其不那么不稳定,并且想知道是否有人可以帮助我。所以我一直在读到,您不能对正在动画的元素进行任何填充或边距设置,因为这会减慢一切。这是我正在使用的动画功能:
$('.content_cell').on('click', function(event) {
var $clonedElement = $( this ).clone(true).attr('class','cloned_object content_cell').appendTo('#mainContentTable');
$clonedElement.css({left:$(this).position().left,
top:$(this).position().top,
opacity:0}) ;
//Position caching for closing animation
selectedPos = $(this).position();
var currPos= $('#invitedToChatCell').position();
//Now animate the cloned element to the correct size
$clonedElement.animate({
height:640, width:700,
//position:'absolute',
left:currPos.left,
top:currPos.top,
opacity:1.0
}, 500, function(){ $('.cloned_object > ul').toggle(); });
event.stopPropagation();
});
内容单元 CSS 和克隆对象 css 看起来像这样,
.content_cell {
border-style: solid;
cursor:pointer;
width:300px;
height:300px;
overflow:auto;
}
.cloned_object{
position:absolute;
background-color:white;
width:300px;
height:300px;
}
有人明白为什么这是一个如此缓慢的动画吗?或者我可以做些什么来加快速度?预先感谢...
更新: 这是 JSFiddle 链接
So I have been creating a simple animation that copies a div, and expands it over the window. I am trying to optimize it so it's less choppy and was wondering if someone could help me out. So I have been reading that you cannot have any padding or margin settings on the element that is being animated, because that will slow everything down. This is the animation function I am using:
$('.content_cell').on('click', function(event) {
var $clonedElement = $( this ).clone(true).attr('class','cloned_object content_cell').appendTo('#mainContentTable');
$clonedElement.css({left:$(this).position().left,
top:$(this).position().top,
opacity:0}) ;
//Position caching for closing animation
selectedPos = $(this).position();
var currPos= $('#invitedToChatCell').position();
//Now animate the cloned element to the correct size
$clonedElement.animate({
height:640, width:700,
//position:'absolute',
left:currPos.left,
top:currPos.top,
opacity:1.0
}, 500, function(){ $('.cloned_object > ul').toggle(); });
event.stopPropagation();
});
The content cell CSS and cloned_object css look like this
.content_cell {
border-style: solid;
cursor:pointer;
width:300px;
height:300px;
overflow:auto;
}
.cloned_object{
position:absolute;
background-color:white;
width:300px;
height:300px;
}
does anyone see why this is such a slow animation? or anything I could do to speed it up? Thanks in advance...
UPDATE:
Here is a JSFiddle link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不仅仅是简单的 JQuery 动画往往会“滞后”。
我尽可能使用 css-animations/css-transforms。
您可以做的是使用 jquery 克隆 div 并使用一些新的 css 类将其放在屏幕上。让 css 处理展开部分。
有关 css 示例和好主意,请参阅: http://daneden.me/animate
刚刚找到了关于此的另一个线程解释CSS部分: Expand分区使用 CSS 从中间开始,而不是从顶部和左侧开始
More than just simple JQuery animations tend to be "laggy".
I use css-animations/css-transforms wherever possible.
What you can do is clone the div with jquery and put it on the screen with some new css classes. Let the css handle the expand-part.
For css examples and good ideas see: http://daneden.me/animate
Just found another thread about this which explains the css part: Expand div from the middle instead of just top and left using CSS