samedi 4 juin 2016

JavaScript — Randomly assign 8 unique numbers to each class name

I need to randomly assign 6 unique numbers (0-5) to 6 class names. One number to each of the class names

How this is being used: When the hamburger icon is clicked, the menu opens and displays 6 rows of individual's names with background images specific to each one. The "transition" is for the profiles to stair-step in.

Here is what I have so far, but I am getting repeated numbers. I want the numbers to be assigned randomly, so the profiles are in a different order each time the menu is opened.

var generate_random_number = function() {
    var number = Math.floor(Math.random() * 6);
    return number;
};

var build_menu_profiles = function() {
    $(".profile-0").html("<div class='profile-title'><span class='emphasis'>Profile</span> 1</div>");
    $(".profile-1").html("<div class='profile-title'><span class='emphasis'>Profile</span> 2</div>");
    $(".profile-2").html("<div class='profile-title'><span class='emphasis'>Profile</span> 3</div>");
    $(".profile-3").html("<div class='profile-title'><span class='emphasis'>Profile</span> 4</div>");
    $(".profile-4").html("<div class='profile-title'><span class='emphasis'>Profile</span> 5</div>");
    $(".profile-5").html("<div class='profile-title'><span class='emphasis'>Profile</span> 6</div>");
};

var animate_menu_profiles = function(i) {
    i = 0; 

    $(".profiles").each(function(i) {
        $(this).toggleClass("transition-" + i);
        $(this).toggleClass("profile-" + generate_random_number());

        build_menu_profiles();
    });
};




Aucun commentaire:

Enregistrer un commentaire