0

我想将随机图像放置在屏幕的随机位置。我试过这个:

$(document).ready(showLetter);
var imgsArray = ["A1", "C1", "F1", "J1", "K1", "L1", "S1", "Ñ1"];

function generateRandomForArray() {
    var num = Math.floor(Math.random() * 8);
    return num;
}

function generateRandom() {
    var num = Math.floor(Math.random() * 400);
    return num;
}

function showLetter() {
    var letter = imgsArray[generateRandomForArray()];
    $("div").append("<img src='imgs/" + letter + ".png'>");
    var left = generateRandom();
    var top = generateRandom();
    $("div").last().css({"top": top + "px", "left": left + "px"});
}

图像出现在屏幕的左上方。再一次:我想将它们放置在屏幕的随机位置。我对 CSS 了解不多。有任何想法吗?提前致谢!

4

2 回答 2

3

对我来说听起来像是定位问题。试试这样,即我加了position:absolute

function showLetter() {
    var letter = imgsArray[generateRandomForArray()];
    $("div").append("<img src='GameHTML5/images/" + letter + ".png'>");
    var left = generateRandom();
    var top = generateRandom();
    $("div").last().css({"position":"absolute","top": top + "px", "left": left + "px"});
}
于 2013-06-28T06:00:14.427 回答
0
function generateRandomForArray(min, max) {
  var num = Math.random() * (max - min) + min;
  return Math.floor(num);
}

//generateRandomForArray(0, 8);
//3
//generateRandomForArray(0, 8);
//0
//generateRandomForArray(0, 8);
//2
//generateRandomForArray(0, 8);
//4
//generateRandomForArray(0, 8);
//5
//generateRandomForArray(0, 8);
//7
//generateRandomForArray(0, 8);
//4
于 2013-06-28T05:55:35.250 回答