javascript - jQuery - 让 .animate 和 .html 更好地发挥作用

标签 javascript jquery html internet-explorer-8

我试图不嵌套这些语句:

$('#capePowerpoint').animate({"right": "+=498px"}, 1000);
$('#capePowerPointContainer').html(capTabArray[capTabCounter][capabilitiesCounter]);
$('#capePowerpoint').animate({"right": "-=996px"}, 0);
$('#capePowerpoint').animate({"right": "+=498px"}, 1000);

如果我注释掉 .html 行或 .animate 行,它们会正常工作,但是当我尝试像这样组合它们时,它们只会删除 div 中的所有内容。不要将 div 滚动到屏幕之外,而是替换它,然后从另一侧滚动回来。

最佳答案

您需要在 animate 调用中声明回调方法!

$('#capePowerpoint').animate({"right": "+=498px"}, {duration:1000,complete:function(){
    $('#capePowerPointContainer').html(capTabArray[capTabCounter][capabilitiesCounter]);
    $('#capePowerpoint').animate({"right": "-=996px"}, {duration:0,complete:function(){
        $('#capePowerpoint').animate({"right": "+=498px"}, 1000);
    });
});

查看更多:http://api.jquery.com/animate/

如果您太喜欢 jQuery animate 方法,就像我一样(我讨厌它),请参阅 GreenSock 的 TweenLite 库:

http://www.greensock.com/v12/

编辑:您应该保存元素而不是到处查询

var cape = $('#capePowerpoint'), container = $('#capePowerPointContainer');
cape.animate({"right": "+=498px"}, {duration:1000,complete:function(){
    container.html(capTabArray[capTabCounter][capabilitiesCounter]);
    cape.animate({"right": "-=996px"}, {duration:0,complete:function(){
        cape.animate({"right": "+=498px"}, 1000);
    });
});

关于javascript - jQuery - 让 .animate 和 .html 更好地发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642859/

相关文章:

javascript - Jquery 占位符插件将密码显示为文本

javascript - Sprite 缩放导致 Three.js 中的定位和旋转不正确

jquery - 如何指定无限(AJAX 加载)轮播的宽度

html - ng-class 如果浏览器不是 Internet Explorer

javascript - 更改新的日期格式

javascript - 在浏览器窗口周围移动图像并在引人注目的侧壁上弹回

javascript - pagify.js 和重新加载/刷新 div

javascript - 如何在不同的div中附加循环值

带有条件的 Javascript 意外标记 {

固定大小的 HTML CSS 适合图像