Question:
NO css , we can use the transition
property to set a transition effect when some of an element's properties are changed.
Example:
.box{
transition: all 1s linear;
background-color: purple;
height: 200px;
box-sizing:border-box;
}
.box:hover{
background-color: #add555;
box-shadow: 0 0 10px 10px #000 inset;
}
<div class='box'></div>
In the case above, I used the all
option, where all properties are affected by the transition
.
However, it is also possible to define which and how some properties are changed.
For example:
.box:hover{
transition: opacity 2s linear, background-color .2s ease;
}
Note that I only defined opacity
and background-color
in the example above.
But I have a problem.
I have a div that I want to animate its appearance via transform: scale(1)
. But that same div has a property inside the transform, which is translate(-50%, -50%)
.
So:
.box{
height: 100px;
background-color: pink;
position:fixed;
width:200px;
height:200px;
left: 50%;
top: 50%;
transform: scale(1) translate(-50%, -50%);
transition: transform 1s linear;
}
.box:hover{
transform:scale(0) translate(-50%, -50%);
}
<div class="box"></div>
I need to animate the transform
, but only the scale
, I don't want to animate the translate
. Is it possible to do this in css
?
Because after I added the translate, the animation didn't look like I would have liked. In this case, it has to be something like this:
.box{ height: 100px; background-color: pink; position:fixed; width:200px; height:200px; left: 30%; top: 30%; transform: scale(1); transition: transform 1s linear; } .box:hover{ transform:scale(0); }
<div class='box'></div>
Note that in this example above, the scale
caused the object to shrink to the center rather than the right side.
How can I do this in css?
Answer:
try to use
transform-origin: bottom right;