-1

I need to animate a relative-positioned div, where the animation goes from display:none; to display:block;. I need to "scale" it, starting from the bottom-right corner towards the top-left corner. Are there any ways of doing this? (Preferably in jQuery, but anything that works is good)

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
  • 4
    If I understand your intention correctly, yes it will be possible... what have you tried so far to achieve this particular effect? – sg3s Mar 06 '13 at 15:14
  • I have tried animating the width and heigth of the element, but this causes the animation to start in the top left corner instead of the bottom right. I have varius variations of the slideLeft and slideUp functions, using several divs and animating each one at the same time, but it just won't work the way I want it to. – user2140478 Mar 06 '13 at 15:41

2 Answers2

1

I am not sure that you want to fade in or change the size of the element. It sounds like you want need to bring it out of hiding.

Since HTML layout is all done from the top and the left, getting something to animate from the bottom right to the top left is tricky to say the least.

By carefully combining slideUp and slideDown with position fixed or absolute, you can get an element to show from the bottom up. But I have not heard of a way to slide right to left without using some very advanced jQuery with bleeding edge includes.

So I offer this solution: Since the div you are trying to show is positioned relative you probably have some kind of wrapper, or an easily place one.

With a wrapper in place you can set it's overflow rules to hidden and then place the 'hiding' div out side of its bounds.

Now with a simple jQuery animate you can bring 'show' it by re-positioning it inside the bounds of the wrapper

Here is an example:

HTML:

<div class='wrapper'>
    <div class='growIt'>Text for div</div>
</div>
<input type='button' id='showHide' value='Show / Hide'/>

CSS:

.wrapper{
    background:blue;
    position:relative;
    width:200px;
    height:200px;
    overflow:hidden;
}
.growIt{
    background: red;
    position:relative;
    width:200px;
    height:200px;
    top:200px;
    left:200px;
    display:block;
}

jQuery:

$(function(){
    var hiding = true;
    $('#showHide').click(function(){
        if(hiding == true){
            hiding = false;
            $('.growIt').animate({top:'0px',left:'0px'},2000);    
        }else{
            hiding = true;
            $('.growIt').animate({top:'200px',left:'200px'},2000);    
        }
    });
});

When you click the button it will either hide or show the red div.

If your requirement of animating it from display:none to display:block is because you need it to push other form elements out of the way when it comes into view, I would suggest adding another wrapper with position:relative and then making the wrapper slide 'down' from the bottom.

Community
  • 1
  • 1
Pow-Ian
  • 3,607
  • 1
  • 22
  • 31
  • Your solution is sound, but I moved the 'hiding' var inside the onload function as to not pollute the global scope in javascript. – sg3s Mar 06 '13 at 17:10
  • I won't lie, I still have trouble knowing when I am making a 'closure'. Thanks for the edit. – Pow-Ian Mar 06 '13 at 17:14
  • 1
    This is technically not a closure, [read this question](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work). You are however defining a new, unique scope in which the variable will be unique given you've defined it using a var statement. The variable will thus be available in this scope and any sub-scopes (the on click function you pass to animate in this case). [Here is a nice question explaining scopes](http://stackoverflow.com/questions/500431/javascript-variable-scope). – sg3s Mar 06 '13 at 17:20
  • In practice you could call it a closure, but something that kind of makes it a closure is that it is a function dedicated solely to closing-over variables by making a new scope, typically they are immediately invoked. Now for this case that would be extra overhead while you already have a scope that will be unique because you make a function to pass to the jQuery object which in turn will be invoked on load, close enough right? – sg3s Mar 06 '13 at 17:32
  • clear as muddy water. Thanks for trying. So far I have not been bitten by memory leaks or quirky assignments, but I am sure there will come a day. Basically closures and advanced scoping fall under techno voodoo for me and I have yet to come across a situation where I was forced to use some kind of advanced scoping to make a solution work, or make sense of something already working. – Pow-Ian Mar 06 '13 at 17:45
  • Thanks alot! Seems from like this does what I was looking for. Will try when I get home. – user2140478 Mar 06 '13 at 19:34
0

Is this what you are looking for?

http://jqueryui.com/resizable/

http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_eff_animate

http://jqueryui.com/effect/

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Parag
  • 4,754
  • 9
  • 33
  • 50
  • Those links are not what I'm looking for, no. They won't allow me to animate the element in such a manner as I described. – user2140478 Mar 06 '13 at 15:43
  • Don't try to 'guess' an answer, that is not the way to get clarification. w3schools is also a terrible source to link to, preferably build a demo yourself using jsfiddle.. – sg3s Mar 06 '13 at 17:07