简体   繁体   English

CSS3过渡问题

[英]CSS3 transition issue

I am removing a bunch of div elements using jquery. 我正在使用jquery删除一堆div元素。 What I want is when The div is removed then the rest of the div elements should animate to the top. 我想要的是将div移除后,其余div元素应设置为顶部的动画。 My current code just removes it and the rest of the div elements shift immediately. 我当前的代码只是将其删除,其余div元素会立即移动。 How can I achieve this? 我该如何实现?

Below is my code 下面是我的代码

HTML 的HTML

<div class="div"><a href="#" class="remove">Remove</a></div>
<div class="div"><a href="#" class="remove">Remove</a></div>
<div class="div"><a href="#" class="remove">Remove</a></div>
<div class="div"><a href="#" class="remove">Remove</a></div>

JS JS

$('.remove').on('click', function () {
    $(this).parent().remove();
});

CSS 的CSS

.div {
    padding: 20px;
    margin-bottom: 20px;
    background: #ccc;
     -webkit-transition: all 2s; /* For Safari 3.1 to 6.0 */
    transition: all 2s;
}

JSFIDDLE JSFIDDLE

this will do the trik 这会很有趣

$('.remove').on('click', function () {
  $(this).parent().slideUp("slow", function(){
    $(this).remove();
  });
});

Here is a solution for your issue: 这是您的问题的解决方案:

http://jsfiddle.net/YwWn5/3/ http://jsfiddle.net/YwWn5/3/

Updated JS: 更新的JS:

$('.remove').on('click', function () {
    var $elem = $(this).parent();
    $elem.addClass('removing');
    setTimeout(function () {
        $elem.remove();
    }, 500);
});

Updated CSS: 更新的CSS:

.div {
    padding: 20px;
    margin-bottom: 20px;
    background: #ccc;
    transition: width 2s, height 0.5s, opacity 0.5s, margin 0.5s, padding 0.5s;
}
.removing {
    height: 0;
    opacity: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-top: 0;
    margin-bottom: 0;
}

I included margin and padding animation to prevent any jumps on element removal. 我包括了边距和填充动画,以防止元素删除发生任何跳跃。 It's also important to note that I use a setTimeout to wait for the CSS animation to finish before actually removing the element. 还需要注意的是,在实际删除元素之前,我使用setTimeout等待CSS动画完成。 If you're not concerned with the elements being removed from the DOM, and are okay with them remaining invisible on the page, you can remove that part altogether. 如果您不关心要从DOM中删除的元素,并且可以让它们在页面上保持不可见,则可以将其完全删除。

You don't need CSS transition to do that, try : 您不需要CSS转换即可尝试:

$('.remove').on('click', function () {
   $(this).parent().hide(300, function(){
       $(this).remove();
   });
});

Fiddle: http://jsfiddle.net/YwWn5/4/ 小提琴: http//jsfiddle.net/YwWn5/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM