<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Slider Archives : vedmant.com : coding blog</title>
	<atom:link href="https://vedmant.com/tag/slider/feed/" rel="self" type="application/rss+xml" />
	<link>https://vedmant.com/tag/slider/</link>
	<description>Sharing my personal experience in web development</description>
	<lastBuildDate>Sun, 05 Jul 2015 12:15:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>Image slider on pure CSS3</title>
		<link>https://vedmant.com/image-slider-on-pure-css3/</link>
					<comments>https://vedmant.com/image-slider-on-pure-css3/#comments</comments>
		
		<dc:creator><![CDATA[vedmant]]></dc:creator>
		<pubDate>Thu, 05 Mar 2015 19:02:16 +0000</pubDate>
				<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Slider]]></category>
		<guid isPermaLink="false">https://vedmant.com/?p=19</guid>

					<description><![CDATA[<p>Recently I&#8217;ve read one article about slider without any JS code and I decided to extend this idea and here is what i&#8217;ve got. First, we will need basic HTML layout for slider: Quite simple and straightforward. Next step, lets style our sliders container, and add some other styles: The whole idea of this slider &#8230; <a href="https://vedmant.com/image-slider-on-pure-css3/" class="more-link">Continue reading <span class="screen-reader-text">Image slider on pure CSS3</span></a></p>
<p>The post <a href="https://vedmant.com/image-slider-on-pure-css3/">Image slider on pure CSS3</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Recently I&#8217;ve read one article about slider without any JS code and I decided to extend this idea and here is what i&#8217;ve got.</p>
<p>First, we will need basic HTML layout for slider:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ul class=&quot;css-slider&quot;&gt;
    &lt;li&gt;&lt;img src=&quot;slides/1.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src=&quot;slides/2.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src=&quot;slides/3.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src=&quot;slides/4.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
    &lt;li&gt;&lt;img src=&quot;slides/5.jpg&quot; alt=&quot;&quot;&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>Quite simple and straightforward.</p>
<p>Next step, lets style our sliders container, and add some other styles:</p>
<pre class="brush: css; title: ; notranslate">
.css-slider{
   position: relative; /* All slides will have absolute position */
   width: 100%; /* Make it responsive */
   padding: 0; margin: 0; 
   padding-bottom: 75%; /* Height will be 75% from width */
   list-style-type: none; 
   overflow: hidden; /* In case images will have wrong aspect ratio */
}

.css-slider li img{width: 100%;} /* And make images responsive as well */
</pre>
<p>The whole idea of this slider is to use keyframe animation for changing slides. </p>
<p>To make this work we will create keyframes that have to do following: show slide, keep it some time, hide slide, wait while all other slides will show.</p>
<p>This is how it looks:</p>
<pre class="brush: css; title: ; notranslate">
@-webkit-keyframes css-slider {
  10%{opacity: 1;} /* This is where slide becomes visible, duration 10 % of 20 sec */
  20%{opacity: 1;} /* Still visible, little more then middle point (100 % / 5 slides) */
  40%{opacity: 0;} /* Now its hidden, when next slide is fully visible */
}
</pre>
<p>Some explanations about percents and durations. </p>
<p>Our slide has 5 slides, delay between slides is 4s, this means that whole animation have to take 5 slides * 4s = 20s.</p>
<p>Then we can calculate how long will take 1%, 20s / 100% = 0.2s, or 10% will take 2s and this is our transition duration from opacity: 0 to opacity: 1. </p>
<p>Next step to get middle point: 4s / 0.2s = 20 % or just 100% / 5 slides = 20%, this is when next slide will start it&#8217;s transition, and current slide have to stay visible until that.</p>
<p>At last we have to hide our slide when next slide is fully shown, it&#8217;s the middle of next slide, 20% + 20% = 40%.</p>
<p>Then we add styles and keyframes to slides:</p>
<pre class="brush: css; title: ; notranslate">
.css-slider li{
   position: absolute; top:0; right: 0; bottom: 0; left: 0; /* Make it fill whole slider container */
   opacity: 0; /* Hide it */
   -webkit-animation: css-slider 20s infinite; /* Use keyframe animation from above */
}
</pre>
<p>And at last we set different delays for each slide to make it work. Each slide must have slides delay durations multiplied by slide position &#8211; 1, or just each next slide&#8217;s delay equals previous plus delay duration.</p>
<pre class="brush: css; title: ; notranslate">
.css-slider li:nth-child(1){-webkit-animation-delay: 0s;}
.css-slider li:nth-child(2){-webkit-animation-delay: 4s;}
.css-slider li:nth-child(3){-webkit-animation-delay: 8s;}
.css-slider li:nth-child(4){-webkit-animation-delay: 12s;}
.css-slider li:nth-child(5){-webkit-animation-delay: 16s;}
</pre>
<p>And here is result:</p>

<style>
.css-slider{
   position: relative; /* All slides will have absolute position */
   width: 100%; /* Make it responsive */
   padding: 0; margin: 0; 
   padding-bottom: 75%; /* Height will be 75% from width */
   list-style-type: none; 
   overflow: hidden; /* In case images will have wrong aspect ratio */
}

.css-slider li img{width: 100%;} /* And make images responsive as well */

@-webkit-keyframes css-slider {
  10%{opacity: 1;} /* This is where slide becomes visible, duration 10 % of 20 sec */
  20%{opacity: 1;} /* Still visible, little more then middle point (100 % / 5 slides) */
  40%{opacity: 0;} /* Now its hidden, when next slide is fully visible */
}

.css-slider li{
   position: absolute; top:0; right: 0; bottom: 0; left: 0; /* Make it fill whole slider container */
   opacity: 0; /* Hide it */
   -webkit-animation: css-slider 20s infinite; /* Use keyframe animation from above */
}

.css-slider li:nth-child(1){-webkit-animation-delay: 0s;}
.css-slider li:nth-child(2){-webkit-animation-delay: 4s;}
.css-slider li:nth-child(3){-webkit-animation-delay: 8s;}
.css-slider li:nth-child(4){-webkit-animation-delay: 12s;}
.css-slider li:nth-child(5){-webkit-animation-delay: 16s;}
</style>

<ul class="css-slider">
    <li><img src="https://vedmant.com/wp-content/uploads/2015/03/slide1.jpg" alt=""></li>
    <li><img src="https://vedmant.com/wp-content/uploads/2015/03/slide2.jpg" alt=""></li>
    <li><img src="https://vedmant.com/wp-content/uploads/2015/03/slide3.jpg" alt=""></li>
    <li><img src="https://vedmant.com/wp-content/uploads/2015/03/slide4.jpg" alt=""></li>
    <li><img src="https://vedmant.com/wp-content/uploads/2015/03/slide5.jpg" alt=""></li>
</ul>

<p>&nbsp;</p>
<p>Ok it looks good, but it has a big con, if you want to change slides count, delays or transitions duration you have to recalculate keyframes positions change duration and add or remove delays for each slide. That&#8217;s why it&#8217;s not very usable in real production projects. </p>
<p>Until some generator will be used, I will add SASS generator here next time.</p>
<p>And here is promised SASS mixin, I&#8217;ve improved it a little by adding z-indexes:</p>
<pre class="brush: css; title: ; notranslate">

@mixin css-slider($count, $duration, $delay) {

	$delay: $duration + $delay;
	$total_duration: $delay * $count;
	$start_visible: $duration / $total_duration * 100;
	$mid_visible: 100 / $count;
	$end_visible: $mid_visible * 2;

	@-webkit-keyframes css-slider {
		#{$start_visible + '%'}{opacity: 1; z-index: 100;}
		#{$mid_visible + '%'}{opacity: 1; z-index: 2}
		#{$end_visible + '%'}{opacity: 0; z-index: 1;}
	}

	@keyframes css-slider {
		#{$start_visible + '%'}{opacity: 1; z-index: 100;}
		#{$mid_visible + '%'}{opacity: 1; z-index: 2}
		#{$end_visible + '%'}{opacity: 0; z-index: 1;}
	}


	position: relative;
	width: 100%; padding: 0 0 75% 0; margin: 0;
	list-style-type: none; overflow: hidden;

	li{
		position: absolute; top:0; right: 0; bottom: 0; left: 0; z-index: 1;
		opacity: 0;
		-webkit-animation: css-slider #{$delay * $count}s infinite;
		animation: css-slider #{$delay * $count}s infinite;
	}

	li img{width: 100%;}

	@for $i from 0 through $count - 1 {
		li:nth-child(#{$i+1}){-webkit-animation-delay: #{$i * $delay}s; animation-delay: #{$i * $delay}s;}
	}

}

/* Apply our minix for .css-slider */
.css-slider{
	@include css-slider(5, 2, 2);
}

</pre>
<p>The post <a href="https://vedmant.com/image-slider-on-pure-css3/">Image slider on pure CSS3</a> appeared first on <a href="https://vedmant.com">vedmant.com :: coding blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://vedmant.com/image-slider-on-pure-css3/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
