Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtraction problem

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
0
0
GB
Ok i have the following script
Code:
function shrinkWidth(id,endWidth,speed){
	el = document.getElementById(id);
	if(Element.Width(id) > endWidth){
		w = Element.Width(id)
		el.style.width = Element.Width(id)-1 + "px";
		document.getElementById('debug').innerHTML = el.style.width;
		var sw = setTimeout(function(){shrinkWidth(id,endWidth,speed)},1);
	}
}

No I thought that
el.style.width = Element.Width(id)-1 + "px"

Would subtract 1 from the width each time. But it seems to add 1 to the width,

Help!!


}...the bane of my life!
 
I would guess Element.Width(id) return something like "250px" or the kind. In that case, it is this.
>Element.Width(id)-1 + "px";
[tt]([blue]parseInt([/blue]Element.Width(id)[blue],10)[/blue]-1) + "px";[/tt]
 
Element.Width(id) returns just a number.
I ended up with

Code:
function shrinkWidth(id,endWidth,speed,startWidth){
	el = document.getElementById(id);
	if(startWidth > endWidth){
		el.style.width = startWidth + "px";
		var sw = setTimeout(function(){shrinkWidth(id,endWidth,speed,startWidth)},1);
		startWidth = startWidth - speed
	}else{
		el.style.display = 'none'
	}
}

}...the bane of my life!
 
GuitarDave: I don't see your whole code, but you may be running into variable scope issues as well - I don't see any instance of the 'var' keyword. Use of the var keyword will ensure that you aren't setting/resetting values of variables in other functions that may use the same var names.

tsuji: Good call

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
I had already tried the parseint method and it did not work sadly.

The only missing var is on w = Element.Width(id) and that is not used.
The Element.Width function is
Code:
var Element = {
	Width : function(id){
		var oWidth;
		if(document.getElementById){
			oWidth = document.getElementById(id).offsetWidth;
		}else if (document.layers){
			oWidth = document.layers[id].clip.width;
		}
		return oWidth;
	}
}

}...the bane of my life!
 
[tt]parse[COLOR=blue yellow]I[/color]nt()[/tt]
 
As I said it was strange. Found a workaround thoug. I think at some point it became negative and started adding. Still can't figure how!!

}...the bane of my life!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top