blumjay74
Apr 9th 2007, 12:54 pm
Hello, I'm new to this forum and to JavaScript as well. A friend of mine wrote this code for me as an alternative to an Animated GIF. It's an image swap setup to swap 4 images based on the position of the mouse. In the code he wrote, the program used the entire width of the screen divided by 4 to trigger the image swaps. But since the image changes only very slightly, I thought it would be better to have the image change only while the mouse is over it.
The best way I could figure how to do that was to store the image width as a variable in an external script in the root directory so if I want to re-size the images I only have
to change one file. All the images will be the same size, so this will get me by. Now I know there's a better was to do that but I'll figure that out once I get better with JavaScript.
The reason for my post is that once I got all that hammered out, and working fine in Firefox, I ran it on IE as an afterthought and it won't work!
I'm sure that the problem stems from the script's use of pageX. My friend won't allow a Windows machine in his house, he's strictly Linux so I can hardly blame him for the oversight.
My question is, what is the best was for this script to work in both FireFox and IE? This my first JavaScript program, so perhaps I don't understand IE's use of the clientX equivalent
http://members.cox.net/blumjay/testpage/bridge/imageSwap4.html
<html>
<head>
<title>image swap demo</title>
<script src="../338.js" type="text/javascript">
// The external script assigns two variables, imagewidth=338 and quarter=imagewidth/4
</script>
<script type="text/javascript">
// this is where the images get pre-loaded
preload1 = new Image();
//preload1.src = "frame1.jpg";
preload1.src = "1.jpg";
preload2 = new Image();
//preload2.src = "frame2.jpg";
preload2.src = "2.jpg";
preload3 = new Image();
//preload3.src = "frame3.jpg";
preload3.src = "3.jpg";
preload4 = new Image();
//preload4.src = "frame4.jpg";
preload4.src = "4.jpg";
</script>
</head>
<body>
<center>
<table border="0" width="100%">
<tr>
<td valign="middle" align="center">
<script type="text/javascript">
// Look out! This is case-sensitive.
document.onmousemove = getMouseX;
function getMouseX(e)
{
mouseX = e.pageX;
if (mouseX < 0) { mouseX = 0; }
if (mouseX > screen.width) { mouseX = screen.width; }
// comment out the below line when not debugging
document.demoForm.MouseX.value = mouseX;
if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (1 * quarter))
{
document["the_image"].src = preload1.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "1";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (2 * quarter))
{
document["the_image"].src = preload2.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "2";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (3 * quarter))
{
document["the_image"].src = preload3.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "3";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (4 * quarter))
{
document["the_image"].src = preload4.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "4";
}
}
</script>
<img src="1.jpg" width="338" height="418" name="the_image" id="the_image">
<!-- comment out this form when not debugging -->
<form name="demoForm">
debugging info:<br>
x-coordinate: <input type="text" name="MouseX" value="?" size="4"><br>
frame number: <input type="text" name="which_image" value="0" size="1"><br>
</form>
<!-- end of form to comment out when not debugging -->
<script type="text/javascript">
// Just more de-bugging info, just ignore this script
document.write(imagewidth + "<br>");
document.write(quarter + "<br>");
document.write((document.body.clientWidth - imagewidth) + "<br>");
</script>
</td>
</tr>
</table>
</center>
</body>
</html>
Thanks for looking!
Jay Blum
The best way I could figure how to do that was to store the image width as a variable in an external script in the root directory so if I want to re-size the images I only have
to change one file. All the images will be the same size, so this will get me by. Now I know there's a better was to do that but I'll figure that out once I get better with JavaScript.
The reason for my post is that once I got all that hammered out, and working fine in Firefox, I ran it on IE as an afterthought and it won't work!
I'm sure that the problem stems from the script's use of pageX. My friend won't allow a Windows machine in his house, he's strictly Linux so I can hardly blame him for the oversight.
My question is, what is the best was for this script to work in both FireFox and IE? This my first JavaScript program, so perhaps I don't understand IE's use of the clientX equivalent
http://members.cox.net/blumjay/testpage/bridge/imageSwap4.html
<html>
<head>
<title>image swap demo</title>
<script src="../338.js" type="text/javascript">
// The external script assigns two variables, imagewidth=338 and quarter=imagewidth/4
</script>
<script type="text/javascript">
// this is where the images get pre-loaded
preload1 = new Image();
//preload1.src = "frame1.jpg";
preload1.src = "1.jpg";
preload2 = new Image();
//preload2.src = "frame2.jpg";
preload2.src = "2.jpg";
preload3 = new Image();
//preload3.src = "frame3.jpg";
preload3.src = "3.jpg";
preload4 = new Image();
//preload4.src = "frame4.jpg";
preload4.src = "4.jpg";
</script>
</head>
<body>
<center>
<table border="0" width="100%">
<tr>
<td valign="middle" align="center">
<script type="text/javascript">
// Look out! This is case-sensitive.
document.onmousemove = getMouseX;
function getMouseX(e)
{
mouseX = e.pageX;
if (mouseX < 0) { mouseX = 0; }
if (mouseX > screen.width) { mouseX = screen.width; }
// comment out the below line when not debugging
document.demoForm.MouseX.value = mouseX;
if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (1 * quarter))
{
document["the_image"].src = preload1.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "1";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (2 * quarter))
{
document["the_image"].src = preload2.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "2";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (3 * quarter))
{
document["the_image"].src = preload3.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "3";
}
else if ( mouseX < 1 * ((document.body.clientWidth - imagewidth) / 2 ) + (4 * quarter))
{
document["the_image"].src = preload4.src;
// comment out the below line when not debugging
document.demoForm.which_image.value = "4";
}
}
</script>
<img src="1.jpg" width="338" height="418" name="the_image" id="the_image">
<!-- comment out this form when not debugging -->
<form name="demoForm">
debugging info:<br>
x-coordinate: <input type="text" name="MouseX" value="?" size="4"><br>
frame number: <input type="text" name="which_image" value="0" size="1"><br>
</form>
<!-- end of form to comment out when not debugging -->
<script type="text/javascript">
// Just more de-bugging info, just ignore this script
document.write(imagewidth + "<br>");
document.write(quarter + "<br>");
document.write((document.body.clientWidth - imagewidth) + "<br>");
</script>
</td>
</tr>
</table>
</center>
</body>
</html>
Thanks for looking!
Jay Blum