Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transition to smooth transition of elements after another element is hidden.

Issue: Can I use a CSS transition to control how a div moves into vacated space created by another div being hidden.

I have elements in my app that appear and disappear (display: none) based on user set intervals. As expected with display:none, sometimes an adjacent element will move to occupy the space vacated by .

I don't mind that moves in this case. I would like to control how it moves. What I would like is for to move via a css transition or other method and not just jerk into place. Is this possible?

Here's a JS fiddle that illustrates the scenario via button click. When #red is hidden I would like to see #blue move into position more gradually. Thx.

http://jsfiddle.net/uV6W7/1/

Here's the code in the jsfiddle:

HTML

<div id="red"></div>
<div id="blue"></div>
<p>
<button id="remove" type="button">Remove Red</button>

CSS

#red {
    display: inline-block;
    background-color: red;
    height: 200px;
    width: 200px;
}
#blue {
    display: inline-block;
    background-color: blue;
    height: 200px;
    width: 200px;
}

Javascript

$("#remove").on("click", function(event){
    if($('#remove').text()=="Remove Red") {
      $('#red').fadeOut();
      $('#remove').html("Restore Red");
    }else {
      $('#red').fadeIn();
      $('#remove').html("Remove Red");
    }
});
like image 680
Joel Avatar asked Jul 03 '13 18:07

Joel


People also ask

Can you transition visibility?

Visibility is an animatable property according to the spec, but transitions on visibility do not work gradually, as one might expect. Instead transitions on visibility delay hiding an element. On the other hand making an element visible works immediately.

Can you have multiple transitions CSS?

Transitioning two or more propertiesYou can transition two (or more) CSS properties by separating them with a comma in your transition or transition-property property. You can do the same with duration, timing-functions and delays as well. If the values are the same, you only need to specify one of them.

How do you trigger transitions in CSS?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).

Can you add transition to display none?

When we want to use transition for display:none to display:block, transition properties do not work. The reason for this is, display:none property is used for removing block and display:block property is used for displaying block. A block cannot be partly displayed.


1 Answers

You could hide red by sliding it to a 0 width state. This would cause the right element to seem to slide into the place of the left.

$('#red').animate({width:'toggle'}); Can be used to slide it to the left.

You can also wrap #red in another div and then fade #red and afterwards slide the container to combine the visual effects.

like image 92
EWit Avatar answered Oct 13 '22 09:10

EWit