给数量的元素随机排序

xiaoxiao2021-02-28  25

给定的背景图随机排序

//添加背景图片 var bgGounp = [ "images/icon1.png", "images/icon1.png", "images/icon2.png", "images/icon3.png", "images/icon4.png", "images/icon5.png", "images/icon6.png", "images/icon6.png", "images/icon7.png", "images/icon8.png" ]; shuffle(bgGounp) var bgPic1 = document.getElementsByClassName('icon'); for (var i = 0; i < 16; i++) { bgPic1[i].src = bgGounp[i] } //随机排列背景图的顺序 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; }
转载请注明原文地址: https://www.6miu.com/read-2300294.html

最新回复(0)