mardi 5 mars 2019

Math.random() generating a single random number

I am creating a keyboard that creates divs upon key presses. (I already have the keyboard part figured out) I want each div to be placed randomly while still clustering by letter. I'm attempting to do this by adding a random integer between 1 and 20 to the existing ".aclass" top and left styling.

var min=1; 
var max=20;  
var random = Math.floor(Math.random() * (+max - +min) + +min);
var newtopa= $('.a').position().top + random;
var newlefta = $('.a').position().left + random;

function createElement(k) {  
  if (k == "a" || k == "A") { 
    $(".aclass").append('<div class="a" style="top:'+newtopa+'px;left:'+newlefta+'px;"></div>');
  }

However, my Math.random line is only returning a single random number. Everytime I press 'a' and create a div, it gets placed in the same place rather than randomly.




Aucun commentaire:

Enregistrer un commentaire