Please take a look at this fiddle: http://jsfiddle.net/dhcyA/
Try clicking on a block. What I want is that when the other elements disapear, the selected block will animate/ease to his giving position instead of just jumping like it does now. Then the same animation repeats itself when clicking again on the box, but then back to place.
Maybe to keep in mind:
I'm using a reponsive design, which means those blocks can be vertical and horizontal after scaling the window.
Any redevisions on the fiddle or suggustions would be great!
Here is my version: http://jsfiddle.net/selbh/dhcyA/92/ (only javascript is changed, and it's responsive)
Here is my solution.
On your existing markup, I added a wrapper division to calculate the position of boxes inside the wrapper. Like this
To maintain the fluidness of the block, I created a function to position the block on the wrapper. Here is the function for position of the blocks:
Finally, the script to toggle the block
Demos
Here is what this solutions gives:
Final Update
Here is a full working solution (pretty straight forward in my opinion) with JS to set the positioning (a simple calculation) and CSS transitions for the rest..
Demo at http://jsfiddle.net/gaby/pYdKB/3/
It maintains the fluidity of
float:left
and works with any number of elements, and you can keep the:nth-child
for the styling, and it will also work if you want to leave more than one element visible..javascript
CSS
note: As @Athari correctly mentioned in the comments, you should include all browser prefixes for the widest support. (my initial answer only included moz / webkit and the standard)
Original Answer
You can not do it directly with your current HTML structure. The floated concept does not support it.
But if you can afford an extra wrapper, then it is no problem..
Just slide the contents of your extra wrapper element..
Put the
float
code on the wrapper element and useDemo at http://jsfiddle.net/gaby/t8GNP/
Update #1
If you need to move the clicked element to the top left and back, then you cannot really do it with CSS.
You will need to manually position them (through JS), set CSS transitions (or jquery), and apply the new positions once you click.
Later on you might want more than one to remain visible and reposition as well..
So you might want to take a look at the great Isotope plugin which can handle this and a multitude of more situations/layouts
I'm kind of sleepy(It's 2:30 AM here) so I leave the half done answer here to give you an idea (I did it in 30 minutes so I guess with 30 minutes more you can get something really nice)
http://jsfiddle.net/LuL2s/2/
The trick comes by the block-holder which make the ease animation and making a difference between when they appear and disappear
JS
HTML
CSS
Great Challenge!
New Version:
Here is a much better version as it makes the blocks stay in their rows. I added a
css
function so that yournth-child
styles could be applied even in the rows. Even maintains same HTML Structure.Demo: http://jsfiddle.net/MadLittleMods/fDDZB/23/
The jQuery for this new revision looks like:
I added a
makeRows
anddeleteRows
functions so that the blocks would stay in their rows instead of getting smaller and moving into the row above. I calldeleteRows
whenever the window resizes so that it can maintain a responsive layout. Then if the blocks need to be toggled, I recreate the rows.css
andcss2json
functions are courtesy of marknadalOld version:
I came up with a solution with
.animate
so that it could ease horizontally.Here is a demo: http://jsfiddle.net/MadLittleMods/fDDZB/8/
The jQuery looks like:
The key was to separate the blocks that were toggled with
.slideToggle
and.animate
because you have to apply the same when they show and hide.