gpt4 book ai didi

JQuery 'Choppy' 动画 - 简单测试用例

转载 作者:行者123 更新时间:2023-12-03 22:53:14 27 4
gpt4 key购买 nike

这是一个使用绝对定位和 jQuery 为 Div 制作动画的简单测试用例。

<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js'></script>
<script type='text/javascript'>
function slide(){
$('#box').animate({'left': 0},3000);
}
</script>
</head>
<body>
<button onclick="slide()">slide</button>
<div id="box" style="position: absolute; width: 120px; height: 100px; background: #ff0000; left: 500px"></div>
</body>

在 Firefox 4(Mac 上)中,动画“流泪”并且非常断断续续。在 Safari 和 Chrome 中效果更好,但仍然有明显的抖动。

将问题简化为上述测试用例后,我不太确定下一步该做什么。这是 jQuery 的错误吗?我是否缺少绝对定位的某些内容,导致浏览器重绘负载?如果有人可以尝试上面的代码并思考一下,我将非常感激......即使这只是为了让我放心,我不会发疯:)

最佳答案

为什么要使用onclick="slide()"如果你想在单击按钮时滑动然后执行以下操作

CSS

#box {
position: absolute;
width: 120px;
height: 100px;
background: #ff0000;
left: 500px;
}

HTML(分配一些id)

<button id="slide">slide</button>
<div id="box">whetever</div>

jQuery
<script type='text/javascript'>
$(document).ready(function (){
$('button#slide').click(function(){
$('#box').animate({'left': 0},3000);
});
});
</script>

Working DEMO

<小时/>

更新(来自 jQuery1.6 )

Smoother Animations

Additionally jQuery is now using the new requestAnimationFrame method provided by browsers to make our animations even smoother. We can use this functionality to avoid calling timers and instead depend upon the browser to provide the best possible animation experience.

.promise()

就像之前的 $.ajax() 一样,$.animate() 得到“延迟”。当集合上的所有动画完成时,jQuery 对象现在可以返回一个 Promise 来观察:

$(".elements").fadeOut();

$.when( $(".elements") ).done(function( elements ) {
// all elements faded out
});

很乐意提供帮助:)

关于JQuery 'Choppy' 动画 - 简单测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5673252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com