<?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>Beta Images Creative Design Studio Inc. &#187; CSS</title>
	<atom:link href="http://www.betaimages.com/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.betaimages.com</link>
	<description>Creative Design Studio specializing in Illustrated/Corporate Logos and Web Design</description>
	<lastBuildDate>Mon, 02 Jan 2012 15:24:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Tutorial: Create a sliding drawer effect with only CSS and jQuery</title>
		<link>http://www.betaimages.com/tips/tutorial-create-a-sliding-drawer-effect-with-only-css-and-jquery/</link>
		<comments>http://www.betaimages.com/tips/tutorial-create-a-sliding-drawer-effect-with-only-css-and-jquery/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 22:21:16 +0000</pubDate>
		<dc:creator>David Miller</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.betaimages.com/?p=2334</guid>
		<description><![CDATA[We recently launched Clothes Mates for Veronica McCain, an entrepreneur who developed an excellent product that allows you to quickly and easily manage your wardrobes. It&#8217;s excellent for those who like to stay organized or may be living in the fast lane. We built their entire brand from the ground up including their logo, packaging, [...]]]></description>
			<content:encoded><![CDATA[<img src="http://www.betaimages.com/wp-content/uploads/2010/02/slidingdrawers1.jpg" alt="" title="Create a sliding drawers animation with only css and jQuery" width="578" height="250" class="alignnone size-full wp-image-2388 service-img" />
<p>We recently launched <a title="Clothes Mates" href="http://www.clothesmates.com/">Clothes Mates</a> for Veronica McCain, an entrepreneur who developed an excellent product that allows you to quickly and easily manage your wardrobes. It&#8217;s excellent for those who like to stay organized or may be living in the fast lane. We built their entire brand from the ground up including their logo, packaging, and website. The works. One neat feature we included on the website was a dresser illustration on the sidebar of each page with sliding drawers. This effect could be created with Adobe Flash but I opted to use 100% CSS and jQuery.</p>
<p><span id="more-2334"></span></p>
<h2><a href="http://www.clothesmates.com/store/" title="See it in action">Click here to see it in action</a></h2>
<h3>Creating the image</h3>
<p>The dresser illustration is a drawing of a dresser with a flat front and two drawers on top of it that slide open. After you draw your dresser and drawers, we will be applying unique CSS to each piece. I drew both of these in Adobe Photoshop but the application used to design these doesn&#8217;t matter. What does matter is that you add a good 10px-15px piece of the inside of the drawer at the top of the drawer to allow for the sliding animation to work well. Test out the size of the drawer vs the dresser by placing two fully opened drawers on the top of the dresser to see how they will look together.</p>
<img src="http://www.betaimages.com/wp-content/uploads/2010/02/pieces3.jpg" alt="" title="pieces" width="578" height="250" class="alignnone size-full wp-image-2394" />
<h3>Markup and CSS</h3>
<p>First thing we want to do is setup the HTML markup for putting your dresser on the page. I opted for making the dresser a CSS background-image for the top of the sidebar as it is static and will always remain there on each page.</p>
<h4>HTML markup for the dresser</h4>
<pre class="brush: php;">
<div id="sidebartop"></div>
</pre>
<h4>CSS markup for the dresser</h4>
<pre class="brush: css;">#sidebartop {
	width:250px;
	float:right;	
	margin:0;
	height:272px;
	position:relative;
	padding:10px 20px;
	background:url(images/sidebartop.jpg) top left no-repeat;
}</pre>
<p>The formatting is fairly simple and relevant to the particular site I was setting this up at.<strong> The important piece of CSS here is &#8220;position:relative;&#8221;</strong>. By making the #sidebartop parent element have a relative position value, and child elements, such as the drawers, can have an absolute position value and that will allow us to set an exact top and left setting. The drawer positioning can also be achieved using margins, but it doesn&#8217;t work in this particularly example as it conflicts with the sliding effect. We&#8217;ll visit that in a bit.</p>

<p>Next we need to setup the drawers. I&#8217;ll give the code for one code as an example and explain how you would work in the second one. The HTML markup for the drawer should be contained within the the div container of the dresser. </p>
<h4>HTML markup for the drawers</h4>
<pre class="brush: css;">
<div id="sidebartop">
	<a title="Contact Me" href="contact" class="drawercontact" style="background-position: 0px -10px;">&nbsp;</a>
</div>
</pre>
<h4>CSS markup for the drawers</h4>
<pre class="brush: css;">.drawercontact {
	position:absolute;
	top:125px;
	left:19px;
	height:73px;
	width:243px;
	background:url(images/drawercontact.png) no-repeat;
	background-position:0 -10px;
	text-decoration:none;
}</pre>
<p><strong>The drawer CSS has two important pieces you need to make sure you have.</strong></p>
<ol><li><strong>First is the &#8220;absolute&#8221; value. </strong>Having an absolute positioned element within a relative positioned element allows you to use the Top and Left attributes to precisely position the child element. Here we have set the top value to 125px (the element is 125px away from the top of the parent div) and the left value to 19px (the element is 19px away from the left of the parent div).</li>
<li><strong>The second important CSS attribute is the background-position.</strong> I currently have the default background position for the drawer set to &#8220;0 -10px&#8221;. This means that by default, the background image is moved up (negative) 10 pixels. By setting the height of the .drawercontact class to full image height and setting the margin to -10px, it effectively hides the top 10px of the drawer which is the inside of the drawer. This will be revealed when we add the jQuery to slide the drawer out though! It&#8217;s important make sure the div is the height of the <strong>entire</strong> drawer, inside included, as we will just be adjusting the background position with jQuery so that you see everything.  </li>
</ol>
<img src="http://www.betaimages.com/wp-content/uploads/2010/02/height1.jpg" alt="" title="height" width="578" height="250" class="alignnone size-full wp-image-2372" />
<p>That is all the CSS you need to have your drawer properly positioned on the dresser with the inside of it hidden. The next step is the jQuery to allow a smooth transition from hidden to visible.</p>
<h3>jQuery</h3>
<p>The jQuery to make the smooth, Flash like opening animation on the drawers is very simple. We are setting a mouseover and mouseout state. 
<pre class="brush: js;">
<script type="text/javascript">
$(document).ready(function(){
	$('.drawercontact')
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				{duration:200})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 -10px)"}, 
				{duration:300})
			})		
	});
</script>
</pre>
<p><strong>That&#8217;s it! You are done!</strong> The jQuery tells the browser to animate the background position from &#8220;0 -10px&#8221; to &#8220;0 0&#8243; on mouseover. This reveals the top 10px of your drawer image, the inside of the drawer! on mouseout the background position is reset to &#8220;0 -10px&#8221;, it&#8217;s closed state. The duration attribute just says how long in milliseconds it will take to animate this effect. I opted for a slightly longer slide in animation so you could see it closing as you opened the next drawer.</p>
<p><strong>Quick Tip : By putting the &#8220;.stop()&#8221; command in front of the mouse out animation, we are telling jQuery to stop it&#8217;s current animation effect in mid execution and begin executing the mouse out function. Using .stop() on most hover related functions works WONDERS because it prevents your effects from queuing up when quickly hovered over again and again. </strong></p>

<p>Be sure to comment or share if you like the article! Feel free to ask any questions you may have.</p>
<h2><a href="http://www.clothesmates.com/store/" title="See it in action">Click here to see this effect in action</a></h2>]]></content:encoded>
			<wfw:commentRss>http://www.betaimages.com/tips/tutorial-create-a-sliding-drawer-effect-with-only-css-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

