How to animate elements move with css3 transitions after hiding some element

余生颓废 提交于 2019-12-07 05:59:19

问题


Is it possible to animate elements move with css3 transitions after hiding some element using jQuery's .fadeOut()?

I found some kind of solution here (see "It works for grids, too" section):

However my case is more like this: http://jsfiddle.net/CUzNx/5/

<div class="container">
    <div id="item1" class="item">Test1</div>
    <div id="item2" class="item">Test2</div>
    <div id="item3" class="item">Test3</div>
    <div id="item4" class="item">Test4</div>
    <div id="item5" class="item">Test5</div>
    <div id="item6" class="item">Test6</div>
    <div id="item7" class="item">Test7</div>
</div>

<div style="clear:both">
<button onclick="$('#item1').fadeOut();">
   Hide first
</button>
<button onclick="$('#item1').fadeIn();">
   Show first
</button>
</div>

I have floating div elements and after some element is hidden, it would be nice if other elements were animated. Is it possible?


回答1:


You could do something like this which uses a CSS class toggled by a little JavaScript and CSS transitions to do what you're looking for instead of using jQuery.

// The relevant CSS
.item {
    // Your original code here
    -webkit-transition: all .5s ease-in-out;
    -moz-transition: all .5s ease-in-out;
    -o-transition: all .5s ease-in-out;
    transition: all .5s ease-in-out;
}
.hide {
    width: 0;
    height: 0;
    opacity: 0;
    margin: 0;
    padding: 0;
}

// The JavaScript
var items = document.getElementsByClassName('item');
for(var i = 0; i < items.length; i ++) {
    items[i].onclick = function() {
        this.classList.toggle('hide');
    }
};

function addBack() {
    for(var i = 0; i < items.length; i ++) {
        items[i].classList.remove('hide');
    }
}

I also took out your buttons and added an "Add all elements back" button:

<button onclick='addBack()'>Add all elements back</button>

If you're already using jQuery somewhere else in your project I also made this jQuery version. Here is the JavaScript for that:

function addBack() {
    $('.item').each(function() {
        $(this).removeClass('hide');
    });
}

$('.item').on('click', function() {
    $(this).toggleClass('hide');
});

Also, you don't need IDs for any of this, so they can be taken out if you so please.




回答2:


One approach would be to replace the div to be removed with a placeholder, then animate both the placeholder and the original.

Using this snippet: Positioning an element over another element with jQuery

Your code can be changed to something like this (I used click on the item to trigger the removal here):

Demo

CSS

div { box-sizing: border-box; }

.item, .placeholder {
    float: left;
    width: 100px;
    height: 100px;
    border: 1px solid black;
    margin: 5px;
    position: relative;
    background: white;
    -webkit-transition: all 1s ease;
}

.placeholder {
    opacity: 0;
    border: 0px;
}

.item.hide {
    border: 1px solid rgba(0,0,0,0);
    margin: 0px;
    top: 500px;
    opacity: 0;
    overflow: hidden;
    z-index: -1;
}

.placeholder.hide {
    width: 0;
    height: 0;
    margin: 0;
    border: 0;
}

Script

$('.item').on('click', function(evt) {
    var $this = $(this);
    var $new  = $($this.clone());

    $new.addClass('placeholder');
    $this.replaceWith($new);

    $this.positionOn($new);
    $this.appendTo($('body'));

    setTimeout(function() {
        $this.css({top: '', left: ''});
        $this.addClass('hide');
        $new.addClass('hide');  
    }, 0);
});

Snippet from: Positioning an element over another element with jQuery

// Reference: http://snippets.aktagon.com/snippets/310-positioning-an-element-over-another-element-with-jquery
$.fn.positionOn = function(element, align) {
  return this.each(function() {
    var target   = $(this);
    var position = element.position();

    var x      = position.left; 
    var y      = position.top;

    if(align == 'right') {
      x -= (target.outerWidth() - element.outerWidth());
    } else if(align == 'center') {
      x -= target.outerWidth() / 2 - element.outerWidth() / 2;
    }

    target.css({
      position: 'absolute',
      zIndex:   5000,
      top:      y, 
      left:     x
    });
  });
};

Now your old div animates out of the way while the grid collapses.

If you are willing to add another library to your code, take a look at Masonry/Isotope by Metafizzy. They were designed to do just this sort of thing.




回答3:


You dont need CSS-transitions. Just use .hide() instead of .fadeout()

<button onclick="$('#item1').hide(400);">
    Hide first
</button>

See here: https://jsfiddle.net/st1o8ngp/



来源:https://stackoverflow.com/questions/18364938/how-to-animate-elements-move-with-css3-transitions-after-hiding-some-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!