Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to animate a centered div to fill the screen while its still being centered?

Tags:

jquery

css

I'm trying to animate a div to fill the screen and then return to its original size again. However, when i make the div fill the screen it doesn't animate from the centre, it starts from the top right. Furthermore, when I minimize it, it doesn't return to the starting position but to the top left.

I have the jsfiddle code here. I'm trying to get it to animate smoothly from its starting point at the centre, fill the screen and then backwards to its starting position like in the begining.

like image 246
Zeeno Avatar asked Aug 23 '11 10:08

Zeeno


3 Answers

I suggest that your moving div has always position: relative;, and contained in a div with position: absolute;, and that he is centered this way

Here is a working example of what you want: http://jsfiddle.net/FP2DZ/.

like image 55
Benjamin Crouzier Avatar answered Nov 04 '22 14:11

Benjamin Crouzier


Another method without the extra div used by pinouchon. jsFiddle is here:

<html>
    <head>
        <script type="text/javascript">
            var isFullscreen = false;

            function fullscreen(){      
                var d = {};
                var speed = 900;
                if(!isFullscreen){ // MAXIMIZATION
                    d.width = "100%";
                    d.height = "100%"; 
                    isFullscreen = true;
                    $("h1").slideUp(speed);
                }
                else{ // MINIMIZATION            
                    d.width = "300px";
                    d.height = "100px";            
                    isFullscreen = false;
                    $("h1").slideDown(speed);
                }
                $("#controls").animate(d,speed);            
            }

        </script> 
        <style> 
            #controls{
                width:300px;
                height:100px;
                margin: auto auto auto auto;
            }   
            body, html{
                height:100%;
            }

        </style> 

    </head>
    <body>
        <h1 align=center> Header (To be covered on Fullscreen) </h1>
        <div id='controls' style="background-color:green;" align="center">
            <input type='button' value='fullscreen' onclick='fullscreen();' />
        </div>
    </body>
</html>
like image 3
Patrick Avatar answered Nov 04 '22 16:11

Patrick


I think this is what you want:

<html>
    <head>
        <script type="text/javascript">
            var isFullscreen = false;

            function fullscreen(){

                var d = {};
                var speed = 900;
                if(!isFullscreen){ // MAXIMIZATION

                    d.width = "100%";
                    d.height="100%";                    
                    d.left="0%";
                    d.top="0%";
                    d.marginLeft="0px";

                    $("#controls").animate(d,speed);
                    isFullscreen = true;

                }else{ // MINIMIZATION

                    d.width="300px";
                    d.height="100px";
                    d.left="50%";
                    d.top="50%";
                    d.marginLeft="-150px";


                    $("#controls").animate(d,speed);
                    isFullscreen = false;
                }

            }

        </script> 
        <style>
            #controls{
                width:300px;
                height:100px;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-left: -150px;
            }
        </style>
    </head>
    <body>
        <h1 align=center> Header (To be covered on Fullscreen) </h1>
        <div id='controls' style="background-color:green;" align="center">
            <input type='button' value='fullscreen' onclick='fullscreen();' />
        </div>
    </body>
</html>

But I guess I was a bit late ;)

like image 1
Patrik Avatar answered Nov 04 '22 16:11

Patrik