The forum of the forums

Would you like to react to this message? Create an account in a few clicks or log in to continue.
The forum of the forums
3 posters

    Randomize name color

    Derp-chan
    Derp-chan
    Forumember


    Female Posts : 29
    Reputation : 1
    Language : English

    Solved Randomize name color

    Post by Derp-chan August 17th 2015, 2:24 am

    I know it's possible to make a color change with each refresh using JS, but is there any way to apply this to a username maybe?



    Last edited by Derp-chan on August 17th 2015, 5:03 pm; edited 1 time in total
    Ace 1
    Ace 1
    Helper
    Helper


    Male Posts : 843
    Reputation : 64
    Language : English - French?
    Location : Druid Hill Park

    Solved Re: Randomize name color

    Post by Ace 1 August 17th 2015, 3:13 am

    if the user is in a group. then its close to impossible without heavy coding.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Randomize name color

    Post by Ange Tuteur August 17th 2015, 10:15 am

    It's possible, you'll just need to use JavaScript to apply a random class. For example :

    Go to Modules > JavaScript codes management > New script

    Placement : In all the pages
    Code:
    $(function() {
      var users = {
        'u1' : ['style_1', 'style_2', 'style_3'],
        'u2' : ['style_3'],
        'u3' : ['style_1', 'style_2']
      },
      a = document.getElementsByTagName('A'), i = 0, j = a.length, k;
     
      for (; i < j; i++) for (k in users) if ($(a[i]).attr('href') == '/' + k) a[i].className += ' ' + users[k][Math.floor(Math.random() * users[k].length)]
    });

    The script above will apply a random class to users 1 and 3, whereas user 2 is stuck with .style_3. The strings in the arrays are just random classnames for the example. You can change them to whatever you want.

    Then you just have to write some styles for your classnames.. Smile
    Code:
    .style_1, .style_1 > span { color:red !important }
    .style_2, .style_2 > span { color:green !important }
    .style_3, .style_3 > span { color:blue !important }
    Derp-chan
    Derp-chan
    Forumember


    Female Posts : 29
    Reputation : 1
    Language : English

    Solved Re: Randomize name color

    Post by Derp-chan August 17th 2015, 5:02 pm

    That worked, thank you!
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Randomize name color

    Post by Ange Tuteur August 17th 2015, 5:05 pm

    You're welcome ! Smile

    Topic archived

      Current date/time is November 13th 2024, 5:20 pm