Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Tuesday, February 24, 2015

HTML5 CSS3 Scrolling Slideshow | HTML 5 & CSS 3 - Smooth Scrolling

Smooth scrolling slideshow using pure CSS3 code

In this article, I'm going to demo how to implement image slideshow using
  1. CSS2 and Javascript
  2. CSS3
And for CSS3 approach, I'll show how to implement
  1. Scrolling up
  2. Scrolling up and down
If you move the mouse over and out the slideshow, it will be paused and resumed accordingly.
Keypoints in the demo includes:
  • setInterval and clearInterval
  • transform and translateY
  • calc
  • animation including animation-play-state
To simplify the example, all the images here are standard AD sidebar images (300px * 250px).

1. CSS2 + Javascript

Sponsors
<style type='text/css'>
 	#ad_container {
		margin-bottom: 10px;
		padding: 0;
		height: 304px;
		width: 304px;
		border: 2px solid rgb(111, 126, 255);
		box-shadow: 2px 2px 10px rgb(111, 126, 255);
		border-radius: 15px 15px 0 0;
		overflow: hidden;
	}
	#ad_title {
		margin: 0;
		padding: 0;
		height: 50px;
		line-height: 50px;
		font-size: 24px;
		color: white;
		text-align: center;
		background: rgb(111, 126, 255); 
	}
	#ad_content {
		margin: 5px auto;
		padding: 0;
		height: 250px;
		width: 300px;
		overflow: hidden;
	}

	#ad_content1, #ad_content2 {
		margin: 0;
		padding: 0;
		list-style-type: none;
	}
</style>
<div id='ad_container'>
		<div id='ad_title'>Sponsors</div>
		<div id='ad_content'>
			<ul id='ad_content1'>
			<!-- MYADS CJ Disney -->
				<li><a href='http://www.kqzyfj.com/click-7744766-11138523' target='_top'>
	<img alt='' border='0' height='250' src='http://www.tqlkg.com/image-7744766-11138523' width='300'/></a></li>
			<!-- MYADS CJ Web Hosting -->
				<li><a href='http://www.anrdoezrs.net/click-7744766-11793715' target='_top'>
	<img alt='' border='0' height='250' src='http://www.awltovhc.com/image-7744766-11793715' width='300'/></a></li>
			<!-- MYADS CJ Hotel -->
				<li><a href='http://www.jdoqocy.com/click-7744766-10691018' target='_top'> <img alt='Hotels.com' border='0' height='250' src='http://www.tqlkg.com/image-7744766-10691018' width='300'/></a></li>
			</ul>
			<ul id='ad_content2'/>
		</div>
</div>
<script>
	var ad_content = document.getElementById('ad_content');
	ad_content.scrollTop = 0;

	var ad_content1 = document.getElementById('ad_content1');
	var ad_content2 = document.getElementById('ad_content2');
	ad_content2.innerHTML = ad_content1.innerHTML;

	function scrollUp() {

		if (ad_content.scrollTop >= ad_content1.offsetHeight)
		{
			ad_content.scrollTop = '5px';
		}
		else
		{
			ad_content.scrollTop ++;
		}
	}
	
	var speed = 30;
	var myScroll = setInterval('scrollUp()', speed);
	
	ad_content.onmouseover = function(){
		clearInterval(myScroll);
	}
	ad_content.onmouseout = function(){
		myScroll = setInterval("scrollUp()", speed);
	}
</script>
To make it a smoothing sliding, we need to clone the content of ad_content1 to ad_content2, this is achieved by ad_content2.innerHTML = ad_content1.innerHTML in the Javascript.

2. CSS3

Sponsors
<style type='text/css'>
	#ad_container {
		margin: 100px;
		padding: 0;
		height: 304px;
		width: 304px;
		border: 2px solid rgba(111, 126, 255, 0.9);
		box-shadow: 2px 2px 10px rgba(111, 126, 255, 0.7);
		border-radius: 15px 15px 0 0;
		overflow: hidden;
	}
	#ad_title {
		padding: 0;
		height: 50px;
		line-height: 50px;
		font-size: 24px;
		color: white;
		text-align: center;
		background: rgba(111, 126, 255, 0.8); 
	}

	@-webkit-keyframes scroll-up {
		0%   {-webkit-transform: translateY(0);}
	    /* the height of each image is 250px, so 4 images have the height of 1000px */
	    100% {-webkit-transform: translateY(-75%);}
	}

	@keyframes scroll-up {
		0%   {transform: translateY(0);}
	    /* the height of each image is 250px, so 4 images have the height of 1000px */
	    100% {transform: translateY(-75%);}
	}

	#ad_content {
		margin: 5px auto;
		padding: 0;
		height: 250px;
		width: 300px;
		overflow: hidden;
	}
	#ad_content1 {
		margin: 0;
		padding: 0;
		list-style-type: none;
		-webkit-animation: scroll-up ease-in-out 10s infinite;
		animation: scroll-up ease-in-out 10s infinite;
	}
	#ad_content1:hover {
		-webkit-animation-play-state: paused;
		animation-play-state: paused;
	}
	#ad_content1 li {
		margin: 0 auto;
		padding: 0;
	}
</style>
<div id="ad_container">
		<div id="ad_title">Sponsors</div>
		<div id="ad_content">
			<ul id="ad_content1">
			<!-- MYADS CJ Disney -->
				<li><a href='http://www.kqzyfj.com/click-7744766-11138523' target='_top'>
	<img alt='' border='0' height='250' src='http://www.tqlkg.com/image-7744766-11138523' width='300'/></a></li>
			<!-- MYADS CJ Web Hosting -->
				<li><a href='http://www.anrdoezrs.net/click-7744766-11793715' target='_top'>
	<img alt='' border='0' height='250' src='http://www.awltovhc.com/image-7744766-11793715' width='300'/></a></li>
			<!-- MYADS CJ Hotel -->
				<li><a href='http://www.jdoqocy.com/click-7744766-10691018' target='_top'> <img alt='Hotels.com' border='0' height='250' src='http://www.tqlkg.com/image-7744766-10691018' width='300'/></a></li>
			<!-- repeat the first image for seamless scroll-up, remove this for scroll-up up and down -->
			<!-- MYADS CJ Disney -->
				<li><a href='http://www.kqzyfj.com/click-7744766-11138523' target='_top'>
	<img alt='' border='0' height='250' src='http://www.tqlkg.com/image-7744766-11138523' width='300'/></a></li>
			</ul>
			<ul id="ad_content2"></ul>
		</div>
</div>
CSS3 animation makes it eaiser to implement a smooth image scrolling. Moveover, since animation supports different timing functions, like linear and ease, you can create different animation effects. You can also combine other properties such as opacity in the animation.

The downside for the pure CSS3 solution is that we have to clone the first image and append it to the end manually in our HTML structure.

Another issue in this approach is that we hardcoded the translate offset in the CSS definition, i.e., 100% {transform: translateY(-75%);}. This can be solved by using a CSS3 function called calc(). So the above CSS can be replaced with 100% {transform: translateY(calc(250px - 100%));}. But do check the compatibility table for this function since it is quite new.

3. CSS3 (Scrolling up and down)

Sponsors
<style type='text/css'>
	#ad_container {
		margin: 100px;
		padding: 0;
		height: 304px;
		width: 304px;
		border: 2px solid rgba(111, 126, 255, 0.9);
		box-shadow: 2px 2px 10px rgba(111, 126, 255, 0.7);
		border-radius: 15px 15px 0 0;
		overflow: hidden;
	}
	#ad_container >#ad_title {
		padding: 0;
		height: 50px;
		width: 100%;
		line-height: 50px;
		font-size: 24px;
		color: white;
		text-align: center;
		background: rgba(111, 126, 255, 0.8); 
	}

	@-webkit-keyframes scroll-up-down {
	    0%   {transform: translateY(0);}
	    50% {transform: translateY(-webkit-calc(250px - 100%));}
	    100% {transform: translateY(0);}
	}

	@keyframes scroll-up-down {
	    0%   {transform: translateY(0);}
	    50% {transform: translateY(calc(250px - 100%));}
	    100% {transform: translateY(0);}
	}

	#ad_container >#ad_content {
		margin: 5px auto;
		padding: 0;
		height: 250px;
		width: 300px;
		overflow: hidden;
	}
	#ad_container >#ad_content >#ad_content1 {
		margin: 0;
		padding: 0;
		list-style-type: none;
		-webkit-animation: scroll-up-down ease-in-out 10s infinite;
		animation: scroll-up-down ease-in-out 10s infinite;
	}
	#ad_container >#ad_content >#ad_content1:hover {
		-webkit-animation-play-state: paused;
		animation-play-state: paused;
	}
	#ad_container >#ad_content >#ad_content1 li {
		margin: 0 auto;
		padding: 0;
	}
</style>
<div id="ad_container">
		<div id="ad_title">Sponsors</div>
		<div id="ad_content">
			<ul id="ad_content1">
			<!-- MYADS CJ Disney -->
				<li><a href='http://www.kqzyfj.com/click-7744766-11138523' target='_top'>
	<img alt='' border='0' height='250' src='http://www.tqlkg.com/image-7744766-11138523' width='300'/></a></li>
			<!-- MYADS CJ Web Hosting -->
				<li><a href='http://www.anrdoezrs.net/click-7744766-11793715' target='_top'>
	<img alt='' border='0' height='250' src='http://www.awltovhc.com/image-7744766-11793715' width='300'/></a></li>
			<!-- MYADS CJ Hotel -->
				<li><a href='http://www.jdoqocy.com/click-7744766-10691018' target='_top'> <img alt='Hotels.com' border='0' height='250' src='http://www.tqlkg.com/image-7744766-10691018' width='300'/></a></li>
			</ul>
		</div>
</div>


Create a Successful Online Store at Bigcommerce! Try it Free Now!

Saturday, February 14, 2015

CSS Layout - Relative and Absolute Position

CSS Layout - Relative and Absolute

The position property is crucial to CSS layout. It has a couple of possible values. This article gives a simple example to help you understand the two most important values: relative and absolute.

CSS Sample

<style type="text/css">
.relative1 {
  position: relative;
  border: 1px solid red;
  color: red;
}
.relative2 {
  position: relative;
  top: -20px;
  left: 20px;
  width: 500px;
  border: 1px solid green;
  color: green;
}
.relative3 {
  position: relative;
  border: 1px solid orange;
  color: orange;
}
.absolute1 {
  position: absolute;
  top: -20px;
  left: 20px;
  width: 500px;
  border: 1px solid blue;
  color: blue;
}
.absolute2 {
  position: absolute;
  top: -20px;
  left: 20px;
  width: 500px;
  border: 1px solid black;
  color: black;
}
</style>
<div style="margin: 30px">
<div class="relative1"><p>relative1</p><p></p><p></p></div>
<div class="relative2"><p>relative2</p><p></p><p></p></div>
<div class="absolute1"><p>absolute1</p><p></p><p></p></div>
<div class="relative3"><p>relative3</p>
<div class="absolute2"><p>absolute2</p><p></p><p></p></div>
</div>
</div>


CSS Rules


There are two rules to remember:
  1. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. In the example above, relative2 should be under relative1, however, since we set its top and left properties, it goes higher and to the left of its normal position. 
  2. A absolutely-positioned element is positioned relative to the nearest positioned ancestor (container). If an absolutely-positioned element has no positioned ancestors, it uses the document body. In the example above, absolute2 use relative3 as ancestor and adjusts accordingly. While absolute1 does not have an ancestor, so it adjusts according to the document.


Create a Successful Online Store at Bigcommerce! Try it Free Now!