工作任務(wù)遇到的這個(gè)問題,雖然很簡單,但是發(fā)現(xiàn)很多知識(shí)點(diǎn)沒有掌握熟練,使得最后東查西問,浪費(fèi)時(shí)間,這里留文,以待后查。
思路整理:
-
我們需要解決兩個(gè)問題:一,隨機(jī)選取子元素;二,每隔一段時(shí)間展開某種行為。
隨機(jī):需要使用random()。
-
原材料:
- Math.random();
- Math.random()函數(shù)的取值為:[0,1);
- Math.floor();
- Math.floor()函數(shù)是向下取整的作用。
- children();
- jquery中,選取父元素的所有子元素。
- Math.random();
-
每隔:需要使用setInterval();
- 原材料:
- setInterval();
- setInterval()與setTimeOut()一樣,都是有兩個(gè)參數(shù):函數(shù)及時(shí)間。最好采用下述方式使用這樣方便調(diào)用終止函數(shù)。
- setInterval();
- 原材料:
實(shí)現(xiàn)如下:
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<!-- <script type="text/javascript" src="./js/index.js"></script> -->
<script type="text/javascript">
var childrenNode
var over;
function overTurn(){
over = setInterval(turn, 3000);
};
function turn(){
var num = Math.floor(Math.random()*9);
$('.box2').eq(0).children().removeClass('animate');
$('.box2').eq(0).children().eq(num).addClass('animate');
};
function stopOverTurn(){
clearInterval(over);
}
overTurn();
</script>