I want to have a div, with a word inside. This word should appear with animation when the div is hovered over. This is animating and behaving the way I want, EXCEPT... the word "hello" is visible when the page loads. I want it to be hidden, and then animate into view. What do I need to fix?
#test{
border: 1px solid #000;
}
#hello {
animation-name: hello;
animation-duration: 0.5s;
animation-fill-mode: backwards;
animation-play-state: paused;
}
#hello:hover {
animation-fill-mode: forwards;
animation-play-state: running;
visibility: visible;
}
#keyframes hello {
0% {visibility:(hidden);transform:scale(0.5); opacity:(0.0);}
1% {visibility:(visible);}
50% {transform:scale(1.2); opacity:(0.5);}
100% {transform:scale(1.0); opacity:(1.0);}
}
<div id="test">
<div id="hello">
Hello!
</div>
</div>
You need to simply remove () from the values of properties as it make them invalid. Also it's better to change the opacity in the 1% of the animation to be able to see the scale effect.
You may also remove the visiblity property from the CSS as it's useless since you are using opacity
#test {
border: 1px solid #000;
background:#fff;
overflow:hidden; /* added this to avoid the scroll to appear and disappear*/
}
#hello {
animation-name: hello;
animation-duration: 0.5s;
animation-fill-mode: backwards;
animation-play-state: paused;
}
#test:hover #hello {
animation-fill-mode: forwards;
animation-play-state: running;
/*visibility: visible; can be removed*/
}
#keyframes hello {
0% {
/*visibility: hidden; can be removed*/
transform: scale(0.5);
opacity: 0;
}
1% {
/*visibility: visible; can be removed*/
opacity:0.3;
}
50% {
transform: scale(1.2);
opacity:0.5;
}
100% {
transform: scale(1.0);
opacity:1.0;
}
}
<div id="test">
<div id="hello">
Hello!
</div>
</div>
Related
I'm playing around with CSS animation, and I was wondering if there's a way to make a vertical line (with certain height) to grow in length automatically and turn to left and add an image at the end when the page loads. I was able to draw vertical line but how to turn it to left and add image.
https://jsfiddle.net/29303g27/3/
#cool
{
width:2px;
margin-left:10%;
background-color:#431;
margin-top:35%;
animation:grow 3s forwards;
position:relative;
}
#keyframes grow
{
0% {
height: 0px;
bottom:0;
}
100%{
height: 200px;
bottom:200px;
}
}
<div id=cool>
</div>
By using div inner div
#cool2{
height:0px;
width:1px;
border-bottom:2px solid #000;
-webkit-animation: increase 3s;
-moz-animation: increase 3s;
-o-animation: increase 3s;
animation: increase 3s;
animation-fill-mode: forwards;
margin-left:10%;
margin-top:0px;
animation-delay:5s;
-webkit-animation-delay:3s;
}
#keyframes increase {
100% {
width: 200px;
}
}
.image
{
width:0px;
height:200px;
left:2px;
top:2px;
-webkit-animation: fade 3s;
-moz-animation: fade 3s;
-o-animation: fade 3s;
animation: fade 3s;
animation-fill-mode: forwards;
margin-left:10%;
margin-top:0px;
animation-delay:6s;
-webkit-animation-delay:6s;
position:absolute;}
#keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
width:200px;
}
}
#cool
{
width:2px;
margin-left:10%;
background-color:#431;
margin-top:35%;
animation:grow 3s forwards;
position:relative;
}
#keyframes grow
{
0% {
height: 0px;
bottom:0;
}
100%{
height: 200px;
bottom:200px;
}
}
<div id=cool>
<div id=cool2>
<img class='image' src='https://www.google.co.in/logos/doodles/2017/sitara-devis-97th-birthday-6469056130449408.5-l.png'>
</div>
</div>
and Check this 2nd way using :after Element
#cool:after{
height:0px;
width:1px;
border-bottom:2px solid #000;
-webkit-animation: increase 3s;
-moz-animation: increase 3s;
-o-animation: increase 3s;
animation: increase 3s;
animation-fill-mode: forwards;
margin-left:10%;
margin-top:0px;
animation-delay:5s;
-webkit-animation-delay:3s;
position:absolute;
content:'';
}
#keyframes increase {
100% {
width: 200px;
}
}
.image
{
width:0px;
height:200px;
left:2px;
top:2px;
-webkit-animation: fade 3s;
-moz-animation: fade 3s;
-o-animation: fade 3s;
animation: fade 3s;
animation-fill-mode: forwards;
margin-left:10%;
margin-top:0px;
animation-delay:6s;
-webkit-animation-delay:6s;
position:absolute;}
#keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
width:200px;
}
}
#cool
{
width:2px;
margin-left:10%;
background-color:#431;
margin-top:35%;
animation:grow 3s forwards;
position:relative;
}
#keyframes grow
{
0% {
height: 0px;
bottom:0;
}
100%{
height: 200px;
bottom:200px;
}
}
<div id=cool>
<img class='image' src='https://www.google.co.in/logos/doodles/2017/sitara-devis-97th-birthday-6469056130449408.5-l.png'>
</div>
It can be done in many ways. Example
In example I use wrapper element and place line element inside. In this way, line element can use percentage value which relate to wrapper element. It means we don't need to fix height or width just let it grows by image.
<div class='wrapper'>
<img class='image' src='https://images.unsplash.com/photo-1485963631004-f2f00b1d6606?auto=format&fit=crop&w=360&h=180&q=60'>
<div class='line'></div>
</div>
and now animation will be
#keyframes show-up {
0% {
height: 0;
}
50% {
left: 0;
height: 100%;
}
100% {
left: 100%;
height: 100%;
}
}
I have a very simple animation that fades out and shrinks a div.
But the problem is that when the animation finishes it goes back to the start and stays there.
div {
background-color: red;
height: 80px;
}
.fade-out {
animation-name: fade-out;
animation-duration: 2s;
}
#keyframes fade-out {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 0; height: 0;}
}
<div class="fade-out">Style Test</div>
If you add animation-fill-mode: forwards; to your .fade-out rule it will fix your animation.
animation-fill-mode specifies how CSS rules should be applied before and after executing the animation. The default is none which means that before and after the animation is executed, it will not apply any of the animation styles. That's why you're seeing it revert to the pre-animation state.
forwards tells the browser to retain the styles from the last keyframe. That's what you're looking for.
See the MDN docs for more information.
div {
background-color: red;
height: 80px;
}
.fade-out {
animation-name: fade-out;
animation-duration: 2s;
animation-fill-mode: forwards;
}
#keyframes fade-out {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 0; height: 0;}
}
<div class="fade-out">Style Test</div>
Use animation-fill-mode property
.fade-out {
animation-name: fade-out;
animation-duration: 2s;
animation-fill-mode: forwards
}
I want to an object move out and in screen(slide in left to right and slide out right to left).
I use this CSS:
#-webkit-keyframes slideInSmooth {
0% {
-webkit-transform: translate3d(-100%,0,0);
}
100% {
-webkit-transform: translate3d(0,0,0);
}
}
#-webkit-keyframes slideOutSmooth {
0% {
-webkit-transform: translate3d(0,0,0);
}
100% {
-webkit-transform: translate3d(-100%,0,0);
}
}
.tabs {
-webkit-animation: slideInSmooth ease-in 250ms;
animation: slideInSmooth ease-in 250ms;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
-webkit-animation-duration: 250ms;
animation-duration: 250ms;
}
.tabs-item-hide > .tabs{
-webkit-animation: slideOutSmooth ease-in 250ms;
animation: slideOutSmooth ease-in 250ms;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
-webkit-animation-duration: 250ms;
animation-duration: 250ms;
}
My Html look like this:
<div id="div1" class="">
<div class="tabs">
</div>
</div>
class tabs-item-hide will be add in div1 by an event and after that item
hide.
class tabs-item-hide will be remove in div1 by an event if want to show
item
The code work fine when object show, it slide from left to right perfect.
But when object hide it does nothing!
Please help me correct my css to have object move from right to left out of screen.
Thanks!
I hope I understood you correctly, but I guess that should work for you.
.tabs-item-hide {
width:100%;
overflow:hidden;
position:relative;
height:50px;
}
#-webkit-keyframes slideInSmooth {
0% { left:0; }
90% { left:100%; }
90.0001% { left:-50px; }
100% { left:0; }
}
#keyframes slideInSmooth {
0% { left:0; }
90% { left:100%; }
90.0001% { left:-50px; }
100% { left:0; }
}
.tabs {
-webkit-animation: slideInSmooth 2.5s forwards;
animation: slideInSmooth 2.5s forwards;
position:absolute;
width:50px;
height:50px;
background:#ffa500;
}
<div class="tabs-item-hide">
<div class="tabs"></div>
</div>
In the slideshow shown below, there are two images available. Once clicking on a button for the second image after first opening my page, there is a sudden jump to that image with no 5 second transition as expected. Also when doing this, I notice that #slideshowimage-2 is shown in the url (doing this offline) after clicking the button for that image. Here's the code below:
CSS:
.slideshowcontainer {
width:800px;
height:400px;
margin-left:auto;
margin-right:auto;
margin-top:0px;
text-align:center;
overflow:hidden;
position:relative;
top:30px;
border-style:solid;
border-width:10px;
border-color:white;
border-radius:15px;
}
.imagecontainer {
width:1800px;
height:400px;
clear: both;
position:relative;
-webkit-transition:left 3s;
-moz-transition:left 3s;
-o-transition:left 3s;
-ms-transition:left 3s;
transition:left 3s;
animation:scroller 16s infinite;
}
#keyframes scroller {
0% {transform:translateX(0);}
31.25% {transform:translateX(0);}
50% {transform:translateX(-800px);}
81.25% {transform:translateX(-800px);}
100% {transform:translateX(0);}
}
.slideshowimage {
float:left;
margin:0px;
padding:0px;
position:relative;
}
#keyframes change {
0% {
transform: translateX(-800px);
}
100% {
transform: translateX(0);
animation: scroller 16s infinite;
}
}
#keyframes change2 {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-800px);
animation: scroller2 16s infinite;
}
}
#slideshowimage-1:target ~ .imagecontainer {
animation: none;
transform: translateX(0px);
animation: change 3s forwards;
}
#slideshowimage-2:target ~ .imagecontainer {
animation: none;
transform: translateX(-800px);
animation: change2 3s forwards;
}
.buttoncontainer {
position:relative;
top:-20px;
}
.button {
display:inline-block;
height:10px;
width:10px;
border-radius:10px;
background-color:darkgray;
-webkit-transition:background-color 0.25s;
-moz-transition:background-color 0.25s;
-o-transition:background-color 0.25s;
-ms-transition:background-color 0.25s;
transition:background-color 0.25s;
}
.button:hover {
background-color:gray;
}
HTML:
<div class="slideshowcontainer">
<span id="slideshowimage-1"></span>
<span id="slideshowimage-2"></span>
<span id="slideshowimage-3"></span>
<div class="imagecontainer">
<img src="WebServiceSlide.png" class="slideshowimage" style="width:800px;height:400px;">
<img src="es-flag.png" class="slideshowimage" style="width:800px;height:400px;">
</div>
<div class="buttoncontainer">
</div>
</div>
How could I make it so that the transition set upon clicking a button occurs on the first click? Many thanks in advance.
Reason:-
Because the left and translateX both are different. If you apply left:-800px when slide is at translateX(-800px) (2nd slide) then animation will continue at the 2nd part of slideshow. Thats why you are seeing a blank white space(when translateX(-800px) accors when it is already left:-800px).
Solution:-
You either have to use translateX or left. use the same in all the places
Part of code that solved the problem:-
#keyframes change {
0% {
transform: translateX(-800px);
}
100% {
transform: translateX(0);
animation: scroller 16s infinite;
}
}
#keyframes change2 {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-800px);
animation: scroller2 16s infinite;
}
}
#slideshowimage-1:target ~ .imagecontainer {
animation: none;
transform: translateX(0px);
animation: change 3s forwards;
}
#slideshowimage-2:target ~ .imagecontainer {
animation: none;
transform: translateX(-800px);
animation: change2 3s forwards;
}
Explanation:-
This code doesn't do translateX manually. Instead it uses animation to scoll single time by animation: change 3s forwards;
Drawback:-
Once we click on the slide selection button the animation stops. I have even tried it to solve by adding animation in the change keyframes animate end section. But unfortunately it didn't work. So I would suggest an alternate method to overcome the drawback as follows
To overcome the drawback I have added a play button which
will replay the slideshow animation which got paused by the slide
button. (Once we click on play button it takes a little time to slide
as we have given 16s in animation)
DEMO:-
.slideshowcontainer {
width: 800px;
height: 400px;
margin-left: auto;
margin-right: auto;
margin-top: 0px;
text-align: center;
overflow: hidden;
position: relative;
top: 30px;
border-style: solid;
border-width: 10px;
border-color: white;
border-radius: 15px;
}
.imagecontainer {
width: 1800px;
height: 400px;
clear: both;
position: relative;
-webkit-transition: all 3s;
-moz-transition: all 3s;
-o-transition: all 3s;
-ms-transition: all 3s;
transition: all 3s;
transform: translateX(0px);
animation: scroller 16s infinite;
}
#keyframes scroller {
0% {
transform: translateX(0);
}
31.25% {
transform: translateX(0);
}
50% {
transform: translateX(-800px);
}
81.25% {
transform: translateX(-800px);
}
100% {
transform: translateX(0);
}
}
#keyframes scroller2 {
0% {
transform: translateX(-800px);
}
31.25% {
transform: translateX(-800px);
}
50% {
transform: translateX(0);
}
81.25% {
transform: translateX(0);
}
100% {
transform: translateX(-800px);
}
}
#keyframes change {
0% {
transform: translateX(-800px);
}
100% {
transform: translateX(0);
animation: scroller 16s infinite;
}
}
#keyframes change2 {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-800px);
animation: scroller2 16s infinite;
}
}
.slideshowimage {
float: left;
margin: 0px;
padding: 0px;
position: relative;
}
#slideshowimage-1:target ~ .imagecontainer {
animation: none;
transform: translateX(0px);
animation: change 3s forwards;
}
#slideshowimage-2:target ~ .imagecontainer {
animation: none;
transform: translateX(-800px);
animation: change2 3s forwards;
}
#slideshowimage-3:target ~ .imagecontainer {
transform: translateX(0);
animation: scroller 16s infinite;
}
.buttoncontainer {
position: relative;
top: -20px;
}
.button {
display: inline-block;
height: 10px;
width: 10px;
border-radius: 10px;
background-color: darkgray;
-webkit-transition: background-color 0.25s;
-moz-transition: background-color 0.25s;
-o-transition: background-color 0.25s;
-ms-transition: background-color 0.25s;
transition: background-color 0.25s;
}
.button:hover {
background-color: gray;
}
.buttonplay:after {
height: 0;
width: 0;
top: 0;
margin-left: 10px;
position: absolute;
content: ' ';
border-left: solid 13px darkgray;
border-top: solid 8px transparent;
border-bottom: solid 8px transparent;
}
<div class="slideshowcontainer">
<span id="slideshowimage-1"></span>
<span id="slideshowimage-2"></span>
<span id="slideshowimage-3"></span>
<div class="imagecontainer">
<img src="https://placehold.it/800x400" class="slideshowimage" style="width:800px;height:400px;">
<img src="https://placehold.it/900x450" class="slideshowimage" style="width:800px;height:400px;">
</div>
<div class="buttoncontainer">
</div>
</div>
I'm trying to animate in CSS3 margins, which this site seems to say you can, but I can't get working.
I actually have 3 animations. 1 for a simple initial fadeIn on initial load, then the 2 others for the margin animation on click. I've also just tried margin instead of the top and bottom but still no sign of it working.
Click on a section to see animation toggle.
$(".section").click(function() {
$(this).toggleClass("open");
});
body{
background: #f1f1f1;
}
.section{
display: block;
background: #fff;
border-bottom: 1px solid #f1f1f1;
animation: fadeIn .5s ease, margin-top .5s ease, margin-bottom .5s ease;
}
.section.open {
margin: 20px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div class="wrapper">
<div class="section">Some content</div>
<div class="section">Some content</div>
<div class="section">Some content</div>
<div class="section">Some content</div>
<div class="section">Some content</div>
<div class="section">Some content</div>
<div class="section">Some content</div>
</div>
Here is a JSFiddle: http://jsfiddle.net/ybh0thp9/3/
You don't need keyframes for this: http://jsfiddle.net/BramVanroy/ybh0thp9/7/
transition: margin 700ms;
You need to add the transition property to the base element that you wish to animate.
You also mentioned that you wanted opacity change, but I don't see how that's possible considering you only have a single element without children. I mean: you can't click on the element if it's hidden.
What you can do, though, is add opacity to the whole thing: http://jsfiddle.net/BramVanroy/ybh0thp9/9/
Or even prettier, with a transformation:
http://jsfiddle.net/BramVanroy/ybh0thp9/10/
.section {
margin: 0;
opacity: 0.7;
transform: scale(0.85);
transition: all 700ms;
}
.section.open {
margin: 20px 0;
opacity: 1;
transform: scale(1);
}
Per comment, you want to fade in the elements on page load. We can do that by adding a class init.
http://jsfiddle.net/BramVanroy/ybh0thp9/12/
$(".section").addClass("init"); // JS
.section.init {opacity: 1;} // CSS
With keyframes: http://jsfiddle.net/BramVanroy/ybh0thp9/14/
#-webkit-keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }
#-moz-keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }
#keyframes fadeIn { from {opacity: 0; } to { opacity: 1; } }
-webkit-animation: fadeIn 1.5s ease;
-moz-animation: fadeIn 1.5s ease;
animation: fadeIn 1.5s ease;
Tip for using transitions if it still isn't working...
Make sure you're not setting two separate transitions for different properties like this:
transition: margin 1000ms ease-in-out;
transition: box-shadow 1000ms ease-in-out;
It's obvious what's happening when looking in your browser's debugging tools:
The box-shadow will animate as intended, but margin isn't considered due to normal css rule handling.
The correct way is to combine the rules:
transition: margin 1000ms ease-in-out, box-shadow 1000ms ease-in-out;
To create animations witch CSS3 you need to:
Create a class with animation attribute; to work in some browsers you need to put prefixes: -webkit-, -o-, -moz-.
Create animation keyframes
see the example:
.animate{
animation: myAnimation 10s;
animation-direction: alternate;
animation-play-state: running;
animation-iteration-count: infinite;
animation-delay: 0;
animation-timing-function: 1;
animation-direction: alternate;
-webkit-animation: myAnimation 10s;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
-webkit-animation-iteration-count: infinite;
-webkit-animation-delay: 0;
-webkit-animation-timing-function: 1;
-webkit-animation-direction: alternate;
-moz-animation: myAnimation 10s;
-moz-animation-direction: alternate;
-moz-animation-play-state: running;
-moz-animation-iteration-count: infinite;
-moz-animation-delay: 0;
-moz-animation-timing-function: 1;
-moz-animation-direction: alternate;
-o-animation: myAnimation 10s;
-o-animation-direction: alternate;
-o-animation-play-state: running;
-o-animation-iteration-count: infinite;
-o-animation-delay: 0;
-o-animation-timing-function: 1;
-o-animation-direction: alternate;
}
#keyframes myAnimation {
0% { margin-top: 0; margin-left: 50px}
25% { margin-top: 100px; margin-left: 50px }
50% { margin-top: 0; margin-left: 50px }
75% { margin-top: 100px; margin-left: 50px }
100% { margin-top: 0; margin-left: 50px }
}
#-webkit-keyframes myAnimation {
0% { margin-top: 0; margin-left: 100px}
25% { margin-top: 100px; margin-left: 100px }
50% { margin-top: 0; margin-left: 100px }
75% { margin-top: 100px; margin-left: 100px }
100% { margin-top: 0; margin-left: 100px }
}
#-moz-keyframes myAnimation {
0% { margin-top: 0; margin-left: 100px}
25% { margin-top: 100px; margin-left: 100px }
50% { margin-top: 0; margin-left: 100px }
75% { margin-top: 100px; margin-left: 100px }
100% { margin-top: 0; margin-left: 100px }
}
#-o-keyframes myAnimation {
0% { margin-top: 0; margin-left: 100px}
25% { margin-top: 100px; margin-left: 100px }
50% { margin-top: 0; margin-left: 100px }
75% { margin-top: 100px; margin-left: 100px }
100% { margin-top: 0; margin-left: 100px }
}