css - How to animate an image as it grows from the width of a container to the full viewport width? -


i have container div image inside of it. when user clicks image, want grow fill viewport width. works me, want animate smoothly grows, not.

how can make image grow smoothly centre of page, rather growing in situ until touches right edge of viewport, , jumping left , continuing until fills viewport?


here's example (you can try on codepen):

html:

<div>   <img id="expandableimage" src="https://placekitten.com/g/500/200"> </div>   

css:

div {   width: 50%;   margin: 0 auto; }  img {   width: 100%;   cursor: pointer;   position: relative; }  .fullsize {   animation-duration: 1s;   animation-name: zoom-in;   animation-fill-mode: forwards;  } .smallsize {   animation-duration: 1s;   animation-name: zoom-out; }  @keyframes zoom-in {   {     width: 100vw;       position: relative;       left: calc(-50vw + 50%);     } }  @keyframes zoom-out {   {     width: 100%;   }   {     width: 100vw;       position: relative;       left: calc(-50vw + 50%);   } } 

js:

document.getelementbyid( "expandableimage" ).addeventlistener( "click", zoom );  function zoom() {         if (this.classname.indexof("fullsize") > -1) {             this.classname = this.classname.replace(" fullsize", "");             this.classname = this.classname + " smallsize";         } else {             this.classname = this.classname.replace(" smallsize", "");             this.classname = this.classname + " fullsize";         }     } 

you don't need @keyframes that, transition enough.

http://jsfiddle.net/xnmojeyn/

document.getelementbyid("expandableimage").addeventlistener("click", zoom);    function zoom() {      if (this.classname.indexof("fullsize") > -1) {          this.classname = this.classname.replace(" fullsize", "");          this.classname = this.classname + " smallsize";      } else {          this.classname = this.classname.replace(" smallsize", "");          this.classname = this.classname + " fullsize";      }  }
body, p {      margin: 0;  }  div {      width: 50vw;      height: 100vh;      margin: 0 auto;      background-color: lightblue;  }  img {      width: 100%;      cursor: pointer;  }  .fullsize {      transition: 1s;      margin-left: -25vw;      width: 100vw;  }  .smallsize {      transition: 1s;      width: 100%;  }
<div>      <p>asdf</p>      <img id="expandableimage" src="https://placekitten.com/g/500/200" />      <p>why doesn't work</p>  </div>


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -