Skip to content

Instantly share code, notes, and snippets.

@arthursoares
Created July 16, 2014 18:22
Show Gist options
  • Save arthursoares/37b0cc2ea24b9b99828e to your computer and use it in GitHub Desktop.
Save arthursoares/37b0cc2ea24b9b99828e to your computer and use it in GitHub Desktop.
javascript: function Gosling(ratio,imageurl){this.ratio=ratio;this.imageurl=imageurl}var getGosling={init:function(myGosling){this.myGosling=myGosling},horizontal:function(){return this.myGosling.filter(function(myGosling){return myGosling.ratio==="horizontal"})},vertical:function(){return this.myGosling.filter(function(myGosling){return myGosling.ratio==="vertical"})},square:function(){return this.myGosling.filter(function(myGosling){return myGosling.ratio==="square"})}};function Randomize(images){return Math.floor(Math.random()*images.length)}var myGosling=[new Gosling("horizontal","https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTX_NtNq8ClLbKYpIJ6-KvgjYOcSFLwX9Fk4H3JDrHy4xxrRMQDTHQ62fU3"),new Gosling("horizontal","https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSYmQzCpf7QSYsGkKlnAKKa6Jtww7-DUYPTgg4d55ebWCbsY0w6ySvKWHXp"),new Gosling("horizontal","https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSTqDQH4GG6mFBF6KWL0kueDnpxXpx_13VR3JoBLIim8dxtSxV5GaMBxwUl"), new Gosling("square","https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRGZ1KVWTPpAg4tsfPQODqReaevFEu89eEj_pupEWqlKQNcMsEGo7xn4Z8C")];function imageRatio(image){var proportion=image.height/image.width;if(proportion>1)return"vertical";else if(proportion===1)return"square";else if(proportion<1)return"horizontal"}(function(document){getGosling.init(myGosling);var images=document.getElementsByTagName("img"),length=images.length;for(var i=0;i<length;i++){var ratio=imageRatio(images[i]);if(ratio==="horizontal"){var number=Randomize(getGosling.horizontal());var img=getGosling.horizontal()[number];images[i].src=img.imageurl}else if(ratio==="square"){var number=Randomize(getGosling.square());var img=getGosling.square()[number];images[i].src=img.imageurl}else if(ratio==="vertical"){var number=Randomize(getGosling.vertical());var img=getGosling.vertical()[number];images[i].src=img.imageurl}}})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment