Connecting Tech Pros Worldwide Forums | Help | Site Map

Mouseover and random image rotation

Newbie
 
Join Date: May 2007
Posts: 6
#1: May 28 '07
Hi

I have a page with 9 images on, each one placed within a table cell.

I want to be able to have a different image displayed each time the page is refreshed as well as being able to have the images rotate if the user places his mouse over an image. On mouesout it can either stick to the last image shown on the rotation or revert back to the orginal image when the page was loaded.

Any ideas?

Thanks

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 28 '07

re: Mouseover and random image rotation


By rotation, do you mean rotation angle-wise or rotate through the images?
Newbie
 
Join Date: May 2007
Posts: 6
#3: May 28 '07

re: Mouseover and random image rotation


Quote:

Originally Posted by acoder

By rotation, do you mean rotation angle-wise or rotate through the images?


I mean rotate through a number of images.

This means that the 9 photos on my main page will have a further 9 images each that I want to start rotating when the mouse is put on the existing image when the page was loaded. I don't want all of them to start rotating at the same time. only when the mouse is placed on the image that specific section should start rotating and when the mouse is moved away it should stop. Also I would like the page to load a different image for the 9 cells of the table each time the page is refreshed.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: May 28 '07

re: Mouseover and random image rotation


You'll need to give each of your image objects an id.

Use Math.random() for randomisation. If you want to guarantee a different image, you'll need cookies.

For rotating through the images, use setInterval on mouseover with the required number of milliseconds. clearInterval() will cancel onmouseout.

To change an image, change its source:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(imageid).src='newimg.gif';
Newbie
 
Join Date: May 2007
Posts: 6
#5: May 28 '07

re: Mouseover and random image rotation


Quote:

Originally Posted by acoder

You'll need to give each of your image objects an id.

Use Math.random() for randomisation. If you want to guarantee a different image, you'll need cookies.

For rotating through the images, use setInterval on mouseover with the required number of milliseconds. clearInterval() will cancel onmouseout.

To change an image, change its source:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById(imageid).src='newimg.gif';

Hi

I have looked at both links to see if I can figure it out but no luck. i can understand what you are saying but I require a bit more help in actually writing the script

Where for instance do i declare the id for my images?

Thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: May 28 '07

re: Mouseover and random image rotation


Quote:

Originally Posted by Ohann

Hi

I have looked at both links to see if I can figure it out but no luck. i can understand what you are saying but I require a bit more help in actually writing the script

Where for instance do i declare the id for my images?

Thanks

An example of an image would be [HTML]<img id="img1" src="image1.gif">[/HTML]

Post what you have so far and what you are stuck on.
Newbie
 
Join Date: May 2007
Posts: 6
#7: May 28 '07

re: Mouseover and random image rotation


gSlideshowInterval = 5;
gNumberOfImages = 5;

gImages = new Array(gNumberOfImages);
gImages[0] = "yumtiholidays.jpg";
gImages[1] = "yumtiholidays1.jpg";
gImages[2] = "yumtiholidays2.jpg";
gImages[3] = "yumtiholidays3.jpg";
gImages[4] = "yumtiholidays4.jpg";

function canManipulateImages() {
if (document.images)
return true;
else
return false;
}
function loadSlide(imageURL) {
if (gImageCapableBrowser) {
document.yhol.src = imageURL;
return false;
}
else {
return true;
}
}
function nextSlide() {
gCurrentImage = (gCurrentImage + 1) % gNumberOfImages;
loadSlide(gImages[gCurrentImage]);
}
gImageCapableBrowser = canManipulateImages();
gCurrentImage = 0;
setInterval("nextSlide()",gSlideshowInterval * 1000);
// -->
</SCRIPT>

gSlideshowInterval = 5;
gNumberOfImages = 5;

gImages = new Array(gNumberOfImages);
gImages[0] = "yumtilicious.jpg";
gImages[1] = "yumtilicious1.jpg";
gImages[2] = "yumtilicious2.jpg";
gImages[3] = "yumtilicious3.jpg";
gImages[4] = "yumtilicious4.jpg";

function canManipulateImages() {
if (document.images)
return true;
else
return false;
}
function loadSlide(imageURL) {
if (gImageCapableBrowser) {
document.ylic.src = imageURL;
return false;
}
else {
return true;
}
}
function nextSlide() {
gCurrentImage = (gCurrentImage + 1) % gNumberOfImages;
loadSlide(gImages[gCurrentImage]);
}
gImageCapableBrowser = canManipulateImages();
gCurrentImage = 0;
setInterval("nextSlide()",gSlideshowInterval * 1000);
// -->
</SCRIPT>

</head>

<body>
<td align="center"><a href="yumtiholidays.html"><img src="yumtiholidays.jpg" alt="Maldives" width="300" height="167" border="0" name="yhol" /></a></td>
<td align="center"><a href="yumtilicious.html"><img src="yumtilicious.jpg" alt="Feed me" width="300" height="167" border="0" name="ylic" /></a></td>


When I only put in the first javascript for yumtiholidays the images rotate fine but the moment I put in the second for yumtilicious the one stops working. Also I need to have it activated by the mouseover function.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: May 29 '07

re: Mouseover and random image rotation


You've redefined the same functions. You need to rename the second set for them to work together or you could reuse the same functions by calling them.

Secondly, you have used gImages again. You need to rename that otherwise you have overwritten the previous code.

To run it onmouseover, move the setInterval line into the onmouseover event handler:
[HTML]<img ... onmouseover="setInterval(...)">[/HTML] Mind the quotes - make sure you don't mix them up.
Don't forget to clear the interval onmouseout.
Newbie
 
Join Date: May 2007
Posts: 6
#9: May 29 '07

re: Mouseover and random image rotation


Quote:

Originally Posted by acoder

You've redefined the same functions. You need to rename the second set for them to work together or you could reuse the same functions by calling them.

Secondly, you have used gImages again. You need to rename that otherwise you have overwritten the previous code.

To run it onmouseover, move the setInterval line into the onmouseover event handler:
[HTML]<img ... onmouseover="setInterval(...)">[/HTML] Mind the quotes - make sure you don't mix them up.
Don't forget to clear the interval onmouseout.


Hi there

Thanks for all your help up to now.
Everything seems to be working fine now except that my clear interval on mousout is not working

Is this what it should look like?

onmouseover="setInterval('nextSlide()',gSlideshowI nterval * 1500)" onmouseout="clearInterval"(gSlideshowInterval * 1500)"

I have also tried it like this:
onmouseout="clearInterval('nextSlide()',gSlideshow Interval * 1500)"


Thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: May 30 '07

re: Mouseover and random image rotation


For clearInterval, see this example. Pass the ID returned by setInterval.
Reply