сейчас на сайте:
Лучшие сказки мира
Детектив и фантастика
Песни прошлых лет
Для досуга
смотреть оглавлениепримерразмер кода
The current sequence of the 18 images selected from an array of 18 images is as follows:
Notice that the same number is never selected twice. Once all the images have been displayed the script is reset and a new random selection beings.
You can also select to show a set number of randomly chosen images by changing images.length at the line how_many=images.length to the number required. So if you want to show 6 out of 18 then how_many=6 The script will now show a randomly selected 6 images at random. Please note that this number cannot be greater than the images contained in the array.
<HTML> <HEAD> <TITLE>Random Images</TITLE> <script type="text/javascript"> <!-- // realise by apachejeff // www.huntingground.freeserve.co.uk images = ["pic01.jpg","pic02.jpg","pic03.jpg","pic04.jpg","pic05.jpg","pic06.jpg"] var preloadYourImages=new Array() // preloads images for (i=0;i<images.length;i++) { preloadYourImages[i]=new Image() preloadYourImages[i].src=images[i] } how_many=images.length Numbers_Range=new Array() // array to hold numbers to select from. function init(){ for(n=0;n<images.length;n++){ // create list of numbers to choose from Numbers_Range[n]=n } show_pic() } num=0 function show_pic(){ num++ rndnum=Math.floor(Math.random()*Numbers_Range.length) Chosen_Number=Numbers_Range.splice(rndnum,1) // select and remove selected number from array document.mypic.src=images[Chosen_Number] timer=setTimeout("show_pic()",3000) if(num==how_many){ clearTimeout(timer) num=0 setTimeout("init()",3000) } } // add onload="init()" to the opening BODY tag // --> </script> </HEAD> <BODY onload="init()"> <h1>Random Images</h1> <img src="pic11.jpg" name="mypic" width=100 height=100> </BODY> </HTML>