【问题标题】:jQuery Animate divs up and down a stackjQuery Animate div 向上和向下堆栈
【发布时间】:2012-11-06 22:19:12
【问题描述】:

我有以下代码来为动态 div 上下设置动画。这个想法是,可能有任意数量的 div 需要在堆栈上上下移动,与上下的 div 交换位置。一旦处于他们的新位置,我需要能够赶上那个新的重新定位的位置。以下代码一次有效,但是一旦较低的 div 向上移动并且较高的 div 向下移动(交换点)到新位置,它们就会停止工作。我该如何设置它,以便他们继续向上遍历堆栈,在他们去的时候交换紧接在上面或下面的那个?一旦完成更新数据库,我还需要知道新职位。我一直在到处寻找,但似乎找不到如何做到这一点。任何帮助将不胜感激。

$('.editUp', this).click(function() {
   thisRowHt = $(this).parents('.rowCont').outerHeight();
   upperRowHt = $(this).parents('.rowCont').prev().outerHeight();
   $(this).parents('.rowCont').animate({'top': '-=' + thisRowHt + 'px'});
   $(this).parents('.rowCont').prev().animate({'top': '+=' + upperRowHt + 'px'});
   return false;
});

$('.editDown', this).click(function() {
   thisRowHt = $(this).parents('.rowCont').outerHeight();
   lowerRowHt = $(this).parents('.rowCont').next().outerHeight();
   $(this).parents('.rowCont').animate({'top': '+=' + lowerRowHt + 'px'});
   $(this).parents('.rowCont').next().animate({'top': '-=' + thisRowHt + 'px'});
   return false;
});

【问题讨论】:

  • 将演示放在 jsfiddle.net 中,以便其他人可以看到您遇到的问题
  • 你试过了吗? jqueryui.com/sortable 真的好用 :)

标签: jquery jquery-animate


【解决方案1】:

您还应该交换 DOM 中的元素,因为当您为 HTML 元素设置动画时,它们只会改变它们在屏幕上的位置。

我已经完成了你的脚本:

$('.editUp', this).click(function() {

    var this_rowCont = $(this).parents('.rowCont');
    var prev_rowCont = $(this).parents('.rowCont').prev();

    // if this is the first element, it returns
    if (prev_rowCont.length != 1){return false;}

    thisRowHt = this_rowCont.outerHeight();
    upperRowHt = prev_rowCont.outerHeight();

    this_rowCont.animate({'top': '-=' + thisRowHt + 'px'});
    prev_rowCont.animate({'top': '+=' + upperRowHt + 'px'}, function(){

        // this is a callback function which is called, when the animation ends
        // This swap this and previous element in the DOM
        this_rowCont.insertBefore(prev_rowCont);
        this_rowCont.css("top", 0);
        prev_rowCont.css("top", 0);
    });

    return false;
});

$('.editDown', this).click(function() {

    var this_rowCont = $(this).parents('.rowCont');
    var next_rowCont = $(this).parents('.rowCont').next();

    // if this is the last element, it returns
    if (next_rowCont.length != 1){return false;}

    thisRowHt = this_rowCont.outerHeight();
    lowerRowHt = next_rowCont.outerHeight();

    this_rowCont.animate({'top': '+=' + lowerRowHt + 'px'});
    next_rowCont.animate({'top': '-=' + thisRowHt + 'px'}, function(){

        // This swap this and next element in the DOM
        next_rowCont.insertBefore(this_rowCont);
        this_rowCont.css("top", 0);
        next_rowCont.css("top", 0);
    });

   return false;
});​

你可以在这里看到它的实际效果:Animate div up and down

【讨论】:

  • 戴夫 - 我非常感谢这个详细的答案。我将在明天早上实施这个,看看我是否可以拨入。谢谢:)
  • 不客气。 :) 如果我的回答对你有帮助,请给我投票支持这个答案。如果它没有帮助并且您有更多问题,请提出。
  • 嘿,David - 工作得很好,但是,出于某种原因,最上面的 var prev_rowCont = $(this).parents('.rowCont').prev(); 正在捕捉上面没有类“rowCont”的标题 div。所以它把它作为最顶层的 div 包含在堆栈中。有什么想法吗?
  • 我知道了,只需将.rowCont 移动到.prev() var prev_rowCont = $(this).parents().prev('.rowCont'); 再次感谢您的帮助。
猜你喜欢
  • 2011-06-06
  • 1970-01-01
  • 2011-12-28
  • 2012-08-27
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2012-05-05
相关资源
最近更新 更多