0

Does anyone know how to have an image change to a random image when the page loads and/or refreshes, and also have it where if you click on the image is goes to the next image, in sequence? Thanks!

4
Contributors
11
Replies
45
Views
3 Years
Discussion Span
Last Post by nauticalmac
Featured Replies
  • Hi, Looking at your code, here's what I suggest. NOTE THIS HAS NOT BEEN TESTED, maybe some work left for you to do. I modified the randomize function to give access to the image array and image number. Then added an 'onclick' event to call a function which finds the … Read More

0

Can you be more specific? What are you trying to achieve? A slideshow? Or just displaying random images that can be cycled by clicking on them?

0

So many possibilities. If you already have an array of image paths in Javascript in your page source (e.g. 'imagepaths'), here's a function to pick a random index to select the next image (different from the last).

function nextindex(previous)
{
// slidemax is the size of your image path array
// (imagepaths.length)
    if (slidemax <= 1)   {return previous;}
    var randomnumber;
    for(;;) {
        if((randomnumber=Math.floor(Math.random()*slidemax)) != previous)   {
            break;
        }
    }
    return randomnumber;
}

This could be integrated into your page using, e.g. jQuery to change the 'src' attribute of the image.
Picking the next in sequence if obvious, something like
`

if (++imageindex >= slidemax)   {
        imageindex = 0;
    }

`
Or you could use an ajax call to get a new index using php

What part of the problem are you having difficulty with?

0

Thanks for the replies! Here is more specifically what I'm trying to do.

On this page: http://lonnyfoster.com/temp/hitech/index.html there is a black & white image in the lower right that looks like text. The white text in these images are testimonials.

I used a javaScript code that makes a random testimonial appear on page load. I was wondering if anyone knows how to add the behavior of clicking on the image to go to the next one.

For example, if the page loads and image03 randomly apears, then you click on it, image04 will appear. Click again and image05 will appear. Click again image06 will appear, and so on. Reload the page and get another random image. Does that make more sense?

Thanks again!!

1

Hi,
Looking at your code, here's what I suggest.
NOTE THIS HAS NOT BEEN TESTED, maybe some work left for you to do.
I modified the randomize function to give access to the image array and image number. Then added an 'onclick' event to call a function which finds the next image and loads it into the background. Also suggest adding the CSS to make th area look clickable.

JS Modify existing and add
var images = new Array("images/testimonial1.png", "images/testimonial2.png", "images/testimonial3.png", "images/testimonial4.png", "images/testimonial5.png");
var imageNum = 0;
function Randomize() {
    imageNum = Math.floor(Math.random() * images.length);
    document.getElementById("navspace").style.backgroundImage = "url('" + images[imageNum] + "')";
}
window.onload = Randomize;

function loadnext() {
    if (++imageNum >= images.length)    {
        imageNum = 0;
    }
    document.getElementById("navspace").style.backgroundImage = "url('" + images[imageNum] + "')";
}

HTML add JS click event
<div id="rightContentDiv">
    <div id="navspace" onclick="loadnext();"></div>
</div>

CSS suggested add
#rightContentDiv #navspace  {cursor:pointer;}

Good luck!

0

Thank you nauticalmac!! It worked like a charm, first time with no modification needed. It's in place so you can see it working if you want. You are brilliant!!

Edited by lonrat

0

The little up arrow in my reply (up-vote). Mark of appreciation...
Don't care really, just started posting here.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.