riptide
08-05-2008, 06:21 PM
I'm using some scripts from the net and some from here i got them to all work with eachother but one thing is strange. let me explain each part.
the first code is the opacity changing script I got from brain error.
it works well and uses getElementById to find the object to control.
The second code is a classname function because I want to control the opacity of a whole class of objects. the classname function and the second opacity function are hooked up correctly as far as I can tell. I don't really know how to make the opacity function control classNames and/or Ids so I have two opacity functions.
the third code is one I was given here and it works nice and I'm using it to fade two divs in by clicking on a green one.
the forth code is a copy of the code above but it now looks for class names and works with onmouseout.
I'm trying to make it so all the divs with the classname sectaball will fade out when the mouse moves off. the whole code worked when there was only one div by the name of sectaball.
there are some other things I want to do to the dup object but I read that calling settimeout on a method is tricky.
I was thinking of making a function to shrink the height of any div. so I would have to use getElementByTag or use the classname function.
and lastly the whole code doesn't work when the setaball divs are inside of the div called the can. I can't find a way to fix it.
thanks in advance
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function opacity(id, opacStart, opacEnd, millisec) {
//speed for each frame
var speed = Math.round(millisec / 100);
var timer = 0;
//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart > opacEnd) {
for(i = opacStart; i >= opacEnd; i--) {
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
} else if(opacStart < opacEnd) {
for(i = opacStart; i <= opacEnd; i++)
{
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
}
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
var object = document.getElementById(id).style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
}
/*test code under here classname function added. the above code works*/
function getElementsByClass(node,searchClass,tag) {
var classElements = new Array();
var els = node.getElementsByTagName('div'); // use "*" for all elements
var elsLen = els.length;
var pattern = new RegExp("\\b"+searchClass+"\\b");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
/*classname function*/
function Classopacity(searchClass, opacStart2, opacEnd2, millisec) {
//speed for each frame
var speed2 = Math.round(millisec / 100);
var timer2 = 0;
//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart2 > opacEnd2) {
for(i = opacStart2; i >= opacEnd2; i--) {
setTimeout("changeClassOpac(" + i + ",'" + searchClass + "')",(timer2 * speed2));
timer2++;
}
} else if(opacStart2 < opacEnd2) {
for(i = opacStart2; i <= opacEnd2; i++)
{
setTimeout("changeClassOpac(" + i + ",'" + searchClass + "')",(timer2 * speed2));
timer2++;
}
}
}
/* above second opacity function for classes. below function to call the classnames opacity working as far as I know*/
function changeClassOpac(opacity, searchClass) {
var Cobject = getElementsByClass(document,searchClass,'div');
//alert(myEls[0].name);
for(i = 0; i < Cobject.length; i++){
Cobject[i].style;
Cobject[i].style.opacity = (opacity / 100);
Cobject[i].style.MozOpacity = (opacity / 100);
Cobject[i].style.KhtmlOpacity = (opacity / 100);
Cobject[i].style.filter = "alpha(opacity=" + opacity + ")";
}}
</script>
<style type="text/css">
<!--
#vanish {
border: 1px solid #000033;
position: absolute;
left: 120px;
top: 40px;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
#thecan {
background-color: #00CC33;
position: absolute;
height: 50px;
width: 200px;
left: 100px;
top: 40px;
}
.sectaball {
background-color: #003399;
height: 100px;
width: 100px;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
#thetest {
border: thin dashed #990000;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
-->
</style>
</head>
<body>
<div id="vanish" class="sectaball" > this is the nav </div>
<div id="thecan" class="linksclass2"></div>
<div id="thetest" class="sectaball" > this is the class test </div>
<script type="text/javascript">
var action=new Object();
action.linksclass1=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if(el)
alert(el.innerHTML);
return false;
}
action.thecan=function(){ opacity('vanish', 0, 100, 1000);opacity('thetest', 0, 100, 1000) ;
return false;
}
action.vanish=function(){alert("still works") ;
return false;
}
var theas=document.getElementsByTagName('div');
for (var i_tem = 0; i_tem < theas.length; i_tem++)
theas[i_tem].onclick=action[theas[i_tem].id];
var dup=new Object();
dup.vanisher2=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if(el)
alert(el.innerHTML);
return false;
}
dup.sectaball=function(){ /*Classopacity('sectaball', 100, 0, 1000)*/ alert("may work");
return false;
}
var sectabs=document.getElementsByTagName('div');
for (var Snav = 0; Snav < sectabs.length; Snav++)
sectabs[Snav].onmouseout=dup[sectabs[Snav].className];
</script>
</body>
</html>
the first code is the opacity changing script I got from brain error.
it works well and uses getElementById to find the object to control.
The second code is a classname function because I want to control the opacity of a whole class of objects. the classname function and the second opacity function are hooked up correctly as far as I can tell. I don't really know how to make the opacity function control classNames and/or Ids so I have two opacity functions.
the third code is one I was given here and it works nice and I'm using it to fade two divs in by clicking on a green one.
the forth code is a copy of the code above but it now looks for class names and works with onmouseout.
I'm trying to make it so all the divs with the classname sectaball will fade out when the mouse moves off. the whole code worked when there was only one div by the name of sectaball.
there are some other things I want to do to the dup object but I read that calling settimeout on a method is tricky.
I was thinking of making a function to shrink the height of any div. so I would have to use getElementByTag or use the classname function.
and lastly the whole code doesn't work when the setaball divs are inside of the div called the can. I can't find a way to fix it.
thanks in advance
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function opacity(id, opacStart, opacEnd, millisec) {
//speed for each frame
var speed = Math.round(millisec / 100);
var timer = 0;
//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart > opacEnd) {
for(i = opacStart; i >= opacEnd; i--) {
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
} else if(opacStart < opacEnd) {
for(i = opacStart; i <= opacEnd; i++)
{
setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
timer++;
}
}
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
var object = document.getElementById(id).style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
}
/*test code under here classname function added. the above code works*/
function getElementsByClass(node,searchClass,tag) {
var classElements = new Array();
var els = node.getElementsByTagName('div'); // use "*" for all elements
var elsLen = els.length;
var pattern = new RegExp("\\b"+searchClass+"\\b");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
/*classname function*/
function Classopacity(searchClass, opacStart2, opacEnd2, millisec) {
//speed for each frame
var speed2 = Math.round(millisec / 100);
var timer2 = 0;
//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart2 > opacEnd2) {
for(i = opacStart2; i >= opacEnd2; i--) {
setTimeout("changeClassOpac(" + i + ",'" + searchClass + "')",(timer2 * speed2));
timer2++;
}
} else if(opacStart2 < opacEnd2) {
for(i = opacStart2; i <= opacEnd2; i++)
{
setTimeout("changeClassOpac(" + i + ",'" + searchClass + "')",(timer2 * speed2));
timer2++;
}
}
}
/* above second opacity function for classes. below function to call the classnames opacity working as far as I know*/
function changeClassOpac(opacity, searchClass) {
var Cobject = getElementsByClass(document,searchClass,'div');
//alert(myEls[0].name);
for(i = 0; i < Cobject.length; i++){
Cobject[i].style;
Cobject[i].style.opacity = (opacity / 100);
Cobject[i].style.MozOpacity = (opacity / 100);
Cobject[i].style.KhtmlOpacity = (opacity / 100);
Cobject[i].style.filter = "alpha(opacity=" + opacity + ")";
}}
</script>
<style type="text/css">
<!--
#vanish {
border: 1px solid #000033;
position: absolute;
left: 120px;
top: 40px;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
#thecan {
background-color: #00CC33;
position: absolute;
height: 50px;
width: 200px;
left: 100px;
top: 40px;
}
.sectaball {
background-color: #003399;
height: 100px;
width: 100px;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
#thetest {
border: thin dashed #990000;
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:.0;
-khtml-opacity: .0;
}
-->
</style>
</head>
<body>
<div id="vanish" class="sectaball" > this is the nav </div>
<div id="thecan" class="linksclass2"></div>
<div id="thetest" class="sectaball" > this is the class test </div>
<script type="text/javascript">
var action=new Object();
action.linksclass1=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if(el)
alert(el.innerHTML);
return false;
}
action.thecan=function(){ opacity('vanish', 0, 100, 1000);opacity('thetest', 0, 100, 1000) ;
return false;
}
action.vanish=function(){alert("still works") ;
return false;
}
var theas=document.getElementsByTagName('div');
for (var i_tem = 0; i_tem < theas.length; i_tem++)
theas[i_tem].onclick=action[theas[i_tem].id];
var dup=new Object();
dup.vanisher2=function(e){
var el=window.event&&window.event.srcElement? window.event.srcElement : e&&e.target? e.target : null;
if(el)
alert(el.innerHTML);
return false;
}
dup.sectaball=function(){ /*Classopacity('sectaball', 100, 0, 1000)*/ alert("may work");
return false;
}
var sectabs=document.getElementsByTagName('div');
for (var Snav = 0; Snav < sectabs.length; Snav++)
sectabs[Snav].onmouseout=dup[sectabs[Snav].className];
</script>
</body>
</html>