<?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>Design, Development, and Consulting &#187; CSS</title>
	<atom:link href="http://netlumination.com/blog/tag/css/feed" rel="self" type="application/rss+xml" />
	<link>http://netlumination.com</link>
	<description>A web design, development, and consulting firm based in Portland, Oregon helping small businesseses and individuals.</description>
	<lastBuildDate>Wed, 01 Sep 2010 23:27:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Big Button CSS Menu Links</title>
		<link>http://netlumination.com/blog/big-button-css-menu-links</link>
		<comments>http://netlumination.com/blog/big-button-css-menu-links#comments</comments>
		<pubDate>Sun, 30 May 2010 01:08:54 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[highlight]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[menu]]></category>

		<guid isPermaLink="false">http://netlumination.com/?p=1147</guid>
		<description><![CDATA[This post will show you how to make this: Home Gnome Rome Notice that the entire highlighted area is clickable, not just the lettering. I&#8217;ll describe a simple way to make pure CSS menu links that highlight nicely when hovered over. Now, you can obviously get a lot fancier with Javascript, but a good website [...]]]></description>
			<content:encoded><![CDATA[<p>This post will show you how to make this:</p>
<div id="da-body4">
<ul id="da-menu4">
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Home</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Gnome</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Rome</a></li>
</ul>
</div>
<p>Notice that the entire highlighted area is clickable, not just the lettering.<span id="more-1147"></span></p>
<p>I&#8217;ll describe a simple way to make pure CSS menu links that highlight nicely when hovered over. Now, you can obviously get a lot fancier with Javascript, but a good website design degrades nicely. This means that you should stack as much functionality into the (X)HTML and CSS as you can, and then add extras on with Javascript. This will enable you to read the page nicely through the widest range of media. This is important these days when you don&#8217;t know whether your visitors are using a cell phone, iPAD, curl on a UNIX shell, text reader for the vision impaired, or whatever. There are obviously some things Javascript is great for, but there&#8217;s no need to kill a fly with an elephant gun. Don&#8217;t use it when you don&#8217;t have to.</p>
<p>The <a href="http://www.w3.org/">W3C</a> page has a nice example of elegant page degradation when Javascript is turned off. The menu links on the left hand side of the page, for example the ones under standards, flash dark gray before settling to light gray when hovered over. The flash is Javascript. When you <a href="http://noscript.net/">turn Javascript off</a>, notice that the flash is gone, but the links are still highlighted in the same way. You can click anywhere in the Menu box to follow the link, not just over the lettering, and the entire menu box is highlighted in light gray.</p>
<p>Ok let&#8217;s build a similar but simpler vertical menu. Horizontal menus are easier to highlight, since the horizontal width of each box can vary without having the menu look unnatural.</p>
<p>Our menu will have three items: Home, Gnome, and Rome. It often makes sense to put your menu items in an unordered list, so we&#8217;ll do that, and we&#8217;ll set a fixed with for the items. We&#8217;ll use a 440 px body width and 220px menu width, just so you can easily see the results as displayed in this blog.</p>
<p>Ok, let&#8217;s set up the menu with simple highlighting using &#8220;a:hover&#8221;.</p>
<div id="da-body">
<ul id="da-menu">
<li><a href="http://peter-ajtai.com/examples/html/menu-bad.html"> Home</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-bad.html"> Gnome</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-bad.html"> Rome</a></li>
</ul>
</div>
<p>You can take a look at the menu above <a href="http://peter-ajtai.com/examples/html/menu1.html">on its own page</a>. Here is what that code looks like (remember to use style sheets; I used a style definition in head, so that all the code could be read easily in one go, but unless you like to individually and tediously change CSS rules one at a time for hundreds or thousands of pages, you&#8217;d better use external style sheets):</p>
<p><a href="http://peter-ajtai.com/examples/html/menu-bad.html">Bad Hover Menu</a></p>
<ol>
<li>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;</pre>
</li>
<li>
<pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;</pre>
</li>
<li>
<pre>    &lt;head&gt;</pre>
</li>
<li>
<pre>	&lt;style type="text/css"&gt;</pre>
</li>
<li>
<pre>	    #da-body {</pre>
</li>
<li>
<pre>	        background-color:#A6734A;</pre>
</li>
<li>
<pre>	        width:440px;</pre>
</li>
<li>
<pre>	    }</pre>
</li>
<li>
<pre>	    #da-menu {</pre>
</li>
<li>
<pre>		list-style-type:none;</pre>
</li>
<li>
<pre>		width:220px;</pre>
</li>
<li>
<pre>	    }</pre>
</li>
<li>
<pre>	    div#da-body ul#da-menu li a {</pre>
</li>
<li>
<pre>		font-size:180%;</pre>
</li>
<li>
<pre>		line-height:220%;</pre>
</li>
<li>
<pre>		color:#FAEAF3;</pre>
</li>
<li>
<pre>	        text-decoration:none;</pre>
</li>
<li>
<pre>	    }</pre>
</li>
<li>
<pre>	    div#da-body ul#da-menu li a:hover {</pre>
</li>
<li>
<pre>		background-color:#BFB6AF;</pre>
</li>
<li>
<pre>	    }</pre>
</li>
<li>
<pre>	&lt;/style&gt;</pre>
</li>
<li>
<pre>    &lt;/head&gt;</pre>
</li>
<li>
<pre>    &lt;body&gt;</pre>
</li>
<li>
<pre>	&lt;div id="da-body"&gt;</pre>
</li>
<li>
<pre>	    &lt;ul id="da-menu"&gt;</pre>
</li>
<li>
<pre>		&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>		&lt;li&gt;&lt;a href="#"&gt;Gnome&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>		&lt;li&gt;&lt;a href="#"&gt;Rome&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>	    &lt;/ul&gt;</pre>
</li>
<li>
<pre>	&lt;/div&gt;</pre>
</li>
<li>
<pre>    &lt;/body&gt;</pre>
</li>
<li>
<pre>&lt;/html&gt;</pre>
</li>
</ol>
<p>If you hover over a link, the background changes in color. Yeah, but it&#8217;s not very elegant, since the area that&#8217;s highlighted has a different shape for each link. Additionally, you have to be pretty precise with you mouse. The list takes up half the colored area, but you have to hover directly over letters in order to follow a link. Let&#8217;s deal with the first problem first.</p>
<p>It turns out that :hover is a valid pseudo class for a list item not just a. So if we change the a:hover code to li:hove like this:</p>
<ol>
<li>
<pre>div#da-body2 ul#da-menu2 li:hover {</pre>
</li>
<li>
<pre>    background-color:#BFB6AF;</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
<p>We&#8217;ll see this:</p>
<div id="da-body2">
<ul id="da-menu2">
<li><a href="http://peter-ajtai.com/examples/html/menu-blah.html"> Home</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-blah.html"> Gnome</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-blah.html"> Rome</a></li>
</ul>
</div>
<p>Now we&#8217;re getting close. The problem is that you cannot click the entire highlighted area to follow the link. This is because we&#8217;re highlighting the li element and not the a element. Ok, well how about:</p>
<ol>
<li>
<pre>/* THIS WILL NOT WORK!!! */</pre>
</li>
<li>
<pre>div#da-body2 ul#da-menu2 a {</pre>
</li>
<li>
<pre>    width:100%</pre>
</li>
<li>
<pre>}</pre>
</li>
<li>
<pre>div#da-body2 ul#da-menu2 a:hover {</pre>
</li>
<li>
<pre>    background-color:#BFB6AF;</pre>
</li>
<li>
<pre>}</pre>
</li>
<li>
<pre>/* ^^ INCORRECT */</pre>
</li>
</ol>
<p>Nope, that&#8217;ll basically just look like the first example. The problem is that the &#8220;a&#8221; element cannot have a width attribute. It&#8217;s not a block level element. You can&#8217;t set it&#8217;s width normally.</p>
<p>Don&#8217;t you wish we could simply display the anchor element like a block item, in this case like a list item, and then we could set its width. Well, CSS knows about inheritance, and there is a display attribute. We just have to force the &#8220;a&#8221; or anchor tag to act different than usual using, &#8220;display:inherit;&#8221;. This means that the &#8220;a&#8221; tag will inherit whatever display mode its parent element has. In this case it will will inherit the &#8220;block&#8221; display from the li element. So, our solution is:</p>
<ol>
<li>
<pre>div#da-body ul#da-menu a {</pre>
</li>
<li>
<pre>    display:inherit;</pre>
</li>
<li>
<pre>    width:100%</pre>
</li>
<li>
<pre>}</pre>
</li>
<li>
<pre>div#da-body ul#da-menu a:hover {</pre>
</li>
<li>
<pre>    background-color:#BFB6AF;</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
<p>This will produce:</p>
<div id="da-body3">
<ul id="da-menu3">
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Home</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Gnome</a></li>
<li><a href="http://peter-ajtai.com/examples/html/menu-good.html">Rome</a></li>
</ul>
</div>
<p>Incidentally, &#8220;display:block&#8221; will work too. To wrap up, here is the full code:</p>
<p><a href="http://peter-ajtai.com/examples/html/menu-good.html">Good Hover Menu</a>:</p>
<ol>
<li>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;</pre>
</li>
<li>
<pre>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;</pre>
</li>
<li>
<pre>    &lt;head&gt;</pre>
</li>
<li>
<pre>        &lt;style type="text/css"&gt;</pre>
</li>
<li>
<pre>            #page {</pre>
</li>
<li>
<pre>                background-color:#A6734A;</pre>
</li>
<li>
<pre>                width:440px;</pre>
</li>
<li>
<pre>            }</pre>
</li>
<li>
<pre>            #menu {</pre>
</li>
<li>
<pre>                list-style-type:none;</pre>
</li>
<li>
<pre>                width:220px;</pre>
</li>
<li>
<pre>                padding:0;</pre>
</li>
<li>
<pre>                margin:0;</pre>
</li>
<li>
<pre>            }</pre>
</li>
<li>
<pre>            div#page ul#menu li {</pre>
</li>
<li>
<pre>                padding:0;</pre>
</li>
<li>
<pre>                margin:0;</pre>
</li>
<li>
<pre>            }</pre>
</li>
<li>
<pre>            div#page ul#menu li a {</pre>
</li>
<li>
<pre>                display:inherit;</pre>
</li>
<li>
<pre>                width:100%;</pre>
</li>
<li>
<pre>                font-size:180%;</pre>
</li>
<li>
<pre>                line-height:220%;</pre>
</li>
<li>
<pre>                color:#FAEAF3;</pre>
</li>
<li>
<pre>                text-decoration:none;</pre>
</li>
<li>
<pre>                text-indent:0.5em;</pre>
</li>
<li>
<pre>            }</pre>
</li>
<li>
<pre>            div#page ul#menu li a:hover {</pre>
</li>
<li>
<pre>                background-color:#BFB6AF;</pre>
</li>
<li>
<pre>            }</pre>
</li>
<li>
<pre>        &lt;/style&gt;</pre>
</li>
<li>
<pre>    &lt;/head&gt;</pre>
</li>
<li>
<pre>    &lt;body&gt;</pre>
</li>
<li>
<pre>        &lt;div id="page"&gt;</pre>
</li>
<li>
<pre>            &lt;ul id="menu"&gt;</pre>
</li>
<li>
<pre>                &lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>                &lt;li&gt;&lt;a href="#"&gt;Gnome&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>                &lt;li&gt;&lt;a href="#"&gt;Rome&lt;/a&gt;&lt;/li&gt;</pre>
</li>
<li>
<pre>            &lt;/ul&gt;</pre>
</li>
<li>
<pre>        &lt;/div&gt;</pre>
</li>
<li>
<pre>    &lt;/body&gt;</pre>
</li>
<li>
<pre>&lt;/html&gt;</pre>
</li>
</ol>
<p>Incidentally, you can see that I&#8217;ve got a few margin:0s and padding:0s thrown in to make things look neater. Instead of dealing with zeroing the margins, paddings, etc. for each item one by one, you can simply use a <a href="http://meyerweb.com/eric/tools/css/reset/">CSS reset</a>. You can put this at the top of your style sheet, or add it in as a separate style sheet. I didn&#8217;t use a CSS reset for this example, since I wanted to keep the code as self contained as possible.</p>
<p>Ok, that&#8217;s all the hovering over CSS menus for today.</p>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/big-button-css-menu-links/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I Hope Your Inheritance Isn&#8217;t An EM</title>
		<link>http://netlumination.com/blog/i-hope-your-inheritance-isnt-an-em</link>
		<comments>http://netlumination.com/blog/i-hope-your-inheritance-isnt-an-em#comments</comments>
		<pubDate>Sun, 28 Mar 2010 19:55:28 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[ems]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[ratios]]></category>
		<category><![CDATA[sizing]]></category>

		<guid isPermaLink="false">http://netlumination.com/blog/i-hope-your-inheritance-isnt-an-em</guid>
		<description><![CDATA[The web allows users to customize their experience. This can be satisfying for the user and scary for the designer. Designers often have very specific ideas about how their web pages should look. Phrases like, &#8220;pixel perfect&#8221; come to mind. Well, &#8220;pixel perfect&#8221; font size is limiting to users who want to be able to [...]]]></description>
			<content:encoded><![CDATA[<p>The web allows users to customize their experience. This can be satisfying for the user and scary for the designer. Designers often have very specific ideas about how their web pages should look. Phrases like, &#8220;pixel perfect&#8221; come to mind. Well, &#8220;pixel perfect&#8221; font size is limiting to users who want to be able to control the size of their text.</p>
<p><span id="more-624"></span></p>
<p>The idea of using units that are dependent on font size instead of pixel size has been around for a very long time now, yet there are still many designers who don&#8217;t use ems. I believe it&#8217;s an inherited problem. In other words, setting the font-size to two ems in a parent div will give a different result than setting the font-size to two ems in a child div. This is because the em unit size is inherited through font-size.</p>
<p>Changing the font-size, changes the length of an em, even when it is used in the width or length css properties.</p>
<p>Changing the font-size in a div, means changing the size of an em relative to how big an em is in the parent div. This new em size will apply not only to font-size, but also to width, height, etc.</p>
<p>Let me demonstrate:</p>
<div style="background-color: #6a94d4; width: 20em; height: 20em; font-size: 2em; line-height: 1.2em; position: relative;">Parent Div:</p>
<p>Font Size: 2em</p>
<p>Width: 20em</p>
<p>Height: 20em</p>
<p>(Line Height: 1.2em)</p>
<div style="background-color: #e065bb; width: 20em; height: 20em; font-size: 0.5em; line-height: 1.2em; position: absolute; top: 0; right: 0;">Child Div:</p>
<p>Font Size: 0.5em</p>
<p>Width: 20em</p>
<p>Height: 20em</p>
<p>(Line Height: 1.2em)</p>
</div>
</div>
<p>Notice that the width, height, and line height values do not change from one div to the other. The only thing that changes is the ems.</p>
<p>The font size of the child div is 0.5 em. This means that the font size is simply 1/2 of whatever the parent&#8217;s font size is. The new 1/2 sized em units that are used to calculate the size of the letters are also used to calculate the size of the width, height, and line-height of the child div. This is why all of those are 1/2 the size of their parent div counter parts.</p>
<p>This is just a simple introduction to how working with EMs&#8230;&#8230;. works. You still have to do the math and deal with the defaults of browsers&#8230;. and IE. Here is some of the nitty gritty in the form of useful links:</p>
<ul>
<li><a href="http://riddle.pl/emcalc/">JavaScript EM calculator</a> by Piotr Petrus</li>
<li><a href="http://www.alistapart.com/articles/howtosizetextincss/">How to Size Text in CSS</a>, by Richard Rutter, from A List Apart</li>
<li><a href="http://www.w3.org/WAI/GL/css2em.htm">Old, slightly broken, but still useful article</a> on the W3C.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/i-hope-your-inheritance-isnt-an-em/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Simple Animations With Javascript</title>
		<link>http://netlumination.com/blog/creating-simple-animations-with-javascript</link>
		<comments>http://netlumination.com/blog/creating-simple-animations-with-javascript#comments</comments>
		<pubDate>Fri, 16 Oct 2009 04:49:21 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[timer]]></category>

		<guid isPermaLink="false">http://netlumination.com/?p=535</guid>
		<description><![CDATA[This is a basic JavaScript tutorial. I will describe how to move an element around the page using a simple animation script. The element you move around the page can be anything from an image to a paragraph. Below is a working example: // 200) { directionBall=-1; } if (0>distanceBall) { directionBall=1; } timerToggle=setTimeout(function() { [...]]]></description>
			<content:encoded><![CDATA[<p>This is a basic JavaScript tutorial. I will describe how to move an element around the page using a simple animation script. The element you move around the page can be anything from an image to a paragraph.</p>
<p>Below is a working example:</p>
<p><script type="text/javascript">// <![CDATA[
    var distanceBall=0; var directionBall=1; var timerToggle=null; function animateBall() {   document.getElementById("basketball").style.top=distanceBall + "px";   distanceBall+=directionBall;   if (distanceBall>200) { directionBall=-1; }
  if (0>distanceBall) { directionBall=1; }
  timerToggle=setTimeout(function() { animateBall(); },10);
}
function checkButton() {
  if (document.getElementById("ballButton").value=="Animate Basketball") {
    document.getElementById("ballButton").value="Stop Basketball";
    animateBall();
  } else {
    document.getElementById("ballButton").value="Animate Basketball";
    clearTimeout(timerToggle);
  }
}
// ]]&gt;</script></p>
<input id="ballButton" onclick="checkButton()" type="button" value="Animate Basketball" />
<div id="basketball" style="position: relative;"><img src="http://netlumination.com/wp-content/uploads/2009/10/Basketball_64x64-1.png" alt="Basketball" /></div>
<p><span id="more-535"></span></p>
<ol>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#Starting">Starting and stopping the animation using checkButton()</a></li>
<li><a href="#Moving">Moving the ball image using animateBall()</a></li>
<li><a href="#References">References</a></li>
<li><a href="#Conclusion">Conclusion</a></li>
</ol>
<h2><a name="Introduction"></a>Introduction</h2>
<p>Here is the code that makes the ball move. For the rest of this post, I&#8217;ll go over the basic ideas required to put the code together and how the specific code works:</p>
<ol>
<li>
<pre>&lt;script type="text/javascript"&gt;</pre>
</li>
<li>
<pre>// &lt;!--</pre>
</li>
<li>
<pre>    var distanceBall=0; </pre>
</li>
<li>
<pre>    var directionBall=1; </pre>
</li>
<li>
<pre>    var timerToggle=null; </pre>
</li>
<li>
<pre>    </pre>
</li>
<li>
<pre>    function animateBall() {   </pre>
</li>
<li>
<pre>        document.getElementById("basketball").style.top=distanceBall + "px";</pre>
</li>
<li>
<pre>        distanceBall+=directionBall;</pre>
</li>
<li>
<pre>        if (distanceBall&gt;200) { directionBall=-1; }</pre>
</li>
<li>
<pre>        if (0&gt;distanceBall) { directionBall=1; }</pre>
</li>
<li>
<pre>        timerToggle=setTimeout(function() { animateBall(); },10);</pre>
</li>
<li>
<pre>    }</pre>
</li>
<li>
<pre>    </pre>
</li>
<li>
<pre>    function checkButton() {</pre>
</li>
<li>
<pre>        if (document.getElementById("ballButton").value=="Animate Basketball") {</pre>
</li>
<li>
<pre>            document.getElementById("ballButton").value="Stop Basketball";</pre>
</li>
<li>
<pre>            animateBall();</pre>
</li>
<li>
<pre>        } else {</pre>
</li>
<li>
<pre>            document.getElementById("ballButton").value="Animate Basketball";</pre>
</li>
<li>
<pre>            clearTimeout(timerToggle);</pre>
</li>
<li>
<pre>        }</pre>
</li>
<li>
<pre>    }</pre>
</li>
<li>
<pre>// --&gt;</pre>
</li>
<li>
<pre>&lt;/script&gt;</pre>
</li>
<li>
<pre></pre>
</li>
<li>
<pre>&lt;input id="ballButton" onclick="checkButton()" type="button" value="Animate Basketball" /&gt;</pre>
</li>
<li>
<pre>&lt;div id="basketball" style="position: relative;"&gt;</pre>
</li>
<li>
<pre>    &lt;img src="http://netlumination.com/wp-content/uploads/2009/10/Basketball_64x64-1.png" alt="Basketball" /&gt;</pre>
</li>
<li>
<pre>&lt;/div&gt;</pre>
</li>
</ol>
<p><a href="http://jsfiddle.net/g3x9f/">Here&#8217;s a jsFiddle for you to play with.</a></p>
<p>There are two functions in this script, animateBall() and checkButton(). animateBall() is what makes the basketball image move. checkButton() starts and stops the ball. Let&#8217;s start by going over checkButton(), since that is the function that is called if the viewer presses the &#8220;Animate Basketball&#8221; button.</p>
<p><script type="text/javascript">// <![CDATA[
    google_ad_client = "pub-4166122661117213"; /* 336x280, created 4/27/09 */ google_ad_slot = "9673694336"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></p>
<h2><a name="Starting"></a>Starting and stopping the animation using checkButton()</h2>
<p>Whenever you create an animation, or any sort of constantly running JavaScript, it&#8217;s considerate to allow the viewer to stop the script somehow. This is done using the checkButton function. The checkButton function also starts the script running, so it serves a dual purpose.</p>
<ol>
<li>
<pre>function checkButton()</pre>
</li>
<li>
<pre>{</pre>
</li>
<li>
<pre>  if (document.getElementById(“ballButton”).value == ”Animate Basketball”) </pre>
</li>
<li>
<pre>  {    </pre>
</li>
<li>
<pre>    document.getElementById(“ballButton”).value = ”Stop Basketball”;</pre>
</li>
<li>
<pre>    animateBall();</pre>
</li>
<li>
<pre>  } else </pre>
</li>
<li>
<pre>  {</pre>
</li>
<li>
<pre>    document.getElementById(“ballButton”).value = ”Animate Basketball”;</pre>
</li>
<li>
<pre>    clearTimeout(timerToggle);</pre>
</li>
<li>
<pre>  }</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
<p>The checkButton function checks what the input button says and does  one of two things based upon what the button reads. Looking at the HTML of the INPUT button, you can see that the INPUT button has the ID of &#8220;ballButton.&#8221; So, we can use document.getElementById(”ballButton”).value to access the words on the button. Accessing  HTML elements, their attributes, and inline CSS styles on a web page with Javascript can be done using the Document Object Model (DOM). <a href="http://www.javascriptkit.com/domref/">Here</a> and <a href="http://www.w3schools.com/jsref/default.asp">here</a> are two easy to use references for the DOM.</p>
<p>checkButton() first replaces the words in the button. If the button reads, &#8220;Animate Basketball&#8221; it writes in &#8220;Stop Basketball&#8221; instead. We know the button can only read one of two things, so we use the <a href="http://www.w3schools.com/js/js_if_else.asp">ELSE statement</a> to set the INPUT BUTTON to &#8220;Animate Basketball&#8221; if it doesn&#8217;t already say that. So one of the things the IF, ELSE statement does is toggle the INPUT BUTTON between &#8220;Animate Basketball&#8221; and &#8220;Stop Basketball&#8221;</p>
<p>There is a second line after the writing on the button is changed. If the button read &#8220;Animate Basketball&#8221; then the function animateBall is called. This gets the ball moving.</p>
<p>If the button read &#8220;Stop Basketball,&#8221; the ELSE statement, then the ball is stopped using clearTimeout. I&#8217;ll go over how clearTimeout works later. For now it&#8217;s just important to understand that checkButton simply functions as a toggle that does two things whenever it&#8217;s called. First, it changes the writing on the INPUT button. Second, it either starts or stops the basketball image moving.</p>
<p>Let&#8217;s look at how the basketball image is moved when checkButton() calls the animateBall function.</p>
<h2><a name="Moving"></a>Moving the ball image using animateBall()</h2>
<p>This is the heart of the animation, and what&#8217;s startling is how few lines it takes to animate something with JavaScript.</p>
<p>Essentially what we want to do is move the DIV containing the basketball image small steps at a time. This requires two things: physical movement of the DIV and a time increment in which this movement will be done.</p>
<ol>
<li>
<pre>var distanceBall=0; </pre>
</li>
<li>
<pre>var directionBall=1; </pre>
</li>
<li>
<pre>var timerToggle=null; </pre>
</li>
<li>
<pre></pre>
</li>
<li>
<pre>function animateBall() {   </pre>
</li>
<li>
<pre>    document.getElementById("basketball").style.top=distanceBall + "px";</pre>
</li>
<li>
<pre>    distanceBall+=directionBall;</pre>
</li>
<li>
<pre>    if (distanceBall&gt;200) { directionBall=-1; }</pre>
</li>
<li>
<pre>    if (0&gt;distanceBall) { directionBall=1; }</pre>
</li>
<li>
<pre>    timerToggle=setTimeout(function() { animateBall(); },10);</pre>
</li>
<li>
<pre>}</pre>
</li>
<li>
<pre></pre>
</li>
</ol>
<p>If you look at the HTML, you&#8217;ll see that the basketball image is in a div with the ID &#8220;basketball&#8221; and the it&#8217;s CSS style of postion is set to RELATIVE. It&#8217;s important to set the position to RELATIVE, since that means that we can use the TOP or LEFT CSS styles to change the position of the DIV relative to where you see it when the image loads.</p>
<ol>
<li>
<pre>&lt;div id="basketball" style="position: relative;"&gt;</pre>
</li>
<li>
<pre>    &lt;img src="http://netlumination.com/wp-content/uploads/2009/10/Basketball_64x64-1.png" alt="Basketball" /&gt;</pre>
</li>
<li>
<pre>&lt;/div&gt;</pre>
</li>
</ol>
<p>There are three variables that the animateBall function uses. The first is distanceBall. distanceBall is how many pixels the TOP CSS property of the DIV with the basketball image in it is set to. So distanceBall will control where the image is displayed. directionBall will control whether the image is moving up or down. We need this so that the image doesn&#8217;t wander too far away. Finally the timerToggle variable will allow us to stop the ball from moving. This is done as a courtesy to the viewer.</p>
<p>The first line of the animateBall function adds directionBall to distanceBall. Initially directionBall is 1, so this simply adds 1 to distanceBall.</p>
<p>Next we display the DIV containing the basketball image at this new location. We use the HTML DOM objects to reference the DIV. We know the DIV has an ID of &#8220;basketball&#8221;. This makes it easy to pick out from all the other elements on the page by writing document.getElementById(&#8220;basketball&#8221;). Now to specifically target the CSS TOP style we&#8217;ll use document.getElementById(&#8220;basketball&#8221;).style.top. The top property can be read from the browser or it can be written to. In other words we can change it. It&#8217;s almost as simple as setting it equal to distanceBall. The only trick part is that TOP has units. We&#8217;ll use pixels, so me must concatenate &#8220;px&#8221; to the end of the number. We do this using the + <a href="http://jennifermadden.com/javascript/concatenation.html">string operator</a>.</p>
<p>So with just two lines of code we&#8217;ve moved the DIV one pixel. The next two lines, the two if statements, check if the DIV is at a TOP greater than 200 or less than 0. If it is, it reverses the direction of movement, so that the ball ends up bouncing up and down 200 pixels. Here is an alternative way to write those two lines as one line (to understand the codes look at this page on <a href="http://www.w3schools.com/JS/js_comparisons.asp">JavaScript comparison and logical operators</a>):</p>
<ol>
<li>
<pre>if(distanceBall&gt;200 || distanceBall&lt;0){directionBall*=-1;}</pre>
</li>
</ol>
<p>Anyway, this all looks great so far, but we&#8217;ve only moved the ball one pixel. We have to repeat this movement every few moments. This would be very easy if there was a function to pause a JavaScript program, but there isn&#8217;t, and trying to make a custom function to do this can end up with very messy results. There is, however, a way to tell JavaScript to do an action in a set amount of time. It&#8217;s like setting an alarm clock. It&#8217;s called setTimeout() and here is how it works:</p>
<ol>
<li>
<pre>setTimeout( [What to do] , [How long to wait] );</pre>
</li>
</ol>
<p>It&#8217;s important to realize that when you use setTimeout(), Javascript will keep evaluating the lines below setTimeout immediately. It does not pause. It&#8217;s like setting an alarm and keeping right on going, now with the alarm timer ticking in the background.</p>
<p>The way to use setTimeout in our animation function is to use it as the last line, and have it call our animation function itself. Like this:</p>
<ol>
<li>
<pre>function animateBall() {</pre>
</li>
<li>
<pre>    [ move ball one increment and show it at its new location ]</pre>
</li>
<li>
<pre>    setTimeout( function() { animateBall(); } , 10 );</pre>
</li>
<li>
<pre>}</pre>
</li>
</ol>
<p>This will perform the animateBall() function once every 10 milliseconds, since setTimeout measures time in milliseconds. It&#8217;s important that animateBall is not a for, while, or do loop, since setTimeout does not pause the execution of the script. Also, not the use of the anonymous function ( function () { &#8230; } ) in setTimeout. This is better than putting animateBall() in quotes and using eval().</p>
<p>Now that we have the ball going, how can we make it stop? The way to stop the animation and the ball is to somehow get setTimeout() to stop firing.</p>
<p>You&#8217;ll notice that the variable timerToggle is set equal to setTimeout() in the animateBall funciton. This may look confusing, since it&#8217;s not immediately apparent what sort of variable timerToggle is. Well, timerToggle simply allows us to keep track of the specific timer we used for the animation, so that we can stop it by using clearTimeout().</p>
<p>To stop a timer we&#8217;ll use clearTimeout(). The only thing we have to do is tell the browser which timer we want to stop. Luckily this is what the timerToggle variable is for, so clearTimeout(timerToggle) will stop the ball from moving.</p>
<p>clearTimeout() is not in the animateBall function, it is in the checkButton function. This is because clearTimeout is called if the viewer presses the button while the ball is moving.</p>
<p><script type="text/javascript">// <![CDATA[
    google_ad_client = "pub-4166122661117213"; /* 336x280, created 4/27/09 */ google_ad_slot = "9673694336"; google_ad_width = 336; google_ad_height = 280;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></p>
<h2><a name="Conclusion"></a>Conclusion</h2>
<p>Ok, so that&#8217;s it. You can animate a DIV on your page using one simple function, and you can stop and start the animation using a second function.</p>
<p>Remember that we are moving the DIV with the image inside. This means we could also put other things in the DIV like a paragraph, form, list, etc.</p>
<p>You should always consider the pros and cons of using animations on your page. As anyone who has sat through a few Power Point presentation knows, it is easy to go overboard with animations (and sound effects, but that&#8217;s another matter).</p>
<p>However, if you can link the animation to the content of your page, it may be well worth using it. For example, if you have a site about food, you could style the logo of your page to slide onto the page like a platter of food being served. Again, this can be done elegantly or it can be easily overdone. Additionally animation and interactivity (like the ability to start and stop the animation) is the seed of video games and interactive art.</p>
<p>Finally, you should consider how this simple function can be improved. You might have noticed that I used global variables. This limits the names of other variables that can be used on the same page, which can be especially problematic or confusing in large projects. So, removing the global variables would be ideal. This leads me to a second possible change. Instead of using an inline call to a javascript function in an HTML DIV, I could create an event handler for that DIV. This would allow me to get rid of the global variables too.</p>
<p>Exactly how to do that might be the subject for a later post.</p>
<h2><a name="References"></a>References</h2>
<p><a href="http://www.chipchapin.com/WebTools/JavaScript/exampleA01.html">Tutorial on JavaScript animation</a></p>
<p><a href="http://www.w3schools.com/jS/js_operators.asp">Javascript operators</a></p>
<p><a href="http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/CatalogJavascript-Methods.htm">Javascript methods</a></p>
<p><a href="http://www.javascriptkit.com/domref/index.shtml">Javascript DOM reference</a></p>
<p><a href="http://www.w3schools.com/jsref/default.asp">Javascript DOM reference</a></p>
<p><a href="http://www.iconspedia.com/icon/basketball-ball-438-.html">Basketball Image</a></p>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/creating-simple-animations-with-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Style guides</title>
		<link>http://netlumination.com/blog/style-guides</link>
		<comments>http://netlumination.com/blog/style-guides#comments</comments>
		<pubDate>Tue, 10 Jun 2008 17:12:21 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Website Advice]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[site maintenance]]></category>
		<category><![CDATA[upkeep]]></category>

		<guid isPermaLink="false">http://wordpress.netlumination.com/?p=27</guid>
		<description><![CDATA[One of the difficulties in having a web site designed for you is that you may not know what to look for. A way to get around this is to ask for explanations... in writing.]]></description>
			<content:encoded><![CDATA[<p>One of the difficulties in having a web site designed for you is that you may not know what to look for. A way to get around this is to ask for explanations&#8230; in writing.<span id="more-27"></span></p>
<p>For example, you may have a web designer come up with a look for your site. Make sure you ask for a written description of this look. This is called a style guide. A style guide may relate to the combination of colors on a site, each page&#8217;s layout, the feel of the writing on the site, or all of the previous. The style guide itself may be online, in electronic form, or even a physical booklet. The style guide will not only help you understand the work that is going in to developing your site, it is also a very valuable resource. A style guide allows someone who has never worked on you site before to edit your current pages and add new pages to your site while maintaining the look and feel of the overall website. This guarantees that your site will not only look good the first day it goes live, but that your site will continue to look good after years of edits and updates.</p>
]]></content:encoded>
			<wfw:commentRss>http://netlumination.com/blog/style-guides/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
