如何用百分比制作随机图像生成器?

时间:2016-04-25 17:32:12

标签: javascript html

首先,我很抱歉我的英语不好。 我想问如何制作这个剧本,我想把这个百分比?我的意思是每张图片都有像百分之一的图像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";
	  myImages1[2] = "images/1.gif";
      myImages1[3] = "images/2.gif";
	  myImages1[4] = "images/1.gif";
      myImages1[5] = "images/2.gif";
	  myImages1[6] = "images/1.gif";
      myImages1[7] = "images/2.gif";
	  myImages1[8] = "images/1.gif";
      myImages1[9] = "images/2.gif";
	  myImages1[10] = "images/1.gif";
      myImages1[11] = "images/2.gif";
	  myImages1[12] = "images/1.gif";
      myImages1[13] = "images/2.gif";
	  myImages1[14] = "images/1.gif";
      myImages1[15] = "images/2.gif";
	  myImages1[16] = "images/1.gif";
      myImages1[17] = "images/2.gif";
	  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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

为每张图片创建一个单独保留precent的数组。 这意味着您拥有myImages1,这是一个在每个单元格图像中都有的数组。 每个细胞都有一个数字。 你创建了另一个名为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>";

}

根据需要更改每张图片的百分比。