o9s
is a microblog
home archives search feed blogroll


Random Avatars

I added random avatars in the header of this website.

I created an _Avatars directory in my Blot directory on Dropbox. It is prepended with an underscore because I don’t want its content to be turned into posts (this is a Blot thing). In this directory I added 50 images I liked named 1.png to 50.png.

Then I wrote this code. This is the markup for my avatar image.

<img class="avatar" src="/_Avatars/placeholder.png"/>

And this is the (vanilla) javascript.

var avatarElements = document.getElementsByClassName('avatar');
if (avatarElements.length > 0) {
  var avatarCount = 50;
  var random = Math.floor(Math.random() * avatarCount) + 1;
  avatarElements[0].setAttribute('src', '/_Avatars/' + random + '.png');
}

Edit: I have since removed this feature.

Posted on 2018-03-02   #html     #javascript     #blot  






← Next post    ·    Previous post →