我在一個遊戲上工作,並在一個問題中堆棧。查看代碼並進行測試。javascript - 如何隨機激活網格中的多個單元格
var createGrid=function(x,y){
\t var arrY = new Array(),
\t \t arrX,
container = $(".table");
\t \t i=1;
\t for(var iy=0; iy<y; iy++){
\t \t arrX = new Array();
\t \t for(var ix=0; ix<x; ix++){
\t \t \t arrX[ix]='<div class="cell">'+i+'</div>';
\t \t \t \t i++;
\t \t }
\t \t arrY[iy]='<div class="row">'+arrX.join("\r\n")+'</div>';
\t }
\t container.append(arrY.join("\r\n"));
};
// call function
(function($){
\t // create grid
\t createGrid(Math.ceil($(window).width()/50),Math.ceil($(window).height()/50));
\t // setup on ready
\t $(document).ready(function(){
\t \t var cell= $(".cell"),
\t \t \t maxCell = cell.length,
\t \t \t // find random cell and setup
\t \t \t randomActivate = function(){
\t \t \t \t \t $(".cell.active").removeClass("active");
\t \t \t \t \t var active=Math.round(Math.random()*(maxCell-1));
\t \t \t \t \t cell.eq(active).addClass("active");
\t \t \t \t };
\t \t \t // start random cell
\t \t \t randomActivate();
\t \t \t // loop random cell
\t \t \t setInterval(function(){
\t \t \t \t randomActivate();
\t \t \t }, 300);
\t });
}(jQuery));
body{
\t padding:0;
\t margin:-1px;
\t clear:both;
\t overflow:hidden;
\t position:relative;
}
.cell {display:table-cell;
border-right: 1px solid #000000;
border-bottom: 1px solid #000000;
width: 50px; height: 50px;
\t text-align:center;
\t vertical-align:middle;
\t font-size:22px;
\t font-weight:900;
\t color:#FFF;
}
.row {
display:table-row;
clear: both;
overflow: hidden;
}
.row:hover{
\t background:#e9e9e9;
}
.row:hover>.cell{
\t color:#e9e9e9;
}
.row>.cell:hover, .active{
\t background:#f00;
}
.table {
\t position:absolute;
\t top:0;
\t left:0;
\t bottom:0;
\t right:0;
\t z-index:0;
border-left: 1px solid #000000;
border-top: 1px solid #000000;
display:table;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="table"></div>
其中I堆棧是如何在同一時間的多個小區在網格中顯示的問題?
現在,就像你看到的,每個間隔只有一分格顯示號碼。我需要在不同的時間添加更多的時間間隔,這會產生像隨機發生的數字一樣的效果,如顯示和隱藏。很難解釋,因爲我找不到例子。
在同一時間可以多於一個有源單元但是每一個間隔有自己的有源單元的控制。
您有任何疑問,請拍。
隨機超時示例是我在尋找的。我在測試中提出的第一個解決方案,其次是我需要的解決方案。謝啦! –