jQuery:淡出然后滑动

发布于 2024-07-16 15:02:21 字数 183 浏览 8 评论 0原文

如果要删除某个项目,那么我想将其淡出并向上滑动其他元素以填充空白空间。 现在,当我使用 fadeOut() 时,该项目末尾没有高度,这会导致其他项目向上跳跃(而不是很好地向上滑动)。

如何在 fadeOut() 之后使用 slideUp() 和元素?

If an item is being deleted then I would like to fade it out and slide the other elements up to fill the empty space. Now, when I use fadeOut() the item doesn't have a height at the end which results in the other items jumping up (instead of sliding up nicely).

How can I slideUp() and element right after fadeOut()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

很酷不放纵 2024-07-23 15:02:21

听起来最好使用 jQuery fadeTo 命令

 $(function() {

     $("#myButton").click(function() {
         $("#myDiv").fadeTo("slow", 0.00, function(){ //fade
             $(this).slideUp("slow", function() { //slide up
                 $(this).remove(); //then remove from the DOM
             });
         });

     });

});

工作演示在这里

通过在前一个命令的回调函数中执行每个命令,该命令将在前一个命令完成之前不会运行; 当元素从 DOM 中移除而没有等待 SlideUp 完成时,就会发生“跳转”。

Sounds like it would be better to use the jQuery fadeTo command

 $(function() {

     $("#myButton").click(function() {
         $("#myDiv").fadeTo("slow", 0.00, function(){ //fade
             $(this).slideUp("slow", function() { //slide up
                 $(this).remove(); //then remove from the DOM
             });
         });

     });

});

Working Demo here.

By performing each command in the callback function of the preceding command, the command will not run until the previous one completes; a "jump" occurs when the element is removed from the DOM without waiting for the slideUp to complete.

余生一个溪 2024-07-23 15:02:21
jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) {
  if (this.is(":hidden")) {
    return this.slideDown(speed, easing).fadeTo(speed, 1, easing, callback);
  } else {
    return this.fadeTo(speed, 0, easing).slideUp(speed, easing, callback);
  }
};

我在 JQuery 1.3.2 上测试了它,它确实有效。

编辑:这是我调用它的代码。 #slide-then-fade 是按钮元素的ID,article-text 是div 标签上的类:

$(document).ready(function() {
  $('#slide-then-fade').click(function() {
    $('.article-text').fadeThenSlideToggle();
  });
});

编辑2:修改为使用内置的slideUp。

编辑3:重写为切换,并使用fadeTo

jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) {
  if (this.is(":hidden")) {
    return this.slideDown(speed, easing).fadeTo(speed, 1, easing, callback);
  } else {
    return this.fadeTo(speed, 0, easing).slideUp(speed, easing, callback);
  }
};

I tested it on JQuery 1.3.2, and it does work.

Edit: This is the code I called it from. #slide-then-fade is the ID of a button element, article-text is a class on a div tag:

$(document).ready(function() {
  $('#slide-then-fade').click(function() {
    $('.article-text').fadeThenSlideToggle();
  });
});

Edit 2: Modified to use the built-in slideUp.

Edit 3: Rewritten as a toggle, and using fadeTo

°如果伤别离去 2024-07-23 15:02:21

你不能链它吗?

$('myelement').fadeOut().slideUp();

编辑

也许会有所帮助?

Can't you chain it?

$('myelement').fadeOut().slideUp();

EDIT:

Maybe this will help instead?

云胡 2024-07-23 15:02:21
$("#id").fadeIn(500, function () {

    $("#id2").slideUp(500).delay(800).fadeOut(400);

});
$("#id").fadeIn(500, function () {

    $("#id2").slideUp(500).delay(800).fadeOut(400);

});
英雄似剑 2024-07-23 15:02:21

尝试 $('.row').animate({ height: 'toggle', opacity: 'toggle' }, 'slow').slideUp();

演示在这里

Try $('.row').animate({ height: 'toggle', opacity: 'toggle' }, 'slow').slideUp();

demo Here

红焚 2024-07-23 15:02:21

fadeOut 函数采用回调函数的第二个可选参数,因此您应该能够执行以下操作:

$('elementAbove').fadeOut(500, function() {
    $('elementBelow').slideUp();
});

编辑:忘记添加 fadeOut 的速度作为第一个参数

The fadeOut function takes a second optional argument of a callback function, so you should be able to do something like this:

$('elementAbove').fadeOut(500, function() {
    $('elementBelow').slideUp();
});

EDIT: forgot to add the speed of the fadeOut as the first parameter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文