<?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>Ashita.org &#187; CSS</title>
	<atom:link href="http://www.ashita.org/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashita.org</link>
	<description></description>
	<lastBuildDate>Wed, 27 Apr 2011 22:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript controls on filmstrip</title>
		<link>http://www.ashita.org/javascript-controls-on-filmstrip/</link>
		<comments>http://www.ashita.org/javascript-controls-on-filmstrip/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 12:35:55 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://ashita.org/?p=160</guid>
		<description><![CDATA[In this article we&#8217;re going to look at how to add some direction controls to our filmstrip. At the end of the last article we had the following filmstrip: Horizontal scrollbars in &#60;div&#62; tags don&#8217;t really play that nicely with scroll wheels or key controls unless you set the tabindex property. I&#8217;ve done so on [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we&#8217;re going to look at how to add some direction controls to our filmstrip. At the end of the last article we had the following filmstrip:</p>
<div class="filmstrip">
<ul class="imageRow">
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<p>Horizontal scrollbars in <code>&lt;div&gt;</code> tags don&#8217;t really play that nicely with scroll wheels or key controls unless you set the <code>tabindex</code> property. I&#8217;ve done so on the filmstrip above, and given it <code>tabindex="1"</code> so it&#8217;s the first thing you tab to. Just press left or right to test it out. And, although the scrollbars work well enough, let&#8217;s try something different. There are several ways to accomplish this, but some are distinctly better than others. I say better because of how well, or poorly, they play with browsers.</p>
<p>The method I describe below is doing nothing more than changing the display css-attribute. It also uses a couple of different ways to move the filmstrip images.</p>
<p>First let&#8217;s look at the events. Using the following for your onload function:</p>
<pre>function onLoad() {
	if (document.addEventListener) {
		document.getElementById('strip').addEventListener('keypress',HandleKeyPress,false);
		document.getElementById('strip').addEventListener('DOMMouseScroll',HandleWheel,false);
		document.getElementById('larrow').addEventListener('click',moveLeft,false);
		document.getElementById('rarrow').addEventListener('click',moveRight,false);
	} else {
		document.getElementById('strip').onkeypress = HandleKeyPress;
		document.getElementById('strip').onmousewheel = HandleWheel;
		document.getElementById('larrow').onclick = moveLeft;
		document.getElementById('rarrow').onclick = moveRight;
	}
	thumbNum = document.getElementById("imagerow").getElementsByTagName("li").length;
	setLeft();
}</pre>
<p>The only reason for the difference is that some browsers don&#8217;t support the <code>addEventListener</code> method. <code>setLeft()</code> we&#8217;ll see a little bit later on. <code>thumbNum</code> is just a convenient store for the number of thumbnails.</p>
<p>Next let&#8217;s look at some of the variables we&#8217;re using and how they&#8217;re initialized:</p>
<pre>var thumbNum;    //total number of thumbnails
var showNum = 4; //how many thumbnails to show
var curLeft = 0; //current index of the leftmost thumbnail</pre>
<p>thumbNum we already know gets initialized in the onLoad function. showNum and curLeft are used throughout the script.</p>
<p>Next, let&#8217;s look at the events and what they call:</p>
<pre>function HandleKeyPress(e) {
	switch (e.keyCode) {
		case e.DOM_VK_LEFT:
			moveLeft();
			break;
		case e.DOM_VK_RIGHT:
			moveRight();
			break;
		case e.DOM_VK_ESCAPE:
			content.focus();
			return;
	}
}

/** Event handler for mouse wheel event.
 * originally from http://adomas.org/javascript-mouse-wheel/
 */
 function handle(delta) {
        if (delta &lt; 0)
		moveRight();
        else
		moveLeft();
}

function HandleWheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}</pre>
<p>Looking at <code>HandleKeyPress</code> we see that it does a short switch-case on the keycode from the event. You can use this same technique to add other keypress events. There are also keyup and keydown events which have their uses.  The best list of the keycodes available is <a href="http://www.w3.org/TR/2000/WD-DOM-Level-3-Events-20000901/events.html">here</a>.</p>
<p>The mouse wheel event I&#8217;ve borrowed from another site (no point in re-inventing the wheel). The annoying part about the mouse wheel is that every browser interprets the event differently. Sooo&#8230; the script has to check for them all.</p>
<p>Both, the mouse wheel event and the keypress event, call <code>moveLeft()</code> and <code>moveRight()</code>, which are shown below:</p>
<pre>function moveLeft(){
  if (curLeft == 0) return; //already at the left
  else {
    curLeft = curLeft - 1;
    setLeft();
  }
}

function moveRight() {
  if (curLeft==thumbNum-showNum) return; //already at the right
  else {
    curLeft = curLeft + 1;
    setLeft();
  }
}</pre>
<p>All these do is check to see if they&#8217;re already at the left or the right and then increase or decrease <code>curLeft</code> and call <code>setLeft()</code>. <code>setLeft</code> is what does our styling changes.</p>
<pre>function setLeft(){
	var rng = getRanges();
	for (var i = 0; i &lt; rng.out.length; i++ ) {
		rng.out[i].setAttribute("style","display:none;");
		rng.out[i].style.display = "none";
	}
	for (var i = 0; i&lt; rng.in.length; i++ ) {
		rng.in[i].setAttribute("style","display:inline;");
		rng.in[i].style.display = "inline";
	}
}</pre>
<p>The first thing it does is get the ranges of elements which will be hidden or shown. Then it loops through those outside the range to be shown and hides them. Finally it loops through those inside the range to be shown and displays them.</p>
<pre>function getRanges() {
	var end = curLeft + showNum - 1; //calculate the image position at the end of the display

	var iva = new Array();	//create arrays for the in and out of range elements
	var ova = new Array();

	//get all of the &lt;li&gt; tags inside our imagrow
	var litags = document.getElementById("imagerow").getElementsByTagName("li");

	// loop through and add them to iva or ova if they
	// are in or out, respectively, of our desired range
	for (var i = 0; i &lt; litags.length; i++) {
		if ((i &lt; curLeft) || (i &gt; end))
			ova.push(litags[i]);
		else
			iva.push(litags[i]);
	}
	return { in: iva, out: ova };
}</pre>
<p>It&#8217;s pretty well commented but just to make some things clear: The <code>childNodes</code> and <code>getElementsByTagName</code> collections don&#8217;t work exactly like arrays and specifically they don&#8217;t support slice(). Therefore, we just make two new arrays and sort through the collection items. The finished product looks like ths:</p>
<div style="width: 750px; height: 170px; vertical-align: middle;"><img id="larrow" style="float:left;padding-top:40px;padding-right:10px;" src="/images/larrow.gif" alt="" /></p>
<div id="strip" class="filmstrip" style="overflow: hidden; float: left; height: 170px; width: 616px;">
<ul id="imagerow" class="imageRow">
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<p><img id="rarrow" style="float:left;padding-top:40px;padding-left:10px;" src="/images/rarrow.gif" alt="" /></div>
<p>Go ahead and try some of the events out. The key events only work if the strip has focus, so either click on it, or tab to it first. </p>
<p>If you want to see what it looks like on it&#8217;s own, here&#8217;s the <a href="http://ashita.org/filmstrip.html">html</a>, the <a href="http://ashita.org/filmstripEx.js">javascript</a>, and the <a href="http://ashita.org/filmstrip.css">css</a><br />
<script src="/filmstrip.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashita.org/javascript-controls-on-filmstrip/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CSS filmstrip</title>
		<link>http://www.ashita.org/css-and-javascript-filmstrip/</link>
		<comments>http://www.ashita.org/css-and-javascript-filmstrip/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 03:34:36 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[filmstrip]]></category>
		<category><![CDATA[img]]></category>
		<category><![CDATA[pictures]]></category>

		<guid isPermaLink="false">http://ashita.org/?p=99</guid>
		<description><![CDATA[Previously, we looked at using CSS opacity to create some interesting effects. In this aricle we&#8217;ll be using the CSS opacity technique to create some interesting effects in a filmstrip. To start with, let&#8217;s work from the assumption that all of our thumbnails are the same size and shape. For this example, thumbnails will be [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, we looked at using CSS opacity to create some interesting effects. In this aricle we&#8217;ll be using the CSS opacity technique to create some interesting effects in a filmstrip.</p>
<p>To start with, let&#8217;s work from the assumption that all of our thumbnails are the same size and shape. For this example, thumbnails will be 140&#215;100. This is of course, the kind of thing you could change in user preferences for a web app, but let&#8217;s start simple. So using a short strip of 3 pictures, we have some XHTML that looks like this.</p>
<pre>&lt;img src="/images/mythumb01.jpg"/&gt;&lt;img src="/images/mythumb02.jpg"/&gt;&lt;img src="/images/mythumb03.jpg"/&gt;</pre>
<p>But the first problem we see, of course, is that they are oriented vertically&#8230;. which is fine, but let&#8217;s try for a horizontal strip, shall we. In the bad old days we&#8217;d have done this in a table. I&#8217;m talking Netscape 4 days, here *shudder*. Moving quickly on, a good tip for whenever you want strict rows or columns of something is to use an unordered list and some CSS, like this:</p>
<pre>
&lt;ul class="imageRow"&gt;
	&lt;li&gt;&lt;img src="/images/mythumb01.jpg"/&gt;&lt;/li&gt;
	&lt;li&gt;&lt;img src="/images/mythumb02.jpg"/&gt;&lt;/li&gt;
	&lt;li&gt;&lt;img src="/images/mythumb03.jpg"/&gt;&lt;/li&gt;
&lt;/ul&gt;</pre>
<pre>ul.imageRow {
	width: 450px; /*width of all of your images plus the margins/padding you use*/
	height:140px;
}

ul.imageRow li {
	float: left;
	display: block;
	padding: 7px;
	margin: 0px;
}

ul.imageRow li img {
	border: 0px;
	height: 100px; /* just incase the images are a different size */
	width: 140px; /* ditto */
	margin: 0px;
	padding:0px;
}</pre>
<p>This will give us something like this:</p>
<ul style="list-style-image: none; list-style-type: none; height: 100px; width: 450px;">
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb01.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb02.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb03.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
</ul>
<p>What if you want to show a lot of images and, say, have the box scroll horizontally? The solution is to add a <code>&lt;div&gt;</code> with a width smaller than the <code>&lt;ul&gt;</code>.</p>
<pre>div.filmstrip {
	overflow-x: scroll;
	overflow-y: hidden;
	height: 140px; /* give it enough space for the images and the scrollbar, if present */
}</pre>
<pre>&lt;div class="filmstrip"&gt;
	&lt;ul class="imageRow"&gt;
	.
	.
	.
	&lt;/ul&gt;
&lt;/div&gt;</pre>
<p>Which gives us the following:</p>
<div style="overflow-y:hidden; overflow-x: scroll; height:140px;">
<ul style="list-style-image: none; list-style-type: none; height: 100px; width: 900px;">
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb01.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb02.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb03.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb04.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb05.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
<li style="background: none; float: left; display: block; padding: 0px; margin: 0px 0px 0px 10px;"><img src="/images/mythumb06.jpg" style="height:100px; width:140px;margin:0px; padding: 0px;" alt="" /></li>
</ul>
</div>
<p>Now I promised using opacity as a way to highlight pictures, so here it is:</p>
<pre>
ul.imageRow li {
	float: left;
	display: block;
	padding: 7px;
	margin: 0px;
	opacity: 0.7;
	filter: alpha(opacity=70);
}
ul.imageRow li:hover {
	opacity: 1.0;
	filter: alpha(opacity=100);
}</pre>
<p>This gives us:</p>
<div class="filmstripTut">
<ul class="imageRow">
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<p>Here are some other ways to add a highlight to the active picture:</p>
<pre>
ul.imageRow li {
	float: left;
	display: block;
	padding: 7px;
	margin: 0px;
	background-color: #fff;
}
ul.imageRow li:hover {
	background-color:#bef;
}</pre>
<div class="filmstripTut">
<ul class="imageRow">
<li class="bg"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="bg"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<pre>
ul.imageRow li {
	float: left;
	display: block;
	padding: 7px;
	margin: 0px;
}
ul.imageRow li:hover {
	padding: 4px;  /* IMPORTANT: allows the 3px border to fill the gap so the element
				is the same size as before */
	border: 3px dashed red;
}</pre>
<div class="filmstripTut">
<ul class="imageRow">
<li class="border"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="border"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<p>Going back to the opacity example, let&#8217;s change the background and add an image to give it that filmstrip feel.</p>
<pre>
div.filmstrip {
	overflow-x: scroll;
	overflow-y: hidden;
	height: 180px; /* give it enough space for the images and the scrollbar, if present (and the
				 filmstrip image) */
	padding: 0px;
	background-color: #000;
}

ul.imageRow {
	width: 1848px; /*width of all of your images plus the margins/padding you use*/
	height: 170px;
	background: url(/images/filmstrip.jpg) 0 0 repeat-x;
	padding: 25px 0px 25px 0px;
	margin: 0px;
}</pre>
<div class="filmstrip">
<ul class="imageRow">
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb01.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb02.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb03.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb04.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb05.jpg" alt="" /></li>
<li class="opac"><img src="/images/mythumb06.jpg" alt="" /></li>
</ul>
</div>
<p>We could have added the filmstrip background to the div instead of the list, but then the strip doesn&#8217;t appear to scroll. Sometimes that&#8217;s desirable, here it isn&#8217;t. Other things to note are that we&#8217;ve added padding to the top and bottom of the unordered list so the thumbnails now fit within the intended area.</p>
<p>Okay, but what about those ugly scroll bars? Well, In the next article we&#8217;ll look at some alternatives using Javascript and CSS positioning.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashita.org/css-and-javascript-filmstrip/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Overlapping Opacity</title>
		<link>http://www.ashita.org/overlapping-opacity/</link>
		<comments>http://www.ashita.org/overlapping-opacity/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 01:46:58 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://ashita.org/?p=26</guid>
		<description><![CDATA[In the last article, I showed very briefly styles and structures that included opacity. This time I&#8217;m going to focus on that exclusivley along with some examples. First off, let&#8217;s look at how we add opacity, including the Internet Explorer-specific method for cross-browser compatibility. .opaque { background-color: gold width: 5.0em; opacity: 1.0; filter: alpha(opacity=100); /* [...]]]></description>
			<content:encoded><![CDATA[<p>In the last article, I showed very briefly styles and structures that included opacity. This time I&#8217;m going to focus on that exclusivley along with some examples.</p>
<p>First off, let&#8217;s look at how we add opacity, including the Internet Explorer-specific method for cross-browser compatibility.</p>
<pre>.opaque {
	background-color: gold
	width: 5.0em;
	opacity: 1.0;
	filter: alpha(opacity=100); /* IE-specific */
}

.translucent {
	background-color: gold
	width: 5.0em;
	opacity: 0.5;
	filter: alpha(opacity=50); /* IE-specific */
}

.transparent {
	background-color: gold;
	width: 5.0em;
	opacity: 0.0;
	filter: alpha(opacity=0); /* IE-specific */
}
<hr />&lt;div class="opaque"&gt;Some text&lt;/div&gt;
&lt;div class="transparent"&gt;Some text&lt;/div&gt;
&lt;div class="translucent"&gt;Some text&lt;/div&gt;</pre>
<p>gives us the following. Note that I&#8217;ve put the transparent <code>&lt;div&gt;</code> between the opaque and translucent ones so you know where it is.</p>
<div style="background-color: gold; width: 5em; opacity: 1;filter: alpha(opacity=100);">Some text</div>
<div style="background-color: gold; width: 5em; opacity: 0;filter: alpha(opacity=0);">Some text</div>
<div style="background-color: gold; width: 5em; opacity: 0.5;filter: alpha(opacity=50);">Some text</div>
<p>now, what if you want the text to be opaque against a translucent background? The problem is, the text is a child of the &lt;div&gt; and opacity is inherited.  We can overcome this problem using multiple &lt;div&gt; tags and positioning.</p>
<pre>&lt;div id="outer_div" class="outer"&gt;
	&lt;div id="background_div" class="translucent"&gt;&amp;nbsp;
	&lt;/div&gt;
	&lt;div id="content_div" class="cover_text"&gt;
		Some Text
	&lt;/div&gt;
&lt;/div&gt;</pre>
<p>And let&#8217;s add the two style definitions, cover_text and outer</p>
<pre>.outer {
	height: 1.1em;
	position:relative;
}

.cover_text {
	position: absolute;
	height: 1.1em;
	top: 0px;
	left: 0px;
}</pre>
<p>Height was included as the only element containing content had <code>position: absolute</code>. You could get around this by adding some text to background_div and setting the <code>color</code> the same as the <code>background-color</code>. Both work just as well.<br/><br/>Now we have:</p>
<div id="outer_div" style="height:1.1em; position:relative;">
<div id="background_div" style="background-color: gold; width: 5em; opacity: 0.5;filter: alpha(opacity=50);">&nbsp;</div>
<div id="content_div" style="height:1.1em; position: absolute; top: 0px; left: 0px;">Some Text</div>
</div>
<p>Well, now that we have that, what about overlapping opacity like the title says?<br/><br />
Lets look at a few different cases.</p>
<pre>
.big_red_box {
	height: 10em;
	width: 10em;
	background-color: red;
	position:relative;
}
.medium_yellow_box {
	height: 6em;
	width: 6em;
	top: 2em;
	left: 2em;
	background-color: yellow;
	position:absolute;
}

.small_blue_box {
	height: 2em;
	width: 2em;
	top: 2em;
	left: 2em;
	background-color: blue;
	position:absolute;
}
</pre>
<p>Making the <code>&lt;div&gt;</code>s nested, largest-&gt;smallest gives us</p>
<div style="height: 10em; width: 10em; background-color: red; position:relative;">
<div style="height: 6em; width: 6em; top: 2em; left: 2em; background-color: yellow; position:absolute;">
<div style="height: 2em; width: 2em; top: 2em; left: 2em; background-color: blue; position:absolute;">&nbsp;</div>
</div>
</div>
<p>Remember that opacity is inherited so our blue box will acquire the same opacity as the yellow box. Adding <code>opacity:0.5; filter: alpha(opacity=50);</code> to <code>.medium_yellow_box</code>, we get:</p>
<div style="height: 10em; width: 10em; background-color: red; position:relative;">
<div style="height: 6em; width: 6em; top: 2em; left: 2em; opacity:0.5; filter: alpha(opacity=50); background-color: yellow; position:absolute;">
<div style="height: 2em; width: 2em; top: 2em; left: 2em; background-color: blue; position:absolute;">&nbsp;</div>
</div>
</div>
<p>What if we want the blue box to be opaque? Just like the text example before, we don&#8217;t make it a child. You&#8217;d need to change the position attributes <code>top</top> and <code>left</code> to reflect its position within it's new parent</p>
<pre>
&lt;div class="big_red_box"&gt;
	&lt;div class="medium_yellow_box"&gt;&amp;nbsp;
	&lt;/div&gt;
	&lt;div class="small_blue_box"&gt;
		&amp;nbsp;
	&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>Gives us,</p>
<div style="height: 10em; width: 10em; background-color: red; position:relative;">
<div style="height: 6em; width: 6em; top: 2em; left: 2em; opacity:0.5; filter: alpha(opacity=50); background-color: yellow; position:absolute;">&nbsp;</div>
<div style="height: 2em; width: 2em; top: 4em; left: 4em; background-color: blue; position:absolute;">&nbsp;</div>
</div>
<p>Now while I'm sure that everyone reading this loves boxes in basic colours, we can also do something a little more useful with this. You can use this same technique with images, which is rather more useful than colored boxes. In the next article I'll show how we can use this to make a nice film strip photo preview.<br/><br/><br />
I'll leave you with one last bit</p>
<pre>&lt;div style=&quot;position:relative;height:3em;width:3em;&quot;&gt;
&lt;div style=&quot;position:absolute;left:0em;top:0em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:red;float:left;&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;position:absolute;top:0em; left:1em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:blue;float:left;&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;position:absolute;top:1em;left:0.5em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:yellow;margin-bottom:1em;&quot;&gt;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;</pre>
<p> Gives us:</p>
<div style="position:relative;height:3em;width:3em;">
<div style="position:absolute;left:0em;top:0em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:red;float:left;">&nbsp;</div>
<div style="position:absolute;top:0em; left:1em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:blue;float:left;">&nbsp;</div>
<div style="position:absolute;top:1em;left:0.5em;height:2em;width:2em;opacity:0.5;filter:alpha(opacity=50);background-color:yellow;margin-bottom:1em;">&nbsp;</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashita.org/overlapping-opacity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dynamically Generated and Updated Table</title>
		<link>http://www.ashita.org/dynamically-generated-and-updated-table/</link>
		<comments>http://www.ashita.org/dynamically-generated-and-updated-table/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 00:21:09 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Opacity]]></category>
		<category><![CDATA[Positioning]]></category>
		<category><![CDATA[Table]]></category>

		<guid isPermaLink="false">http://ashita.org/?p=12</guid>
		<description><![CDATA[Although the page itself is pretty bare bones, the script itself is even simpler.  The bandanna guide is another piece I wrote to accompany the game Pirate Quest. This article attempts to demonstrate how we can use dynamically generated DOM objects to update table data on the fly.  In this case it&#8217;s fairly simple as [...]]]></description>
			<content:encoded><![CDATA[<p>Although the page itself is pretty bare bones, the script itself is even simpler.  The <a href="http://pq.ashita.org/bandannaguide.php">bandanna guide</a> is another piece I wrote to accompany the game <a href="http://www.piratequest.net/index.php?r=60051 ">Pirate Quest</a>.</p>
<p>This article attempts to demonstrate how we can use dynamically generated DOM objects to update table data on the fly.  In this case it&#8217;s fairly simple as only one value is changing and we&#8217;re only changing the cell text. There are some other ideas demonstrated here, such as using opacity and alternating row styles to create a colourful and easy to read table.</p>
<p>The following is the table we&#8217;ll be updating. It&#8217;s pretty simple &#8211; just the column headings.  <span style="color: #ff0000;">EDIT: added thead and tbody tags. IE would append rows to the table unless they were in the tbody tag.</span></p>
<pre id="line175">&lt;table id="tbl_guide" border="1"&gt;
	&lt;thead&gt;
		&lt;tr&gt;
			&lt;td rowspan="2"&gt;Level&lt;/td&gt;
			&lt;td rowspan="2"&gt;Energy&lt;/td&gt;
			&lt;td colspan="2"&gt;No Bandanna&lt;/td&gt;
			&lt;td colspan="2"&gt;Bandanna of Vigor&lt;/td&gt;
			&lt;td colspan="2"&gt;Bandanna of Vitality&lt;/td&gt;
			&lt;td colspan="2"&gt;Bafunda de la Cabeza&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;min&lt;/td&gt;
			&lt;td&gt;max&lt;/td&gt;
			&lt;td&gt;min&lt;/td&gt;
			&lt;td&gt;max&lt;/td&gt;
			&lt;td&gt;min&lt;/td&gt;
			&lt;td&gt;max&lt;/td&gt;
			&lt;td&gt;min&lt;/td&gt;
			&lt;td&gt;max&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody id="tbl_body"/&gt;
&lt;/table&gt;</pre>
<p>You can use whatever you want to start the update process. In this case I used the onchange event on a drop down list</p>
<pre>&lt;<span class="start-tag">select</span><span class="attribute-name"> id</span>=<span class="attribute-value">"hideout" </span><span class="attribute-name">onchange</span>=<span class="attribute-value">"update(Number(this.options[this.selectedIndex].value));"</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"100"</span>&gt;homeless (100)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"105"</span>&gt;Wretched Alcove (105)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"110"</span>&gt;Abandoned Outhouse (110)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"115"</span>&gt;Festering Swamp (115)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"120"</span>&gt;Swamp with a View (120)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"130"</span>&gt;Desolate Beach (130)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"140"</span>&gt;Rundown Shanty (140)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"150"</span>&gt;Rusted Roof Shack (150)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"160"</span>&gt;Shanty with a Fence (160)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"170"</span>&gt;Deserted Manor (170)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"180"</span>&gt;Ruined Castle (180)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"185"</span>&gt;Rundown Castle (185)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"190"</span>&gt;Stronghold (190)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"195"</span>&gt;Fortified Stronghold (195)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"200"</span>&gt;Shack on Skull Island (200)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"220"</span>&gt;Cavern on Skull Island (220)&lt;/<span class="end-tag">option</span>&gt;
  &lt;<span class="start-tag">option</span><span class="attribute-name"> value</span>=<span class="attribute-value">"230"</span>&gt;Stronghold on Skull Island (230)&lt;/<span class="end-tag">option</span>&gt;
&lt;/<span class="end-tag">select&gt;

</span></pre>
<p>Next, on to the script itself. First we define the number of rows and columns, and we create a 2D array for our cells.</p>
<pre>//2D array of table cells
var cells = new Array();

var numcols = 10;
var numrows = 600;</pre>
<p>I used the body onload event to set up the table rows. <span style="color: #ff0000;">EDIT: Because IE doesn&#8217;t seem to recognize a class or style change made with setAttribute(), I had to fall back on an alternative</span></p>
<pre>function onLoad() {
	//get our table and our select box
	var table = document.getElementById("tbl_guide");
	var select = document.getElementById("hideout");
	var row; //tr DOM object
	var cell; //td DOM object
	var celltext; //createTextNode DOM object
	var row_arr; //array of cells for each row
	//for the opacity overlay we use three divs. one on the outside, one for the
	//background color and one for the text
	var out_div, in_div1, in_div2; 

	//loop through the number of rows we want
	for (var i = 0; i&lt;numrows; i++) {

		//create this row and set the style for even or odd.
		row = document.createElement("tr");
		row.setAttribute("class",(i%2 == 0)? "even":"odd");
		row.className = (i%2 == 0)? "even":"odd"; //for IE

		//get a new, clean array to work with
		row_arr = new Array();

		//loop through each cell for the number of columns we have
		for (var j=0; j&lt;numcols;j++) {

			//create our table cell and div arrangement for our opacity trick
			cell = document.createElement("td");
			out_div = document.createElement("div");
			in_div1 = document.createElement("div");
			in_div2 = document.createElement("div");

			//set the styles for the div objects
			out_div.setAttribute("class","outer");
			in_div2.setAttribute("class","text");
			in_div1.setAttribute("class","bg");

			//for IE (up to 7) since it appears to lack proper support for setAttribute
			out_div.className = "outer";
			in_div1.className = "bg";
			in_div2.className = "text";

			//depending on what cell we're in we should do different things. mostly this affects the styles
			switch(j) {
				case 0:
					//level
					celltext = document.createTextNode(String(i+1));
					in_div1.setAttribute("style","background-color:yellow;");
					in_div1.style.background = "yellow"; //for IE again
					break;
				case 1:
					//Energy
					celltext = document.createTextNode(String(i+10));
					in_div1.setAttribute("style","background-color:yellow;");
					in_div1.style.background = "yellow";
					break;
				case 2:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:red;");
					in_div1.style.background = "red";
					break;
				case 3:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:red;");
					in_div1.style.background = "red";
					break;
				case 4:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:blue;");
					in_div1.style.background = "blue";
					break;
				case 5:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:blue;");
					in_div1.style.background = "blue";
					break;
				case 6:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:green;");
					in_div1.style.background = "green";
					break;
				case 7:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:green;");
					in_div1.style.background = "green";
					break;
				case 8:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:purple;");
					in_div1.style.background = "purple";
					break;
				case 9:
					celltext = document.createTextNode("");
					in_div1.setAttribute("style","background-color:purple;");
					in_div1.style.background = "purple";
					break;
			}

			//add our text to the second inner div
			in_div2.appendChild(celltext);

			//append the two inner divs to the outer div, then append the outer div to the cell
			out_div.appendChild(in_div1);
			out_div.appendChild(in_div2);
			cell.appendChild(out_div);

			//append the cell to the row
			row.appendChild(cell);

			/* finally add this celltext to the row array.
			 * NOTE: you could just as easily append one of the divs or the td tag. In this case
			 * I'm only changing the text. If you wanted to change the colors, for example, you'd
			 * need to push in_div1 onto the array. To change both the color and the text\, you'd
			 * need to push out_div onto the array and later access the children
			 */
			row_arr.push(celltext);
		}

		//add the row to the table, and push the row array onto our 2d cell array
		table.appendChild(row);
		cells.push(row_arr);
	}
	/* then update the values. The operation performed in update() could have been included
	 * in the switch-case above, but then if I changed it, I'd need to change it in two places.
	 * Casts the value from the select box to Number. for *, / , etc. the value is interpreted as
	 * a number anyways, but for +, it assumes it's a string value.... so we have to cast it.
	 */
	update(Number(select.options[select.selectedIndex].value));
}</pre>
<p>The onLoad function only assigns values to level and energy.  Hopefully the comments in the code are clear enough to see what I&#8217;ve done.</p>
<p>Next up, the update function</p>
<pre>function update(value) {
	//loop through each row
	for (var i = 0; i&lt;numrows; i++) {
		//loop through each cell in a row but ignore the first two
		//(values were set up in the onLoad and don't change now)
		for (var j=2; j&lt;numcols;j++) {
			//again switch-case, this time for the different formulas in each cell
			switch(j) {
				case 2:
					//no bandanna min
					cells[i][j].nodeValue = tp((value/150) * ((i+10)/20));
					break;
				case 3:
					//no bandanna max
					cells[i][j].nodeValue = tp((value/75) * ((i+10)/20));
					break;
				case 4:
					//vigor min
					cells[i][j].nodeValue = tp((value/150) * ((i+20)/20));
					break;
				case 5:
					//vigor max
					cells[i][j].nodeValue = tp((value/75) * ((i+20)/20));
					break;
				case 6:
					//vitality min
					cells[i][j].nodeValue = tp(((value + 20)/150) * ((i+10)/20));
					break;
				case 7:
					//vitality max
					cells[i][j].nodeValue = tp(((value + 20)/75) * ((i+10)/20));
					break;
				case 8:
					//bafunda min
					cells[i][j].nodeValue = tp(((value + 25)/150) * ((i+25)/20));
					break;
				case 9:
					//bafunda max
					cells[i][j].nodeValue = tp(((value + 25)/75) * ((i+25)/20));
					break;
			}
		}
	}
}</pre>
<p>The update function, as you can see, is very simple. we use .nodeValue to change the text content of our text nodes. As noted in the comments of the onLoad, you could also use the cell itself or one of the containing divs. Your cases would look more like <code>cells[i][j].childNodes[1].childNodes[0].nodeValue</code> if you used <code>out_div</code> in your cells array.</p>
<p>I suppose the last thing to look at is the style section.</p>
<pre>table { border-collapse:collapse; border-width:3px; border-style:double; border-color:black; }
td { border-color:black; border-width:1px; }
thead tr td {  padding-left:.5em; }
tbody tr td { padding: 0px 0px 0px 0px; height:1em; width:5em; }
div.outer { position:relative;}
div.bg { opacity: 0.3; height:1em; filter: alpha(opacity=30); padding: 5px 10px 5px 10px; }
div.text { position: absolute; top: 0; bottom: 0; color: black; padding: 5px 10px 5px 10px; }
tbody tr.even { background-color:#d0d0d0; }
tbody tr.odd { background-color:#ffffff; }</pre>
<p>It&#8217;s pretty short. True, I could have done more to pretty up the page.. and I may yet. but for now, it works well enough and demonstrates the dynamic generation and update technique.</p>
<p>As you can see the code is far from complicated and this would have been far messier in the old pre-DOM days *shudder*.</p>
<p><span style="color: #ff0000;"><br />
</span></p>
<p><span style="color: #ff0000;">EDIT: Also, please note that in IE the page is so slow to generate and update that I develop a whole new loathing for Ie every time I open it. In firefox, the generation takes a little time, but not too bad. The updating in Firefox is quite quick. For a small set of rows, IE is fine&#8230; 600 rows seemsto kill it though.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashita.org/dynamically-generated-and-updated-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

