Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS transition between left -> right and top -> bottom positions

Is it possible to use CSS transitions to animate something between a position set as left: 0px to right: 0px so it goes all the way across the screen? I need to accomplish the same thing with top to bottom. Am I stuck calculating the screen width / object-size?

#nav {     position: absolute;     top: 0px;     left: 0px;     width: 50px;     height: 50px;     -webkit-transition: all 0.5s ease-out; }  .moveto {     top: 0px;     right: 0px; } 

and then I use jQuery's .addClass

like image 418
grep Avatar asked Apr 20 '12 13:04

grep


People also ask

What is top bottom left and right in CSS?

The top and bottom properties specify the vertical offset from its normal position; the left and right properties specify the horizontal offset. An absolutely positioned element is an element whose computed position value is absolute or fixed .

When we use top left right bottom property?

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.


1 Answers

You can animate the position (top, bottom, left, right) and then subtract the element's width or height through a CSS transformation.

Consider:

$('.animate').on('click', function(){   $(this).toggleClass("move"); })
     .animate {         height: 100px;         width: 100px;         background-color: #c00;         transition: all 1s ease;         position: absolute;         cursor: pointer;         font: 13px/100px sans-serif;         color: white;         text-align: center;       }                                 /* ↓ just to position things  */       .animate.left   { left: 0;   top: 50%;  margin-top: -100px;}       .animate.right  { right: 0;  top: 50%;  }       .animate.top    { top: 0;    left: 50%; }       .animate.bottom { bottom: 0; left: 50%; margin-left: -100px;}         .animate.left.move {         left: 100%;          transform: translate(-100%, 0);       }        .animate.right.move {         right: 100%;          transform: translate(100%, 0);       }        .animate.top.move {         top: 100%;          transform: translate(0, -100%);       }        .animate.bottom.move {         bottom: 100%;          transform: translate(0, 100%);       }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Click to animate <div class="animate left">left</div> <div class="animate top">top</div> <div class="animate bottom">bottom</div> <div class="animate right">right</div>

And then animate depending on the position...

like image 160
methodofaction Avatar answered Oct 05 '22 15:10

methodofaction