How to achieve reverse animation on mouse out in keyframes - html

I am trying to add scale up animation on a div.
I tried this using both transition and animation property.
In case of transition you can notice that when hovered out the animation is smoothly reversed. However, this doesn't happen when using animation property (the div transitions back to initial width instantly)
Can someone tell me:
Why this behaviour in case of animation only?
How can I achieve the same using animation property?
.animations {
display: flex;
padding: 80px;
width: 100vw;
height: 100vh;
background: linear-gradient(to right, #f3d2d2, white, #cee5f3);
}
.animations > div {
width: 200px;
height: 200px;
margin: 40px;
font-family: system-ui;
display: flex;
flex-direction: column;
align-items: center;
}
.animations > p {
color: black;
flex: 1;
text-align: center;
}
.animations .animated-box {
flex: 2;
width: 100%;
background: grey;
cursor: pointer;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.animated-box.scale-up {
}
.animated-box.scale-up:hover {
animation: scale-up 0.5s ease forwards;
transform: scale(1);
}
.animated-box.scale-up-with-mouseout {
transition: transform 0.5s ease-in;
}
.animated-box.scale-up-with-mouseout:hover {
transform: scale(1.2);
}
#keyframes scale-up {
100% {transform: scale(1.2)};
0%{transform: scale(1)};
}
<div class="animations">
<div>
<div class="animated-box scale-up">Hover me</div>
<p>Scale up (with keyframes)</p>
</div>
<div>
<div class="animated-box scale-up-with-mouseout">Hover me</div>
<p>Scale up (with transition)</p>
</div>
</div>

Related

I have elements that move on my page, when they do, the outlines flicker. Is there a way to fix this?

Any idea why it's happening? I appreciate any and all help. I'm new to html and css so maybe im making a simple mistake here.
I have several columns that move up and down with images loaded locally into the columns. The images shuffle every 6 minutes. The outline around the images flickers as they move up and down the screen. The issue goes away if I remove the outline. I have no idea whats going on.
::-webkit-scrollbar {
display: none;
}
html {
height: 100vh;
height:100%
overflow: hidden;
}
body {
margin: 0;
padding: 0;
height: 100vh;
display: grid;
place-items: center;
scroll: no;
overflow: hidden;
}
.main {
display: flex;
overflow: hidden;
min-height: 50vw;
background: #333;
}
.main img {
border-radius: 6px;
max-width: 100%;
vertical-align: middle;
outline: 1px solid white;
box-sizing: border-box;
##filter: blur(0.03rem);
opacity: .75;
outline-offset: -3px;
padding: 3px;
overflow: hidden;
}
.main:hover img {
##filter: blur(0.03rem);
opacity: .75;
transition: all 10s;
}
.main img:hover {
##filter: blur(0);
filter: drop-shadow(0 0 0.75rem black);
opacity: 1;
transition: all .2s
}
.main .single-column {
-webkit-animation: var(--animation, none) 360s linear 0.01s infinite;
}
.main .single-column:hover {
animation-play-state: paused;
}
.main .single-column:nth-of-type(odd) {
vertical-align:top;
align-self: flex-end;
--direction: 15%;
}
#keyframes slide {
to {
-webkit-transform: translateY(var(--direction, -15%));
}

CSS rotate text to vertical and scroll bottom to top infinite animation

I want to rotate text like this first, and then scroll bottom to top infinitely, I have checked answers like CSS animate scrolling text using text-indent, anyone who can help? Thanks in advance.
What I found was that the behavior of the writing-mode of the English characters is different from that of the Chinese characters.
writing-mode: vertical-lr;
Here is the behavior of the writing-mode of the Chinese characters.
Update
Thanks to #ulou, I changed the code he posted. The original question did not describe clearly. I removed the animation rotate 0 to rotate 90.
.container {
width: 100%;
height: 100vh;
background-color: black;
color: white;
display: flex;
justify-content: center;
}
.cool-text {
align-self: center;
animation: 4s coolAnimation infinite;
font-size: 100px;
white-space: nowrap;
}
#keyframes coolAnimation {
0% {
transform: rotate(90deg);
}
100% {
transform: rotate(90deg) translateX(-100vh);
}
}
<div class="container">
<div class="cool-text">我來试一下效果怎么样</div>
</div>
I just need the infinite animation from bottom to top, below is my code without
animation. You see there is padding-left: 100vh, also I need all the text to disappear in the previous frame before the new frame begins, something like loop text or circular playing.
body {
background: black;
}
.main-nav {
color: white;
font-size: 50vw;
text-align: center;
white-space: nowrap;
height: 100vw;
line-height: 100vw;
transform: rotate(90deg) translateY(-100%);
transform-origin: left top;
width: 100vh;
padding-left: 100vh;
}
<div class="main-nav">
我来试试效果哈哈哈哈
</div>
.container {
width: 100%;
height: 95vh;
background-color: black;
color: white;
display: flex;
justify-content: center;
}
.cool-text {
align-self: center;
font-size: 100px;
transform: rotate(90deg);
animation: 6s coolAnimation infinite;
animation-timing-function: linear;
white-space: nowrap;
width: 100%;
}
#keyframes coolAnimation {
0% {
transform: rotate(90deg) translateX(300vh);
}
100% {
transform: rotate(90deg) translateX(-300vh);
}
}
<div class="container">
<div class="cool-text">Cool animated text</div>
</div>

CSS hover not working after initial animation

I have a basic button, which I animate initially. But once animated, I want to add a new animation on hover; but it seems to not work for some reason.
For example:
button animation:
.container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
max-height: 100%;
overflow: hidden;
}
.slide-btn {
display: flex;
align-items: center;
justify-content: center;
border-radius: 100%;
background-color: #feffff;
box-shadow: 0 2px 20px 0 #686f7638;
margin-top: 10px;
width: 45px;
height: 45px;
animation: testing 1s ease-in forwards;
}
.slide-btn:hover {
transform: scale(1.5);
}
#keyframes testing {
to {
transform: translateX(100px);
}
}
<div class="container">
<div class="slide-btn">></div>
</div>
My guess is that for the CSS animation I'm using forwards, but I really need forwards to be there.
Yes, it's because the forwards that make the animation to override the transform. Instead of forwards you can do like below:
.container {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
max-height: 100%;
overflow: hidden;
}
.slide-btn {
display: flex;
align-items: center;
justify-content: center;
border-radius: 100%;
background-color: #feffff;
box-shadow: 0 2px 20px 0 #686f7638;
margin-top: 10px;
width: 45px;
height: 45px;
transform: translateX(100px);
transition:0.5s;
animation: testing 1s ease-in;
}
.slide-btn:hover {
transform: translateX(100px) scale(1.5);
}
#keyframes testing {
from {
transform: translateX(0px);
}
}
<div class="container">
<div class="slide-btn">></div>
</div>
how about adding 'animation-fill-mode: none' to '.slide-btn:hover':
.slide-btn:hover {
animation-fill-mode: none;
transform: scale(1.5);
}

Transform height of child div when hover on parent div

I would like to transform the height of my .bar to 3px when hovering .wrapper. I know I have to use transition and transform, but I have no idea how to transform my child div by hovering his parent. At the moment, I transform just my parent div (it's clear why). How to transform height of my child by hovering my parent div (should be a bar coming up/down on the bottom of .wrapper)? Note that the parent div should not transform his dimensions, just the child!
.wrapper {
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 200px;
height: 100px;
background-color: lightgreen;
transition: all 1s ease-in-out;
}
.wrapper:hover {
/*ON HOVER WRAPPER TRANSFORM HEIGHT OF BAR TO 3px*/
transform: scale(2);
cursor: pointer;
}
.bar {
width: 100%;
height: 0px;
background-color: blue;
}
.bar:hover {}
<div class="wrapper">
<div class="bar"></div>
</div>
Use the child selector .wrapper:hover>.bar:
.wrapper {
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 200px;
height: 100px;
background-color: lightgreen;
transition: all 1s ease-in-out;
}
.wrapper:hover {
cursor: pointer;
}
.bar {
width: 100%;
height: 0px;
background-color: blue;
transition: height 200ms ease-in-out;
}
.wrapper:hover>.bar {
height: 3px;
}
<div class="wrapper">
<div class="bar"></div>
</div>
Use this CSS selector > to do that,
.wrapper {
display: flex;
flex-direction: column;
justify-content: flex-end;
width: 200px;
height: 100px;
background-color: lightgreen;
transition: all 1s ease-in-out;
}
/*.wrapper:hover {
transform: scale(2);
cursor: pointer;
}*/
.bar {
width: 100%;
height: 0px;
background-color: blue;
transition: 0.6s ease;
}
.wrapper:hover > .bar {
height: 10px;
}
<div class="wrapper">
<div class="bar"></div>
</div>

CSS display none and opacity animation with keyframes not working

I have a very basic piece of HTML with the objective of animating from display: none; to display: block with opacity changing from 0 to 1.
I'm using Chrome browser, which uses the -webkit prefixes as preference and did a -webkit-keyframes transition set to make the animation possible. However, it does not work and just changes the display without fading.
I have a JSFiddle here.
<html>
<head>
<style type="text/css">
#myDiv
{
display: none;
opacity: 0;
padding: 5px;
color: #600;
background-color: #CEC;
-webkit-transition: 350ms display-none-transition;
}
#parent:hover>#myDiv
{
opacity: 1;
display: block;
}
#parent
{
background-color: #000;
color: #FFF;
width: 500px;
height: 500px;
padding: 5px;
}
#-webkit-keyframes display-none-transition
{
0% {
display: none;
opacity: 0;
}
1%
{
display: block;
opacity: 0;
}
100%
{
display: block;
opacity: 1;
}
}
</style>
<body>
<div id="parent">
Hover on me...
<div id="myDiv">
Hello!
</div>
</div>
</body>
</head>
</html>
The display doesn't work with CSS transition or animation.
Use opacity, visibility or z-index. You can combine all them.
Try to use visibility: visible in place display: block and visibility: hidden in place display: none.
And finally, combine z-index: -1 and z-index: 100 for example.
Good work ;)
If you are using #keyframes you should use -webkit-animation instead of -webkit-transition. Here is the doc for #keyframes animation: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations.
See code snippet below:
.parent {
background-color: #000;
color: #fff;
width: 500px;
height: 500px;
padding: 5px;
}
.myDiv {
display: none;
opacity: 0;
padding: 5px;
color: #600;
background-color: #cec;
}
.parent:hover .myDiv {
display: block;
opacity: 1;
/* "both" tells the browser to use the above opacity
at the end of the animation (best practice) */
-webkit-animation: display-none-transition 1s both;
animation: display-none-transition 1s both;
}
#-webkit-keyframes display-none-transition {
0% {
opacity: 0;
}
}
#keyframes display-none-transition {
0% {
opacity: 0;
}
}
<div class="parent">
Hover on me...
<div class="myDiv">Hello!</div>
</div>
2016 UPDATED ANSWER
To reflect today's best practices, I would use a transition instead of an animation. Here is the updated code:
.parent {
background-color: #000;
color: #fff;
width: 500px;
height: 500px;
padding: 5px;
}
.myDiv {
opacity: 0;
padding: 5px;
color: #600;
background-color: #cec;
-webkit-transition: opacity 1s;
transition: opacity 1s;
}
.parent:hover .myDiv {
opacity: 1;
}
<div class="parent">
Hover on me...
<div class="myDiv">Hello!</div>
</div>
You can not animate display property. You can try with visibility: hidden to visibility: visible
Just use position: fixed and drop the z-index: -5 at the end of the #keyframe animation (you can do any negative index....
CSS:
#keyframes fadeOut {
0% { opacity: 1
}
99% {
opacity: 0;
z-index: 1;
}
100%{
opacity: 0;
display:none;
position: fixed;
z-index: -5;
}
}
It's been tricky, it's been nasty, but here it is...
FadeOut (opacity) first
then truly hide (meaning: not covering up or catching any clicks, getting height: 0,...)
display: <whatever> is indeed no option.
But animating scaleY is. Or translate to far-far-away or the old classic: animating max-height (from a specific high px value) down to 0px…
For an earlier version of this snippet with some more general info on „back and forth animation on class toggle“ (and preventing that animation upon initial page load look here.
const div = document.querySelector('.target')
function toggleTarget() {
div.classList.add('active');
div.classList.toggle('play');
}
/* REF https://stackoverflow.com/a/49575979 */
/* REF https://stackoverflow.com/questions/26607330/css-display-none-and-opacity-animation-with-keyframes-not-working/64857102#64857102 */
body, html { /* eye candy */
background: #444; display: flex; min-height: 100vh; align-items: center; justify-content: center;
}
button { font-size: 4em; border-radius: 20px; margin-left: 60px;}
div { /* eye candy */
width: 200px; height: 100px; border-radius: 20px;
background: green; display: flex; align-items: center; justify-content: center;
font-family: sans-serif; font-size: 2em; color: white; text-align: center;
text-shadow: 0px 2px 4px rgba(0,0,0,.6);
}
/* using this extra .active class prevents that there is an animation already on loading */
.active {
animation: fadeAndHideBack 1s linear forwards;
}
.play {
opacity: 0;
/* learning curve: setting background "awaits" animation finish,
setting scale prematurely jumps to it, then doing animation from there */
animation: fadeAndHide 1s linear forwards;
}
#keyframes fadeAndHide {
0% { opacity: 1; }
99.9% { opacity: 0; max-height: 100px; }
100% { opacity: 0; max-height: 0; }
}
#keyframes fadeAndHideBack {
0% { opacity: 0; max-height: 0; }
0.1% { opacity: 0; max-height: 100px; }
100% { opacity: 1; }
}
<div class="target"></div>
<button onclick="toggleTarget()">
Toggle
</button>
You can use Javascript to change both the display properties and animation. You can't put display in #keyframes.
Start with the element display:none. Then simultaneously add display:block and animation:* classes.
Here's a working example with animation in/out.
add this css ;
.fade:not(.show) {
opacity: 1;
}
this work for me..
How about this example: jsfiddle
The issue was needing to use an animation rather than transition with keyframes
#-webkit-keyframes fadeAnimation {
0% {
opacity: 0;
}
25% {
opacity: 0.25;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
#myDiv {
opacity: 0;
padding: 5px;
color: #600;
background-color: #CEC;
}
#parent {
background-color: #000;
color: #FFF;
width: 500px;
height: 500px;
padding: 5px;
}
#parent:hover #myDiv {
-webkit-animation: fadeAnimation 6s;
}
You can't animate the display property. You can animate the visibility property. But visibility is not the same as display, as it will not remove the div element completely from the DOM (the property, visibility:collapse, can remove an element from the DOM, if the element is a table. This is an exception). You can instead animate CSS properties height and width. For instance, the below code will animate the square-block out.
function myAnimation(){
var square= document.getElementById('square');
if(square.getAttribute("class")==='square'){
square.classList.add('animation');
}else{
square.classList.remove('animation');
}
}
.square {
background-color:blue;
transform: translate(0, 0);
width: 100px;
height: 100px;
opacity: 1;
transition: all 0.5s ease-out;
}
.square.animation {
transform: translate(-260px, -260px);
width: 0;
height: 0;
opacity: 0;
transition: all 0.5s ease-in;
}
<html>
<body>
<div class="square" id="square"></div>
<br/>
<button onclick="myAnimation()">Animate</button>
</body>
</html>
FYI, I have used CSS transitions to animate the div. Hope this was useful.