
25-02-2009
|
Member | | Join Date: May 2008
Posts: 2,008
| |
Re: Upload image randomly with JavaScript Page Now create the variable for the random function, to extract a given random in this case an image and access the individual properties of the matrix ran = Math.floor (4 * Math.random ()); The Math object provides constants and mathematical functions beyond those provided by standard operators for addition, subtraction, multiplication, etc. ...
in which each character represents the function to perform.The Math object is considered static, ie does not require an instance of to use the properties and methods of the object. There is no new statement to create a copy of Math. The method Math.floor (number) shows the nearest integer towards zero ES: Math.floor (32.5) becomes 32 you make a test with the examples below : <script> document.write (Math.floor (32.5)) </ script>
If the decimal part of N and ? '0.5 ? N is rounded up 0 Same for negative numbers -24.8 becomes -25 <script> document.write (Math.floor (-24.8)) </ script>
After the method Math.floor between parentheses put the number of array elements this example the 4 and multiply them by using the method Math.random () the method Math.random () will extract a random from among those included in the elements of the matrix Construction matrix elements img [0] = 'immagine1.jpg "width =" "height =" "border =""'; Method: identifier [number] where the identifier is the variable that calls img matrix array, img [0] img [1] img [2] img [3] Nb. the first number assigned to the identification, inserted between the brackets must be zero [0]
if we start from a [1] img [1] img [2] img [3] img [4] this picture will never be displayed img [0] = 'immagine1.jpg "width =" "height =" "border =""'; if you want comfort or convenience first identifier assigned to the No. [1] nodificate variable ran like this: ran = Math.floor (4 * Math.random () +1); after the identifier just insert the images to be displayed Just img [0] = 'immagine1.jpg', but is recommended to enter the dimensions of the images to avoid (although slight) a slowdown in the loading of the browser having to perform this function This is the part that deals with the display of object document.write image document.write ( "<img src=\""+img[ran]+"\">"); we put the tag <img src =, the variable for the matrix img Array and between the brackets variable ran [ran] to the identifiers and the function random. |