8

I have a DIV that is covering the whole page (height and width are 100%). I am trying to use CSS (and possibly JavaScript) to create a zoom out animation effect so the DIV is smaller (making everything inside the div - its children - smaller as well) to a specific point on the page (middle of the page) and to a specific width and height (let's say 100 * 100px for example).

I am starting with the following code:

<div id="toBeZoomedOut">
  <div>something</div>
  <div><img src="background.jpg"></div>
</div>

#toBeZoomedOut {
   background-color: black;
   border: 1px solid #AAAAAA;
   color: white;
   width: 300px;
   height: 300px;
   margin-left: auto;
   margin-right: auto;
   -webkit-transition: 1s ease-in-out;
   -moz-transition: 1s ease-in-out;
   transition: 1s ease-in-out;
}

#toBeZoomedOut img {
   height: 250px;
   width: 250px;
}

#toBeZoomedOut:hover {
   zoom: 0.5;
}

The issue with this code is that it zooms out on component down (the parent div) and immediately zooms out what's inside it then goes back to zoom in the components.

Basically it is a little buggy. Any helpful fixes to make it zoom out everything together? It would be great if I can zoom out everything together to a specific location on the page and to a specific width/height (for example, zoom everything out to left: 100px, top: 100px and the parent div should be: 100px * 100px and everything else is relative in size).

I understand this might be easier with JavaScript? Any help?

One final note, if you notice the animation is not really reflecting a zoom animation. Although this would be an additional plus, the actual zoom animation would be great.

JSFiddle link to make it easier: http://jsfiddle.net/HU46s/

TylerH
  • 20,799
  • 66
  • 75
  • 101
user220755
  • 4,358
  • 16
  • 51
  • 68
  • Are you trying to do [this](http://stackoverflow.com/questions/21300673/how-do-i-zoom-a-background-image-on-a-div-with-background-size/21300704#21300704)? – Mr. Alien Feb 07 '14 at 19:12
  • Thanks for responding but not really. What I am trying to do is when I have a div (even if it has 10 different children with different sizes and locations within that div), I am trying to zoom the parent div out, zoom out what's inside it relatively to its now new size without the div being zoomed in in the first place. What you have there is a div that has its background zoomed in on hover and zoomed out when you leave the hover which is different than my question. I do appreciate you trying to help though, thanks :) – user220755 Feb 07 '14 at 19:15
  • Oh you need them relative :) ok ok, thanks for clarifying.. – Mr. Alien Feb 07 '14 at 19:16

2 Answers2

6

I am using the universal selector to target everything inside of the parent container to have the css transitions applied to it.

The next thing I did was changed the inside contents width to a % for ease of scaling.

Here is the css:

#toBeZoomedOut * {
   -webkit-transition: all 1s ease;
   -moz-transition: 1s ease;
   transition: 1s ease;
}

Finally, a fiddle: Demo

Josh Powell
  • 6,219
  • 5
  • 31
  • 59
  • Thank you for the answer! Few more questions (asked in the question above): 1. Can we have the final result be of a specific width and height instead of zoom: 0.5? 2. Can we have the final result (the final zoomed out box) be at a specific location (zoom out to a specific location in the page smoothly, for example top: 100px, left: 100px)? Thanks again for the answer :) – user220755 Feb 07 '14 at 19:29
  • Awesome, I also updated it to make sure it zooms out to a specific location on the page. This is cool, I will accept this as an answer. Thanks! :) – user220755 Feb 07 '14 at 19:37
  • No problem mate, best of luck in your design! – Josh Powell Feb 07 '14 at 19:40
  • Using zoom is not recommended, since it does not work on most browsers:https://developer.mozilla.org/en-US/docs/Web/CSS/zoom "Non-standard This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future." – Vlad Jun 21 '17 at 12:58
2

To make all images and div backgrounds zoom at the same time you have to use percentage size for #zoomer-inside elements and set a specific font-sizes...

However is not smooth, if you want a smoother result, I suggest you use a jQuery in combination with some animation() method or plugin.

Fiddle: http://jsfiddle.net/HU46s/1/

Code:

#toBeZoomedOut {
   background-color: black;
   border: 1px solid #AAAAAA;
   color: white;
   width: 300px;
   height: 300px;
   margin-left: auto;
   margin-right: auto;
   -webkit-transition: all 1s ease-in-out;
   -moz-transition: all 1s ease-in-out;
   transition: all 1s ease-in-out;
}
#toBeZoomedOut div, #toBeZoomedOut img {
   width: 90%;
   font-size: 20px;
}
#toBeZoomedOut img {
   height: 90%;
   width: 90%;
}

#toBeZoomedOut:hover {
   zoom: 0.5;
}

smoother by jQuery:

Fiddle: http://jsfiddle.net/HU46s/5/

Code: jQuery - smoother solution (even less CSS):

 $('#toBeZoomedOut').hover( /* change the animation speed as you want :) */
     function(){
        $(this).animate({ 'zoom': 0.5}, 400); //animation speed 400=0.4s !
     },
     function(){
        $(this).animate({ 'zoom': 1}, 400); //animation speed 400=0.4s !
     }
 );

...with this only CSS you need is:

#toBeZoomedOut {
   background-color: black;
   border: 1px solid #AAAAAA;
   color: white;
   width: 300px;
   height: 300px;
   margin-left: auto;
   margin-right: auto;
}

#toBeZoomedOut img {
   width: 250px;
}
jave.web
  • 13,880
  • 12
  • 91
  • 125
  • Thanks for the answer! Although this fixes part of the issue (zooming everything relatively) which is great, it is still not smooth (as you mentioned) as well as the fact that it doesn't look like a zoom effect. Do you think you can provide any help with a jQuery implementation? Thanks again for the answer :) – user220755 Feb 07 '14 at 19:27