frenchie
frenchie

Reputation: 51937

smooth div repositioning with CSS transitions

I have 3 divs. The first 2 are on top of the third one.

The divs on top are of different heights and only one is visible at a time: they toggle their visibility. Because they're of different height, when these two divs toggle, they cause a shift in the top position of the third div that's underneath. I was wondering if there's a way to use CSS transition to make the movement of the third div smooth.

This is the HTML:

<div id="Toggle">click here</div>

<div id="InnerDiv1"></div>
<div id="InnerDiv2"></div>

​<div id="BottomDiv"></div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

This is the CSS:

#InnerDiv1{
    height:30px;
    width:200px;
    background:red;
    margin:10px 10px;   
    clear:both;}

#InnerDiv2{
    height:60px;
    width:200px;
    background:blue;
    margin:10px 10px;
    clear:both;    
    display:none;}

#BottomDiv{
    height:60px;
    width:200px;
    background:yellow;
    margin:10px 10px;
    transition:all 2s ease;}

And some quick jquery for the toggle:

$('#Toggle').click(function() {
    $('#InnerDiv1').toggle();
    $('#InnerDiv2').toggle();
});​

Here is the jsfiddle for clarity.

The goal is to make the yellow div move smoothly. I can do this with jquery but I was wondering if there's a way to make it work just with CSS.

Thanks for your suggestions.

Upvotes: 4

Views: 6233

Answers (2)

Casey
Casey

Reputation: 11

This will get it to work when you hover over innerDv1. Hope this helps.

#InnerDiv1{
   height:30px; 
    height:30px;
    width:200px;
    background:red;
    margin:10px 10px;   
    clear:both;
-webkit-transition:width 2s, height 2s, background-color 2s, -webkit-transform 2s;
    transition:width 2s, height 2s, background-color 2s, transform 2s;}
#InnerDiv1:hover {
   height:60px; 
    height:60px;
    background:blue;
}

Upvotes: 1

Chris
Chris

Reputation: 404

Well, I could give a better example if I knew the purpose of this code. So I will just guess.

http://jsfiddle.net/35USv/1/

this is sort of a poorly set up toggle. There is however good stuff. You can use -webkit-transition or -moz or -o for the animation. you also now only have to think about one element and can just change the content of that div.

Upvotes: 2

Related Questions