2013-02-20 98 views
1

第I页reference如何随机化图像的顺序?

部分在图像被加载:

<div id="lighttable"> 

<img src="http://throwingupthew.com/girls/01.jpg" alt="01" /> 
<img src="http://throwingupthew.com/girls/02.jpg" alt="02" /> 
<img src="http://throwingupthew.com/girls/03.jpg" alt="03" /> 

etc.... 

</div> 

我如何随机图像的顺序? 请记住我不知道在index.html中放置任何代码/答案的位置

谢谢。

+1

通过先做一点研究开始:http://stackoverflow.com/questions/5329201/jquery-move-elements-into-a-random-order – Blender 2013-02-20 01:07:48

+0

你想用JavaScript或...随机化他们吗? – 2013-02-20 01:08:50

+0

我想以最简单的方式随机化。除了调整值来改变结果之外,我对HTML/javascript编码一无所知。 – 2013-02-20 01:12:26

回答

0

你的HTML改成这样:

<div id="lighttable"> 

<img id="girl1" src="" alt="01" /> 
<img id="girl2" src="" alt="02" /> 
<img id="girl3" src="" alt="03" /> 

etc.... 

</div> 

将这个正确的页面的</head>标签之前

<script type="text/javascript"> 
var array = ['http://throwingupthew.com/girls/01.jpg','http://throwingupthew.com/girls/02.jpg','http://throwingupthew.com/girls/03.jpg']; 
function shuffle(array) { 
var currentIndex = array.length; 
var temporaryValue; 
var randomIndex; 
    while (0 !== currentIndex) { 
    randomIndex = Math.floor(Math.random() * currentIndex); 
    currentIndex -= 1; 
    temporaryValue = array[currentIndex]; 
    array[currentIndex] = array[randomIndex]; 
    array[randomIndex] = temporaryValue; 
    } 
    return array; 
} 
var shuffled_images = shuffle(array); 
for(var i=0;i<3;i++) { 
document.getElementById('girl' + (i+1)).src = shuffled_images[i] ; 
} 
</script> 

由于Randomizing(Shuffling) an Array这个页面,我是能够使这个脚本为你: )

这里是工作JSFiddle :)