Random display of pictures in Web pages

Source: Internet
Author: User
Tags array
Random | web | display Pictures random display is a very wide range of techniques to apply. For example, the display of random banner, when you enter a website, its banner is always different, or there are always different tips (tips), you will often find such examples when browsing the Internet. The use of this technology, not only in a certain space to put more content, but also can give people a constant update of the illusion Oh.
How's the heartbeat? In fact, as long as you have a little bit of HTML and JavaScript basics, everything is so simple. Follow me, let's take a look at her random arcane.

Let's start with a simple example. Usually we add the picture in the page is to use To complete. If we want to randomly display 3 different pictures, we will make a small change to this code, first add <script> tag:
<script language=javascript></script>
Then in this tag, the Document.Write ("") into the form, became
document.write ("Now we are going to complete the most closed section:
Id=math.round (Math.random () *2) +1
So get the random number for 1,2,3 to rename the picture you want to show as 1.gif,2.gif,3.gif,ok! The final code is:
<script language=javascript>
Id=math.round (Math.random () *2) +1
document.write ("</script>
Try it, isn't it good? What if each of my pictures corresponds to a hyperlink?
Let's just assume there are 3 pictures, 1.gif,2.gif,3.gif, and the corresponding links are url1,url2,url3.
In order for the pictures and links to correspond, we want to set an array image to place the link's address as follows:
var image=new Array (3)
Image.length=3
Image= "URL1"
Image= "Url2"
Image= "Url3"
To remove the link to the picture, we also define an array Imageurl=image[id]
The principle is this:
When the page is read, take a random number, assuming that 2 is id=2, then we can easily complete the 2.gif on the page display. Then we can see: Imageurl=image and Image= "Url2", the rest of the matter will be done. The complete code is as follows:
<script language=javascript>
var image=new Array (3)
Image.length=3
Image= "URL1"
Image= "Url2"
Image= "Url3"
Id=math.round (Math.random () *2) +1
Imageurl=image[id]
document.write ("<a href=" +bannerurl+ ">" + "</script>
This is just a simple example, there are many functions can be achieved, such as the setting of the picture display probability.

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.