Random image display skills

Source: Internet
Author: User
Random image display is a widely used technique. For example, when a random banner is displayed, its banner is always different when you enter a website, or there are always prompts with different content (tips ), this example is often found during online browsing. Using this technology can not only add more content in a certain space, but also give people the illusion of frequent updates.

How is it? As long as you have a little bit of html and javascript, everything is so simple. Follow me. Let's take a look at her random password.

Let's start with a simple example. We usually add images to the page to complete the process. If we want to randomly display three different images, we need to make a small modification to the code. First, add the mark:

Then, the document. write ("") type is put into the Mark

Document. write ("")

Now let's complete the most close section:

Id = Math. round (Math. random () * 2) + 1

In this example, the random number is. 3. Rename the image you want to display to 1.gif, 2.gif, 3.gif, and OK! The final code is:


Id = Math. round (Math. random () * 2) + 1
Document. write ("")

Try it, isn't it good? What if each of my images corresponds to a hyperlink?
We also assume that there are 3 images, 1. Gif, 2. Gif, and 3. The links are respectively url1, url2, and url3.
To match images and links one by one, we need to set an array image to place the link address, as shown below:

Var image = new Array (3)
Image. length = 3
Image [1] = "url1"
Image [2] = "url2"
Image [3] = "url3"

To obtain the link corresponding to the image, we also need to define an array imageurl = image [id]
The principle is as follows:
When the page is read, take a random number and set it to 2: id1_2. Then we can easily complete 2.gif display on the page. Then we can see: imageurl = image [2] and image [2] = "url2", the rest is easy to do. The complete code is as follows:


Var image = new Array (3)
Image. length = 3
Image [1] = "url1"
Image [2] = "url2"
Image [3] = "url3"
Id = Math. round (Math. random () * 2) + 1
Imageurl = image [id]
Document. write ("" + "")

This is just a simple example, there are a lot of functions can be achieved, for example, set the picture display probability, etc., please go to my homepage: http://www.avyes.com to see the effect, I learned limited, can only attract others, please give me more advice. Pc-king@21cn.com ).

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.