Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div Expand to Full Screen Smoothly on Click

I am trying to make a div expand smoothly to fullscreen when clicked. The final product I am going for is similar to when a user clicks a case study on this website https://infinum.co/

So far my code can make the div fullscreen but it jumps because of the position fixed I add. I am not bothered whether the actual animation is handled by CSS or JavaScript/jQuery.

$(function() {
  $(".block").on("click", function() {
    $(this).addClass("fullscreen");
  });
});
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.block {
  width: 50%;
  margin: 0 auto 50px auto;
  height: 300px;
  background-color: red;
  transition: all 0.5s linear;
}
.block.fullscreen {
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="block"></div>
<div class="block"></div>

All I have so far can be found on this pen: http://codepen.io/anon/pen/RKGeYj

like image 233
James Walker Avatar asked Jan 16 '17 13:01

James Walker


People also ask

How do I make Div full screen on button click?

You'll want a fixed position element at 100% width and height , if you don't have a background color or image you'll be able to click through it. Set z-index higher then all other elements to ensure it is at the front if you need that.

How do I make a Div expand to the whole screen?

position:absolute You can also use position absolute as well as set all the viewport sides (top, right, bottom, left) to 0px will make the div take the full screen.

How do you expand a div in HTML?

You set that div with a height that matches the div on the left, then add a child div that is scrollable and expands to 100%. See this fiddle. You can try to fine tune the effect you want to achieve by replacing height by either max-height or min-height . Hope it helps.


2 Answers

make your #block fullscreen first and then apply the position:absolute; after a delay greater than the fullscreen animation speed.

Here's a working snippet.

var isFullscreen = false;
$("#block").click(function (){ 
    var prop = {};
    var speed = 910;
    if(!isFullscreen){ // MAXIMIZATION
       prop.width = "100%";
       prop.height = "100vh";
       isFullscreen = true;
      $("#block").animate(prop,speed); 
      setTimeout(function() { 
        $("#block").css("position","absolute");
      }, 920);
    }
    else{         
      prop.width = "50%";
      prop.height = "250px";            
      isFullscreen = false;
      $("#block").animate(prop,speed); 
      setTimeout(function() { 
        $("#block").css("position","relative"); 
      }, 920);
    }
    
});
html,body{
  width:100%;
  height:100%;
  margin:0;
  padding:0;
}
#block,#blockTwo{
  width:50%;
  height:250px;
  margin:0 auto;
  background-color: red;
}
#block{
  z-index:100;
}
#blockTwo{
  background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="block"></div>
<div id="blockTwo"></div>
like image 82
ab29007 Avatar answered Oct 14 '22 13:10

ab29007


Checkout http://usefulangle.com/post/38/animating-lightbox-with-css-javascript .It contains the animation that you're looking for.

When you're making the position as fixed, you should give the initial top & left properties as well. You can get the initial top & left properties using the getBoundingClientRect method. Along with animating top & left, you should animate width & height as well for a smoother look.

.in-animation {
    animation: inlightbox 0.8s forwards;
    position: fixed !important;
}

@keyframes inlightbox 
{ 
    50% { 
        width: 100%;
        left: 0;
        height: 200px;
    }
    100% {
        height: 100%;
        width: 100%;
        top: 0;
        left: 0;
    }
}
like image 30
Useful Angle Avatar answered Oct 14 '22 13:10

Useful Angle