2016-04-25 50 views
0

首先,我很抱歉我的英語不好。 我想問如何使這個腳本,我想把百分比?我的意思是每個圖像都有自己的百分比像一個圖像1有5%顯示圖像2有10%顯示出來並且像那樣?如何使隨機圖像發生器的百分比?

<!DOCTYPE html> 
 
<html> 
 
<body> 
 

 
<input class="randombutton" type="button" value="Randomize" onclick="randomImg1()"/> 
 

 
<script type="text/javascript"> 
 
    function randomImg1() { 
 

 

 
     myImages1 = new Array(); 
 
     myImages1[0] = "images/1.gif"; 
 
     myImages1[1] = "images/2.gif"; 
 
\t myImages1[2] = "images/1.gif"; 
 
     myImages1[3] = "images/2.gif"; 
 
\t myImages1[4] = "images/1.gif"; 
 
     myImages1[5] = "images/2.gif"; 
 
\t myImages1[6] = "images/1.gif"; 
 
     myImages1[7] = "images/2.gif"; 
 
\t myImages1[8] = "images/1.gif"; 
 
     myImages1[9] = "images/2.gif"; 
 
\t myImages1[10] = "images/1.gif"; 
 
     myImages1[11] = "images/2.gif"; 
 
\t myImages1[12] = "images/1.gif"; 
 
     myImages1[13] = "images/2.gif"; 
 
\t myImages1[14] = "images/1.gif"; 
 
     myImages1[15] = "images/2.gif"; 
 
\t myImages1[16] = "images/1.gif"; 
 
     myImages1[17] = "images/2.gif"; 
 
\t myImages1[18] = "images/1.gif"; 
 
     myImages1[19] = "images/2.gif"; 
 

 
     var rnd = Math.floor(Math.random() * myImages1.length); //incorporated other solution 
 

 
     document.getElementById("image").innerHTML = "<img src='" + myImages1[rnd] + "' alt='image'></img>"; 
 
    } 
 

 
</script> 
 
<div id="image"></div> 
 
</body> 
 
</html>

+0

你有php標籤。該死的,如果是php,我可以輕鬆做到這一點。 – Phiter

+0

嗨,如果這個任何答案已經解決了你的問題,請點擊複選標記,考慮[接受它](http://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – Ofir

回答

0

請單獨地保持precent對於每個畫面的陣列。 這意味着你有myImages1這是一個數組在每個單元格圖像。每個單元格都有一個數字。 你做了另一個稱爲precent的數組,它的單元數保持在另一個數組中相同單元數的圖像的precent。

function randomImg1() { 

    var precent = new Array(); 
    myImages1 = new Array(); 
    myImages1[0] = "images/1.gif"; 
    precent[0] = 5; 
    myImages1[1] = "images/2.gif"; 
    precent[1] = 10; 
    myImages1[2] = "images/1.gif"; 
    precent[2] = 20; 
    myImages1[3] = "images/2.gif"; 
    precent[3] = 30; 
    myImages1[4] = "images/1.gif"; 
    precent[4] = 40; 
    myImages1[5] = "images/2.gif"; 
    precent[5] = 70; 
    myImages1[6] = "images/1.gif"; 
    precent[6] = 90; 
    myImages1[7] = "images/2.gif"; 
    precent[7] = 45; 
    myImages1[8] = "images/1.gif"; 
    precent[8] = 93; 
    myImages1[9] = "images/2.gif"; 
    precent[9] = 21; 
    myImages1[10] = "images/1.gif"; 
    precent[10] = 20; 
    myImages1[11] = "images/2.gif"; 
    precent[11] = 34; 
    myImages1[12] = "images/1.gif"; 
    precent[12] = 66; 
    myImages1[13] = "images/2.gif"; 
    precent[13] = 94; 
    myImages1[14] = "images/1.gif"; 
    precent[14] = 56; 
    myImages1[15] = "images/2.gif"; 
    precent[15] = 87; 
    myImages1[16] = "images/1.gif"; 
    precent[16] = 12; 
    myImages1[17] = "images/2.gif"; 
    precent[17] = 44; 
    myImages1[18] = "images/1.gif"; 
    precent[18] = 76; 
    myImages1[19] = "images/2.gif"; 
    precent[19] = 89; 

    var rnd = Math.floor(Math.random() * myImages1.length); //incorporated other solution 

    document.getElementById("image").innerHTML = "<img src='" + myImages1[rnd] + "' alt='image' width='"+precent[rnd]+"%' height='"+precent[rnd]+"%'></img>"; 

} 

更改每張照片的百分比,只要你喜歡。

+0

我不明白?如果可以的話,你可以把它放在我的代碼中嗎? –

+0

您使用我的代碼行更改了document.getElementById,並且上面的代碼需要爲每張圖片分配precentage,就像myImages1上的圖片[5]您執行precent [5] = 5; - 它會給它5 precent size – Ofir

+0

試着去嘗試上面的代碼。 – Ofir