The following seems to work:
$('#left-bg, #right-bg').click( function(){ $(this).animate({'width': '100%'},600).siblings().animate({'width':'0'},600); });
Although I'm not sure how you plan to return the βotherβ div .
JS Fiddle demo .
Edited to add a button (via jQuery), which allows you to return as a
div to its original size:
$('#left-bg, #right-bg').click( function(){ $(this).animate({'width': '100%'},600).siblings().animate({'width':'0'},600); $('<button class="show">Show all</button>') .appendTo('#wrapper'); }); $('.show').live('click', function(){ $('#left-bg').animate( { 'width': '50%' },600); $('#right-bg').animate( { 'width': '50%' },600); $(this).remove(); });
Updated JS script .
Edited to answer the question posted by OP in the comments:
Is there a way to redirect the page after the animation is complete?
Yes, just add the line window.location.href = "http://path.to.url.com/";
$('#left-bg, #right-bg').click( function(){ $(this).animate({'width': '100%'},600).siblings().animate({'width':'0'},600); $('<button class="show">Show all</button>') .appendTo('#wrapper'); window.location.href = "http://www.google.com/" // <-- this line redirects. }); $('.show').live('click', function(){ $('#left-bg').animate( { 'width': '50%' },600); $('#right-bg').animate( { 'width': '50%' },600); $(this).remove(); });
Updated JS script .
Edited in response to the error report (in the comments):
Another mistake (a simple fix) is that every time you click on any of the divs a new button is created. So say that you clicked on the left half, and it expanded, filled the page, etc., and then you clicked on it again (it is now somewhere on the page). He will try to add a second button.
To prevent the second button from being added to the div , just add an if :
$('#left-bg, #right-bg').click( function(){ if (!$('.show').length) { $(this).animate({'width': '100%'},600).siblings().animate({'width':'0'},600); $('<button class="show">Show all</button>') .appendTo('#wrapper'); window.location.href = "http://www.google.com/"
That will add only button or even animate div s if the $('.show') selector returns no .
However, if you are also redirected to another page by clicking the button, this should still not be a problem, since none of the jQuery on the original page will go out / have access to the page to which the user is redirected (if this is not a page in your own domain, and you obviously decided to add the same button ).