這是這篇文章的繼續問題: Add style to random loaded divs我現在試圖儘可能簡化這個問題。添加樣式隨機加載divs - 不能正常工作
這裏所說:
使用此代碼我試圖添加樣式隨機加載取決於在它們被加載什麼順序的項目。
<script>
$(document).ready(function(){
var divs = $("div.item").get().sort(function(){
return Math.round(Math.random())-0.5;
}).slice(0,6)
$(divs).each(function(index) {
if(index==1 || index==3)
$(this).css("margin-left", "0%");
else
$(this).css("margin-left", "2%"); //or whatever left value you need
});
$(divs).show();
});
</script>
我需要.item
條排隊在這幅畫你刷新瀏覽器每隔多少時間 到目前爲止,這只是ocurs偶然。
我覺得如果你自己嘗試一下,你會看到這個問題 這裏是一個快速的複製/粘貼整個事情
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<style>
.container {width:750px; background-color:#CCC; height:200px; padding-top:70px; margin: 0 auto; margin-top:5%}
.item {display:none; text-align:center; width:32%; float:left}
</style>
<script>
$(document).ready(function(){
var divs = $("div.item").get().sort(function(){
return Math.round(Math.random())-0.5;
}).slice(0,6)
$(divs).each(function(index) {
if(index==1 || index==3)
$(this).css("margin-left", "0%");
else
$(this).css("margin-left", "2%"); //or whatever left value you need
});
$(divs).show();
});
</script>
</head>
<body>
<div class="container">
<div class="item" style="background-color:#F00">1</div>
<div class="item" style="background-color:#9F0">2</div>
<div class="item" style="background-color:#FF0">3</div>
<div class="item" style="background-color:#939">4</div>
<div class="item" style="background-color:#3CF">5</div>
<div class="item" style="background-color:#CF3">6</div>
<div class="item" style="background-color:#6C9">7</div>
<div class="item" style="background-color:#999">8</div>
<div class="item" style="background-color:#90F">9</div>
<div class="item" style="background-color:#FF9">10</div>
<div class="item" style="background-color:#099">11</div>
<div class="item" style="background-color:#666">12</div>
</div>
</body>
</html>
這是怎麼回事? – David