I wrote animation of changing background and it work every browser besides firefox. On FF background is changing but without animation effect. I tried out #-webkit-keyframes but it didn't helped.
Thats the code:
https://jsfiddle.net/tkw5pfm8/
background-image fade animation in FF is really disables. You will have to use separate divs with background-image for that
* {
box-sizing: border-box;
}
html {
height: 100%;
}
body {
min-height: 100%;
margin: 0;
padding: 0;
}
.background {
width: 100%;
height: 100%;
position: absolute;
}
.background>div {
width: 100%;
height: 100%;
position: absolute;
background-size: cover;
background-position: center;
}
.slide1 {
background-image: url(https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__340.jpg);
animation: fade 8s infinite;
}
.slide2 {
background-image: url(https://c4.wallpaperflare.com/wallpaper/246/739/689/digital-digital-art-artwork-illustration-abstract-hd-wallpaper-thumb.jpg);
animation: fade2 8s infinite;
}
.slide3 {
background-image: url(https://wallpaperplay.com/walls/full/c/5/3/34778.jpg);
animation: fade3 8s infinite;
}
#keyframes fade {
0% {
opacity: 1
}
33.333% {
opacity: 0
}
66.666% {
opacity: 0
}
100% {
opacity: 1
}
}
#keyframes fade2 {
0% {
opacity: 0
}
33.333% {
opacity: 1
}
66.666% {
opacity: 0
}
100% {
opacity: 0
}
}
#keyframes fade3 {
0% {
opacity: 0
}
33.333% {
opacity: 0
}
66.666% {
opacity: 1
}
100% {
opacity: 0
}
}
<div class='background'>
<div class='slide1'></div>
<div class='slide2'></div>
<div class='slide3'></div>
</div>
Or better, use some simple JS slider, like Swiper
https://swiperjs.com/demos/#fade_effect
Related
I copied this code from codepen and there is only 5 text looping I want to add the 6th to show but when i copy the same style with new class name i.e "w6" and the new animation with "w6anim" it doesn't load the 6th waqas. Kindly help!
I tried to change #keyframe values but still, it doesn't work.
#font-face {
font-family: system;
font-style: normal;
font-weight: 600;
src: local(".SFNSText-Light"), local(".HelveticaNeueDeskInterface-Light"), local(".LucidaGrandeUI"), local("Ubuntu Light"), local("Segoe UI Light"), local("Roboto-Light"), local("DroidSans"), local("Tahoma");
}
* {
margin: 0;
padding: 0;
background: transparent;
font-family: "system";
}
.container {
width: 200px;
}
.word {
font-size: 3em;
font-weight: bold;
float: left;
opacity: 0;
position: absolute;
top: 40%;
left: 40%;
}
.w1 {
animation: w1anim 20s infinite;
}
.w2 {
animation: w2anim 20s infinite;
}
.w3 {
animation: w3anim 20s infinite;
}
.w4 {
animation: w4anim 20s infinite;
}
.w5 {
animation: w5anim 20s infinite;
}
.w6 {
animation: w6anim 20s infinite;
color: red;
}
#keyframes w1anim {
0% {
opacity: 0;
}
5% {
opacity: 1;
}
10% {
opacity: 0;
}
}
#keyframes w2anim {
10% {
opacity: 0;
}
15% {
opacity: 1;
}
20% {
opacity: 0;
}
}
#keyframes w3anim {
20% {
opacity: 0;
}
25% {
opacity: 1;
}
30% {
opacity: 0;
}
}
#keyframes w4anim {
30% {
opacity: 0;
}
35% {
opacity: 1;
}
40% {
opacity: 0;
}
}
#keyframes w5anim {
40% {
opacity: 0;
}
45% {
opacity: 1;
}
50% {
opacity: 0;
}
}
#keyframes w6anim {
50% {
opacity: 0 !important;
}
55% {
opacity: 1 !important;
}
60% {
opacity: 0 !important;
}
}
<div class="container">
<div class="word w1">LOREM</div>
<div class="word w2">IPSUM</div>
<div class="word w3">CARPE</div>
<div class="word w4">DIEM</div>
<div class="word w5">AVENO</div>
<div class="word w6">waqas</div>
</div>
link to the codepen https://codepen.io/waqasahmad9961/pen/bGbxpNr
Remote !important and simply copy 1:1 in future cases.. never change a running system
#keyframes w6anim {
50% {
opacity: 0 !important;
}
55% {
opacity: 1 !important;
}
60% {
opacity: 0 !important;
}
}
Remove -> !important
How could I place a text over the image?
When I try it just makes the text stay in one of the images.
I don't want the text to change or disappear, it has to be the same text for all the images.
I have tried to set position as absolute and place it over the image but won't show for all images just the first one.
.slider {
max-width: 1080px;
height: 95%;
margin: 0 auto;
padding-top: 120px;
position: relative;
}
.slide1,.slide2,.slide3 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
background: url(images/child.jpg)no-repeat center;
background-size: cover;
animation:fade 8s infinite;
-webkit-animation:fade 8s infinite;
}
.slide2 {
background: url(images/tech.jpg)no-repeat center;
background-size: cover;
animation:fade2 8s infinite;
-webkit-animation:fade2 8s infinite;
}
.slide3 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
background-size: cover;
animation:fade3 8s infinite;
-webkit-animation:fade3 8s infinite;
}
#keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 1}
}
#keyframes fade2
{
0% {opacity:0}
33.333% { opacity: 1}
66.666% { opacity: 0 }
100% { opacity: 0}
}
#keyframes fade3
{
0% {opacity:0}
33.333% { opacity: 0}
66.666% { opacity: 1}
100% { opacity: 0}
}
<div class="slider">
<div class="slide1"></div>
<div class="slide2"></div>
<div class="slide3"></div>
</div>
Add an element, position it absolute also, and give it a bigger z-index than that of the images.
.slider {
position: relative;
max-width: 1080px;
height: 95vh;
margin: 0 auto;
padding-top: 120px;
overflow: hidden;
}
.slider-text {
position: absolute;
left: 0;
top: 0 z-index: 2;
}
<div class="slider">
<div class="slider-text"> My text </div>
<div class="slide1"></div>
<div class="slide2"></div>
<div class="slide3"></div>
</div>
Change the left/top properties to reposition it.
There are two images, first is the boat, second the plane. The desired result is: Boat animates from left to right, at that time the plane is hidden. When the boat reaches the middle of the screen it disappears and the plane appears. This change should happen smoothly.
.image1 {
width: 259px;
height: 259px;
display: block;
position: absolute;
bottom: 135px;
margin: auto;
#include transition(all 1.2s);
background-size: contain;
-webkit-animation: helicopter-move-one 19s linear infinite;
animation: helicopter-move-one 19s linear infinite;
opacity: 1;
}
#-webkit-keyframes helicopter-move-one {
0% {
left: -300px;
}
60% {
opacity: 0;
}
100% {
left: 110%;
}
}
#keyframes helicopter-move-one {
0% {
left: -300px;
display: block;
}
59% {
display: none;
}
60% {
display: none;
}
100% {
left: 110%;
}
}
<div class="outer">
<div class="image1"><img src="" alt="boat"></div>
<div class="image2"><img src="" alt="plane"></div>
</div>
Since I don't have your images I'm using dogs. In this case "The desired result is: adult dog animates from left to right, at that time the puppy is hidden. When adult dog reaches the middle of the screen it disappears and the puppy appears. This change should happen smoothly." Please note that display is not animatable. You need to animate the opacity instead.
img {
width: 150px;
height: 150px;
}
[class ^="image"] {
width: 150px;
height: 150px;
display: block;
position: absolute;
left: 0;
right: 0;
background-size: contain;
}
.image1 {
z-index: 2;
animation: daAnimation1 19s linear infinite;
}
.image2 {
z-index: 1;
margin: auto;
left: 0;
right: 0;
opacity: 0;
animation: daAnimation2 19s linear infinite;
}
#keyframes daAnimation1 {
0% {
left: -150px;
opacity: 1;
}
45% {
left: calc(50vw - 75px);
opacity: 1;
}
50% {
left: calc(50vw - 75px);
opacity: 0;
}
100% {
left: 110%;
opacity: 0;
}
}
#keyframes daAnimation2 {
0% {
opacity: 0;
}
45% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 1;
}
}
<div class="outer">
<div class="image1"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg" alt="adult dog"></div>
<div class="image2"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppyBeagle300.jpg" alt="puppy"></div>
</div>
I hope this answers your question.
UPDATE:this is an answer to #Danish comment (see below)
img {
width: 150px;
height: 150px;
}
[class ^="image"] {
position:absolute;
background-size: contain;
}
.image1 {
z-index: 2;
opacity: 1;
animation: daAnimation1 19s linear infinite;
}
.image2 {
z-index: 1;
opacity: 1;
}
.outer{
width: 150px;
height: 150px;
display: block;
position: absolute;
animation: OuterAnimation 19s linear infinite;
}
#keyframes OuterAnimation{
0% {
left: -150px;
}
100% {
left: 110%;
}
}
#keyframes daAnimation1 {
0% {
opacity: 1;
}
45% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<div class="outer">
<div class="image1"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg" alt="adult dog"></div>
<div class="image2"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppyBeagle300.jpg" alt="puppy"></div>
</div>
I have a css fader, but it fades way to fast for anyone to read, and I cant seem to find a solution.
Once a image is fading, its also going 50% into the other image showing both images at 50%, how can I go around this so it only shows 1 image at the time?
Thanks!
There's always a problem with me and adding code somehow.. here's a codepen link.
HTML:-
<div class='slider'>
<img class="slide1" style="background: url(https://i.imgur.com/Jor6iZe.png)no-repeat center;" alt="">
<img id="img2" class="slide1" src="http://i.imgur.com/3N7tUT2.png">
<img class="slide2" style="background: url(https://i.imgur.com/h797HTW.png)no-repeat center;" alt="">
<img id="img2" class="slide2" src="http://i.imgur.com/0GQZobp.png">
<img class="slide3" style="background: url(https://i.imgur.com/n04pyfC.png)no-repeat center;" alt="">
<img id="img2" class="slide3" src="http://i.imgur.com/lfRhiqe.png">
</div>
CSS
.slider {
max-width: 1140px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,.slide2,.slide3,.slide4,.slide5 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
background-size: cover;
animation:fade 15s infinite;
-webkit-animation:fade 5s infinite;
}
.slide2 {
background-size: cover;
animation:fade2 15s infinite;
-webkit-animation:fade2 5s infinite;
}
.slide3 {
background-size: cover;
animation:fade3 15s infinite;
-webkit-animation:fade3 5s infinite;
}
#keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 15}
}
#keyframes fade2
{
0% {opacity:0}
33.333% { opacity: 15}
66.666% { opacity: 0 }
100% { opacity: 0}
}
#keyframes fade3
{
0% {opacity:0}
33.333% { opacity: 0}
66.666% { opacity: 15}
100% { opacity: 0}
}
Codepen Link
HTML
<div class='slider'>
<img class="slide1" style="background: url(https://i.imgur.com/Jor6iZe.png)no-repeat center / cover;" alt="">
<img class="slide2" style="background: url(https://i.imgur.com/h797HTW.png)no-repeat center / cover;" alt="">
<img class="slide3" style="background: url(https://i.imgur.com/n04pyfC.png)no-repeat center / cover;" alt="">
</div>
CSS
.slide1 {
animation:fade 10.5s infinite;
-webkit-animation:fade 10.5s infinite ;
}
.slide2 {
animation:fade1 10.5s infinite;
-webkit-animation:fade1 10.5s infinite;
}
.slide3 {
animation:fade2 10.5s infinite;
-webkit-animation:fade2 10.5s infinite;
}
#keyframes fade
{
0% { opacity: 1 }
28.57% { opacity: 1 }
30.95% { opacity: 0 }
97.61% { opacity: 0 }
100% { opacity: 1 }
}
#keyframes fade1
{
0% { opacity: 0}
30.95% { opacity: 0 }
33.33% { opacity: 1 }
61.9% { opacity: 1 }
64.28% { opacity: 0 }
100% { opacity: 0 }
}
#keyframes fade2
{
0% { opacity: 0}
64.28% { opacity: 0 }
66.66% { opacity: 1 }
95.23% { opacity: 1 }
97.61% { opacity: 0 }
100% { opacity: 0 }
}
I am working on a CSS only slider. However I don't have much experience with using keyframes.
I found this pen; could someone explain to me how the keyframes ensure that the animation runs in a cyclic manner rather than at the same time (where all the slides would disappear and reappear together)?
Code pen link
.slider {
max-width: 300px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,
.slide2,
.slide3,
.slide4,
.slide5 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
background-size: cover;
animation: fade 8s infinite;
-webkit-animation: fade 8s infinite;
}
.slide2 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
background-size: cover;
animation: fade2 8s infinite;
-webkit-animation: fade2 8s infinite;
}
.slide3 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
background-size: cover;
animation: fade3 8s infinite;
-webkit-animation: fade3 8s infinite;
}
#keyframes fade {
0% {
opacity: 1
}
33.333% {
opacity: 0
}
66.666% {
opacity: 0
}
100% {
opacity: 1
}
}
#keyframes fade2 {
0% {
opacity: 0
}
33.333% {
opacity: 1
}
66.666% {
opacity: 0
}
100% {
opacity: 0
}
}
#keyframes fade3 {
0% {
opacity: 0
}
33.333% {
opacity: 0
}
66.666% {
opacity: 1
}
100% {
opacity: 0
}
}
<div class='slider'>
<div class='slide1'></div>
<div class='slide2'></div>
<div class='slide3'></div>
</div>
This is due to the each slide using a different keyframe, i.e. slide1 uses fade, slide2 uses fade2 and slide3 uses fade3. These keyframes all last 8 seconds however the frame in which the slide is shown is different:
slide1 is shown at 0% (0 seconds)
slide2 is shown at 33.333% (about 2.6 seconds)
slide3 is shown at 66.666% (about 5.3 seconds)
This particular method will work when you have three slides but would need to be adapted if you were to have a different amount. For example with four you would need to add an extra step to the keyframe:
.slider {
max-width: 300px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,
.slide2,
.slide3,
.slide4,
.slide5 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
background-size: cover;
animation: fade 8s infinite;
-webkit-animation: fade 8s infinite;
}
.slide2 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
background-size: cover;
animation: fade2 8s infinite;
-webkit-animation: fade2 8s infinite;
}
.slide3 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
background-size: cover;
animation: fade3 8s infinite;
-webkit-animation: fade3 8s infinite;
}
.slide4 {
background: red;
background-size: cover;
animation: fade4 8s infinite;
-webkit-animation: fade4 8s infinite;
}
#keyframes fade {
0% {
opacity: 1
}
25% {
opacity: 0
}
50% {
opacity: 0
}
75% {
opacity: 0
}
100% {
opacity: 1
}
}
#keyframes fade2 {
0% {
opacity: 0
}
25% {
opacity: 1
}
50% {
opacity: 0
}
75% {
opacity: 0
}
100% {
opacity: 0
}
}
#keyframes fade3 {
0% {
opacity: 0
}
25% {
opacity: 0
}
50% {
opacity: 1
}
75% {
opacity: 0
}
100% {
opacity: 0
}
}
#keyframes fade4 {
0% {
opacity: 0
}
25% {
opacity: 0
}
50% {
opacity: 0
}
75% {
opacity: 1
}
100% {
opacity: 0
}
}
<div class='slider'>
<div class='slide1'></div>
<div class='slide2'></div>
<div class='slide3'></div>
<div class='slide4'></div>
</div>
As suggested by #ILoveCSS this code can be shortened to just the one keyframe animation by adding a third property to the animation property. This value is the animation-delay property and will stall the animation by the specified time:
.slider {
max-width: 300px;
height: 200px;
margin: 20px auto;
position: relative;
}
.slide1,.slide2,.slide3,.slide4,.slide5 {
position: absolute;
width: 100%;
height: 100%;
}
.slide1 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
background-size: cover;
animation:fade 8s infinite;
-webkit-animation:fade 8s infinite;
}
.slide2 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371565525_p17tbqpu0d69c21hetd77dh483.jpeg)no-repeat center;
background-size: cover;
animation:fade 8s infinite 2.6s;
-webkit-animation:fade 8s infinite 2.6s;
}
.slide3 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371564896_p17tbq6n86jdo3ishhta3fv1i3.jpg)no-repeat center;
background-size: cover;
animation:fade 8s infinite 5.3s;
-webkit-animation:fade 8s infinite 5.3s;
}
#keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 1}
}
<div class='slider'>
<div class='slide3'></div>
<div class='slide2'></div>
<div class='slide1'></div>
</div>
I think what he is trying is:
#keyframes fade
{
0% {opacity:1}
33.333% { opacity: 0}
66.666% { opacity: 0}
100% { opacity: 1}
}
in this case slide1 is getting visible at the start of the animation and stops when it reached 33.333% and 66.666% and starts to visible at the end of the animation which is 100%.
#keyframes fade2
{
0% {opacity:0}
33.333% { opacity: 1}
66.666% { opacity: 0 }
100% { opacity: 0}
}
in the second slide2 at the start of the animation is not showing because slide1 is in the playing of it's animation and when slide1 reaches 33.333% started to stop and slide2 will start to fadeIn.
#keyframes fade3
{
0% {opacity:0}
33.333% { opacity: 0}
66.666% { opacity: 1}
100% { opacity: 0}
}
in the third slide3 the animation start to fadeIn at 66.666% because at this percent slide2 is in fadeOut state and it continue like this infinitely...
Hope you have an idea.