【问题标题】:An effect with jquery animate function and absolute position带有jquery animate函数和绝对位置的效果
【发布时间】:2016-02-28 18:09:09
【问题描述】:

假设我有一个绝对位置 css {bottom:0px;} 的 div,那么我希望它折叠,我使用

$('#id').animate({ height: "0" }, { duration: 1000 });

显然它是从上往下塌陷的,也就是说底部固定,顶部向下。

接下来我希望它扩展顶部固定,底部移动,所以我写:

$('#id2').animate({ height: "0" }, { duration: 1000 }).queue(function () {

    $('#id2').css({ top: '0' }).animate({ height: "50" }, { duration: 1000 });
});

但它没有消耗,所以我的代码有什么问题

谢谢

这是我的在线示例: http://jsfiddle.net/hh54188/pngK4/

【问题讨论】:

  • 我相信你会喜欢阅读this

标签: jquery jquery-animate absolute


【解决方案1】:

因为你把动画放在了一个队列中你需要使用dequeue

$('#id2').dequeue().css({ top: '0' }).animate({ height: "50" }, { duration: 1000 });   

使用出队修复 JSFiddle

但确实没有理由使用队列,这样更好:

    function x() {
    $('#id2').css({
        top: '0'
    }).animate({
        height: "50"
    }, {
        duration: 1000
    });
}

$(function() {
    $('#id2').animate({
        height: "0"
    }, {
        duration: 1000,
        complete: x
    });    
});

当动画结束时,调用一个callback 函数来显示它。不涉及队列。

JSffidle 没有queue

【讨论】:

  • 谢谢你解决了我的问题。但我还是不太明白为什么我需要“出队”,它如何影响行政命令?
  • @hh54188。在队列中读取 JQuery docs“请注意,当使用 .queue() 添加函数时,我们应确保最终调用 .dequeue() 以便执行行中的下一个函数。”
猜你喜欢
  • 2012-10-20
  • 1970-01-01
  • 2012-05-30
  • 2012-05-02
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 2010-10-03
  • 2012-06-09
相关资源
最近更新 更多