Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery animate width to the right

I want to animate div to slide to the right like the example below:

http://jsfiddle.net/56hxy/3/

but instead of shifting the margin, I want to "squish" the width but it's not working by changing the width % ( width: '60%' ) it squishes to the left instead..

like image 564
bushdiver Avatar asked Jan 12 '13 21:01

bushdiver


People also ask

How do you animate in jQuery?

jQuery Animations - The animate() Method. The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated. The optional speed parameter specifies the duration of the effect.

Is there a way to animate the width of a page?

Or give right:0 to #content in the CSS, then you can animate the width and it will shrink from left to right without requiring margins. Same effect as the above but cleaner. Fiddle Also, jQuery has a built-in " toggle event " for alternating clicks so, if you're toggling just the click event you can use:

How to increase the width of an element using jQuery?

Remember: The code { width: ‘+=250px’ } tells to increase the element’s width by 250px. If you write multiple .animate () on an element then they will get queue up and will execute one by one.

How to create an animation effect with jQuery timer?

This will create an animation effect when the div will move to the left side. You can quite easily use the .animate () method on the jQuery Timer example which I created in one of my previous tutorial. We can use any number of CSS properties with the .animate () method.


1 Answers

Not sure if this is exactly what you're looking for but try:

$('#content').animate({
    marginLeft: '40%',
    width:'60%'
});

Fiddle


Or give right:0 to #content in the CSS, then you can animate the width and it will shrink from left to right without requiring margins. Same effect as the above but cleaner. Fiddle

Also, jQuery has a built-in "toggle event" for alternating clicks so, if you're toggling just the click event you can use:

$('#content').toggle(function() { 
    $(this).animate({ width:'60%' }); 
}, function() {
    $(this).animate({ width:'100%' }); 
});

Fiddle

like image 123
Fabrício Matté Avatar answered Oct 13 '22 07:10

Fabrício Matté