Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I apply a CSS transition to the overflow property?

I'm trying to set a transition-delay to the overflow property of body when a div is clicked by adding a class to the body as follows:

$("div").click(function(){      $("body").addClass("no_overflow");  });
div{    background:lime;    height:2000px;  }  .no_overflow{     overflow:hidden;  }  body{      overflow:auto;    transition: overflow 0 2s;    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div>I'm div</div>

However, this doesn't seem to work (there's no delay). Am I doing anything wrong here?

I know this can be achieved by using setTimeout function, but was wondering why can't this be achieved using css transitions? Are there any specific style properties to which css transitions can be applied?

like image 567
Nikunj Madhogaria Avatar asked Jan 12 '15 15:01

Nikunj Madhogaria


People also ask

Can you animate the display property?

One of the properties that cannot be animated is the display property.

Can we add transition to display?

display is not one of the properties that transition works upon. See Animatable CSS properties for the list of CSS properties that can have transitions applied to them.

What is CSS overflow property?

The CSS overflow property controls what happens to content that is too big to fit into an area. This text is really long and the height of its container is only 100 pixels. Therefore, a scrollbar is added to help the reader to scroll the content.


2 Answers

There are many properties that can't be transitioned. overflow is among them; the render engine has no idea how to transition between "hidden" and "shown", because those are binary options, not intervals. This is the same reason why you can't transition between display: none; and display: block; (for example): there are no in-between phases to use as transitions.

You can see a list of properties you can animate here on Mozilla Developer Network.

like image 108
TylerH Avatar answered Sep 20 '22 21:09

TylerH


You can simulate a delay with animation:

$("div").click(function() {    $("body").addClass("no_overflow");  });
div {    background: lime;    height: 2000px;  }    .no_overflow {    overflow: hidden;    /* persist overflow value from animation */    animation: 7s delay-overflow;  }    body {    overflow: auto;  }    @keyframes delay-overflow {    from { overflow: auto; }  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div>I'm div</div>

You'll have to apply a separate animation to .body if you want a delay on removeClass, and also to take care that the two animations don't overlap or they'll cancel each other out.

like image 36
Dmitry Avatar answered Sep 18 '22 21:09

Dmitry