Pure CSS Slider - html

So I'm getting acquainted with css3 and I've been trying to find a purely css slider. I finally found one that is exactly as I was looking for on code pen but for some reason when I try the code in localhost or jsfiddle it doesn't work. There is no external files that its accessing as far as I can tell in codepen and there is no jQuery needed. Below I've linked the (working) codepen and jsfiddle. Any ideas why it wont work elsewhere?
codepen
jsFiddle
html
<div class="slider">
<img class='photo' src="http://i.imgur.com/zMGSiyl.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/soQhb13.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/39yOaYB.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/tnctKD4.jpg" alt="" />
</div>
css
body{background:#000;}
.slider{
margin:50px auto;
width:100%;
height:300px;
overflow:hidden;
position:relative;
}
.photo{
position:absolute;
animation:round 16s infinite;
opacity:0;
width:100%;
}
#keyframes round{
25%{opacity:1;}
40%{opacity:0;}
}
img:nth-child(4){animation-delay:0s;}
img:nth-child(3){animation-delay:4s;}
img:nth-child(2){animation-delay:8s;}
img:nth-child(1){animation-delay:12s;}

You may need to use vendor specific keyframes. Codepen is clever and is overcompensating in this instance.
#-webkit-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
#-moz-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
#-o-keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
#keyframes NAME-YOUR-ANIMATION {
0% { opacity: 0; }
100% { opacity: 1; }
}
More info http://css-tricks.com/snippets/css/keyframe-animation-syntax/

This works perfectly, please check: jsFiddle Demo. The syntax for the CSS3 animations and the keyframes which was being used in the code was the standard syntax, for e.g. animation:round 16s infinite;, #keyframes round{ 25%{opacity:1;} 40%{opacity:0;} } and img:nth-child(4){animation-delay:0s;}.
You should instead use -webkit-animation:round 16s infinite;`, `#-webkit-keyframes round{ 25%{opacity:1;} 40%{opacity:0;} } ` and `img:nth-child(4){-webkit-animation-delay:0s;} so that it's browser compatible.
More information on this is available over here.
body {
background: #000;
}
.slider {
margin: 50px auto;
width: 100%;
height: 300px;
overflow: hidden;
position: relative;
}
.photo {
position: absolute;
-webkit-animation: round 16s infinite;
-moz-animation: round 16s infinite;
-o-animation: round 16s infinite;
animation: round 16s infinite;
opacity: 0;
width: 100%;
}
#-webkit-keyframes round {
25% {
opacity: 1;
}
40% {
opacity: 0;
}
}
#-moz-keyframes round {
25% {
opacity: 1;
}
40% {
opacity: 0;
}
}
#-o-keyframes round {
25% {
opacity: 1;
}
40% {
opacity: 0;
}
}
#keyframes round {
25% {
opacity: 1;
}
40% {
opacity: 0;
}
}
img:nth-child(4) {
-webkit-animation-delay: 0s;
}
img:nth-child(3) {
-webkit-animation-delay: 4s;
}
img:nth-child(2) {
-webkit-animation-delay: 8s;
}
img:nth-child(1) {
-webkit-animation-delay: 12s;
}
<div class="slider">
<img class='photo' src="http://i.imgur.com/zMGSiyl.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/soQhb13.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/39yOaYB.jpg" alt="" />
<img class='photo' src="http://i.imgur.com/tnctKD4.jpg" alt="" />
</div>

Related

How to constantly alternate two images

I'm trying to use CSS or jQuery to constantly switch between two images. What I have works ok, but it's essentially placing an image on top of the other one, which causes issues if the images I'm using are transparent.
section {
position: relative;
}
section img {
position: absolute;
width: 200px;
}
.top {
animation-name: fade;
animation-iteration-count: infinite;
animation-duration: 1s;
animation-direction: alternate;
}
#keyframes fade {
0% {
opacity: 1;
}
25% {
opacity: 1;
}
75% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<section>
<img class="bottom" src="https://placeimg.com/640/480/nature">
<img class="top" src="https://placeimg.com/640/480/arch">
</section>
Codepen: https://codepen.io/Rhys_Eng/pen/NWdwxaO
To solve the issue with transparent images overlaying each other you can use your current technique to fade out the underlying image as the new one is displayed over it. To do that add a 1 second delay to its animation. Try this:
section img {
position: absolute;
width: 200px;
animation: fade 1s infinite alternate;
}
.bottom {
animation-delay: 1s;
}
#keyframes fade {
0% { opacity: 1; }
25% { opacity: 1; }
75% { opacity: 0; }
100% { opacity: 0; }
}
<section>
<img class="bottom" src="https://placeimg.com/640/480/nature">
<img class="top" src="https://placeimg.com/640/480/arch">
</section>
If you wanted to change the image without fading, while still using CSS alone, then you can amend the keyframes to this:
#keyframes fade {
0% { opacity: 1; }
49% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 0; }
}

CSS3 Animation - Smooth Infinite Animation

I've made a small Image animation where images changes opacity over time.It works smoothly but when last image gets to 100% it jumps straight to 0% without any transition.
I have already tried animation-direction: alternate for third image and delay for all image but it does not work for me. Delay works only first step of animation cycle after it delay becomes 0 for all.
Here is my CSS
.rightside .img-container img.first {
animation-name: first-image;
animation-duration: 9s;
animation-fill-mode: both;
animation-iteration-count: infinite;
/* animation-delay: -10s; */
}
.rightside .img-container img.second {
position: absolute;
top: 0;
animation-name: second-image;
animation-duration: 9s;
animation-fill-mode: both;
animation-iteration-count: infinite;
}
.rightside .img-container img.third {
position: absolute;
top: 0;
animation-name: final-image;
animation-duration: 9s;
animation-fill-mode: both;
animation-iteration-count: infinite;
animation-timing-function: linear;
/* animation-direction: alternate; */
}
#keyframes first-image {
0% {
opacity: 0;
}
33.3% {
opacity: 1;
}
67% {
opacity: 0;
}
100% {
opacity: 0;
}
}
#keyframes second-image {
0% {
opacity: 0;
}
33.3% {
opacity: 0;
}
67% {
opacity: 1;
}
100% {
opacity: 0;
}
}
#keyframes final-image {
0% {
opacity: 0;
}
33.3% {
opacity: 0;
}
67% {
opacity: 0;
}
100% {
opacity: 1;
}
}
HTML
<div class="img-container">
<img src="Images/Apple.png" class="first turn" alt="Image Here" />
<img src="Images/Bee.png" class="second" alt="Image Here" />
<img src="Images/Cat.png" class="third" alt="Image Here" />
</div>
The clasical aproach to this would be just using different delays:
div {
animation-name: all;
animation-duration: 9s;
animation-iteration-count: infinite;
width: 100px;
height: 100px;
background-color: yellow;
}
.first {
animation-delay: -3s;
background-color: lightgreen;
}
.third {
animation-delay: -6s;
background-color: lightblue;
}
#keyframes all {
0% {
opacity: 0;
}
33.3% {
opacity: 1;
}
67% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>

How to add delay on image fade and only make one image visible at the time?e

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 }
}

CSS Animation not running in Webkit based browsers

I want to toggle one image with another and vice-versa continuously with some time delay. This is not working in Webkit browsers such as Chrome and Safari.
Here's what I'm doing:
.bkgd_img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.top {
animation-name: toggle;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-duration: 5s;
animation-direction: alternate;
}
#keyframes toggle {
0% {
opacity: 1;
}
45% {
opacity: 1;
}
55% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<img class="bottom bkgd_img" src="http://www.placehold.it/500/FF0000" />
<img class="top bkgd_img" src="http://www.placehold.it/500/FF9900" />
The problem that I'm getting is that the "top" image never becomes transparent, the animation does not happen. Where am I going wrong?
Do I need to use browser prefixes for CSS3 Animation in Webkit based browsers?
Yes, the -webkit- prefix is still required currently.
Take a look at this reference here — Currently Chrome, Safari and Opera require the -webkit- prefix in order to support Keyframe Animations.
A note to future readers — This will change in the future as browser vendors adapt the native animation properties. Ensure that the non-prefixed animation property is also used underneath the webkit prefix.
Complete Example
Note: The non prefixed property should be placed underneath the -webkit- prefix. This ensures that supporting browsers will use the native CSS property.
The animation properties have been condensed into one: animation: toggle 5s ease-in-out infinite alternate
.bkgd_img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.top {
-webkit-animation: toggle 5s ease-in-out infinite alternate;
animation: toggle 5s ease-in-out infinite alternate;
}
#-webkit-keyframes toggle {
0% {
opacity: 1;
}
45% {
opacity: 1;
}
55% {
opacity: 0;
}
100% {
opacity: 0;
}
}
#keyframes toggle {
0% {
opacity: 1;
}
45% {
opacity: 1;
}
55% {
opacity: 0;
}
100% {
opacity: 0;
}
}
<img class="bottom bkgd_img" src="http://www.placehold.it/500/FF0000" />
<img class="top bkgd_img" src="http://www.placehold.it/500/FFFF00" />
tested your code in Firefox 32 and IE10 and Chrome 36. It seems to work fine with IE and Mozilla. But does not work well with chrome. Chrome has different CSS notations, it is not able to read them, kindly use following code for chrome. You can retain your previous code for other browsers
.top {
-webkit-animation-name:toggle;
animation-name: toggle;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-iteration-count: infinite;
-webkit-animation-duration: 5s;
-webkit-animation-direction: alternate;
}
#-webkit-keyframes toggle {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}
You missed vendor specific css property.
-webkit-animation: toggle 5s infinite;
#-webkit-keyframes toggle {}
Refer the working code.
http://codepen.io/bhuvana/pen/dPYzdZ
If you want to use JQuery with a simple way of doing this. There are other ways of doing it example: toggle()
HTML:
<img class="bottom bkgd_img" src="xyz.jpg" id="img-change" />
JQuery:
$('#img-change').on({
'click': function () {
var originalsrc = $(this).attr('src');
var src = '';
if (originalsrc == 'xyz.jpg') src = 'abc.jpg';
if (originalsrc == 'abc.jpg') src = 'xyz.jpg';
$(this).attr('src', src);
}
});
Note: I didn't try it, but it probably works.
You have to use vendor specific code, you can check it in below code
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style>
.bkgd_img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.top {
-webkit-animation: 5s 'toggle' infinite alternate;
-moz-animation: 5s 'toggle' infinite alternate;
-o-animation: 5s 'toggle' infinite alternate;
animation: 5s 'toggle' infinite alternate;
}
#keyframes 'toggle' {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}#-webkit-keyframes 'toggle' {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}#-moz-keyframes 'toggle' {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}#-o-keyframes 'toggle' {
0% {
opacity:1;
}
45% {
opacity:1;
}
55% {
opacity:0;
}
100% {
opacity:0;
}
}
</style>
</head>
<body>
<img class="bottom bkgd_img" src="xyz.jpg" />
<img class="top bkgd_img" src="abc.jpg" />
</body>
</html>

Crossfade two images repeatedly after showing for 10 seconds

I'm trying to use HTML and CSS to crossfade two images after showing them for 10 seconds each. I want this to repeat constantly.
Here is my HTML:
<div id="container">
<img class="bottom" src="1.png">
<img class="top" src="2.png">
</div>
CSS:
#container {
float: right;
height: 246px;
position:relative;
width: 230px;
}
#container img {
height: 246px;
width: 230px;
left:0;
opacity: 0;
position:absolute;
}
#container img.bottom {
opacity: 1;
}
#container img.top {
animation-duration: 0.1s;
animation-name: crossFade;
animation-iteration-count: infinite;
animation-direction: alternate;
}
#keyframes crossFade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
I've never used CSS animations before so I'm a bit confused. Only the "bottom" image is being shown and nothing else happens.
What is going wrong?
Here is an example with 10s delay and 1s animation duration.
#container {
float: right;
height: 246px;
position: relative;
width: 230px;
}
#container img {
height: 246px;
width: 230px;
left: 0;
opacity: 0;
position: absolute;
}
#container img.bottom {
opacity: 1;
}
#container img.top {
-webkit-animation: crossFade 11s infinite;
animation: crossFade 11s infinite;
-webkit-animation-direction: alternate;
animation-direction: alternate;
}
#-webkit-keyframes crossFade {
0% {
opacity: 0;
}
47.62% {
opacity: 0;
}
52.38% {
opacity: 1;
}
100% {
opacity: 1;
}
}
#keyframes crossFade {
0% {
opacity: 0;
}
47.62% {
opacity: 0;
}
52.38% {
opacity: 1;
}
100% {
opacity: 1;
}
}
<div id="container">
<img class="bottom" src="https://dummyimage.com/200x200/404/fff">
<img class="top" src="https://dummyimage.com/200x200/101/fff">
</div>