An experiment to "stretch out" a list of floated items so it wouldn't collapse or stack when the window is shrunk. A continuation of the previous experiment, which rounded up the width values of list items to the next unit of measurement. Made for Flash Experiments portal, where I made the list items look like frames on a timeline in Flash.
The entire thing is accessible so that if JavaScript is turned off, the preset CSS widths will be displayed. Otherwise the JavaScript will replace them if JS is turned on.
Things learned:
element.style.width only sets the width and can't return the value; element.offsetWidth does that.px when setting a style measurement via JS; it works just like in CSS.for loop should be "less than" (<) and not "less than or equal to" (<=). It caused me grief when my Firebug kept saying the item has no properties. That's because I made the loop run past the number of arrays, resulting in a request for nothing.Click on the button above to see the script re-run as the longest row hides and appears. Adjust your window size to see where the bottom scrollbar appears. Turn off JavaScript and see how the list items would behave if the script is not active. (The items would stack if necessary.) (JavaScript is turned off.)
Row 1:
Row 2:
Row 3:
25 Feb 2008.
offsetWidth, among other offsetWhatever properties, which returns (as opposed to "setting") the current value of the element property.px" when defining style measurements via JS.sort() that w3schools.com seriously lacked.Relatively simple addition, sorting, and styles applying.
Here’s the code. Please link back here if you’re using it.
Place this CSS in the head tag.
body{
/*set your own minimum width in case JS is turned off.
ALSO: min-width won't work in IE unless you have a hack*/
min-width:800px;
}
ul.allLists{
list-style-position:outside;
clear:left;
}
ul.allLists li.row{
list-style-type:none;
float:left;
}
ul.allLists li.row{
margin-bottom:1em;
clear:left;
}
ul.allLists li.row ul li{
padding:0 16px 0 8px;
width:200px;
border-left:1px solid #000;
border-top:1px solid #000;
}
Place this JavaScript in the head tag.
//Redefine the width so Opera won't be confused later when width="auto" renders to 100%
//This also makes all the browsers with JS enabled to read it as auto
document.write("<style type='text/css' >ul#allLists li.row ul li.listItem{width:auto;}</style>");
Place this HTML in the body tag.
<ul id='allLists' >
<li class='row' id='row1' >
<ul id='list1' >
<li class='listItem' >Put list item content here. Repeat <li class="listItem">'s and <li class="row">'s as needed.</li>
</ul>
</li>
<li class='row' id='row2' >
<ul id='list2' >
<li class='listItem' >Put list item content here.</li>
</ul>
</li>
</ul>
Place this JavaScript at the end of the body tag.
//THIS PAGE ONLY - to show whether JS is turned on/off
document.getElementById("JSswitch").innerHTML="on";
/*global document, onload */
function doIt() {
var listItem=getElementsByClassName(document,"li","frameItem");
for (var i = 0;i < listItem.length;i += 1) {
listItem[i].style.width = 8-((listItem[i].offsetWidth-1)%8) + listItem[i].offsetWidth -26 + "px";
}
minWidth();
}
function minWidth(){
var row = getElementsByClassName(document, "ul", "row");
var rowItem=new Array();
var countAiner=new Array();
var count=0;
var k=0;
var l=0;
for (k=0;k<row.length;k+=1){
rowItem=getElementsByClassName(row[k],"li","frameItem");
count=0;
for (l=0;l<rowItem.length;l+=1){
count+=rowItem[l].offsetWidth;
countAiner[k]=count;
}
}
countAiner.sort(function(a,b){return b-a});
var minWidth=countAiner[0]+32;
document.body.style.minWidth=minWidth+"px";
}
onload = doIt;
var onOff=true; //true means on
var button=document.getElementById("switch2ndRow");
button.onclick=function(){
if (onOff==true){
document.getElementById("list2").style.display="none";
onOff=false;
} else if(onOff==false){
document.getElementById("list2").style.display="block";
onOff=true;
}
doIt();
}