-
-
Save AlFirous/5afba21f240fc39562ba2a001be3e7c7 to your computer and use it in GitHub Desktop.
A minimal HTML example to grab a random background from unsplash.com using their API and apply to a div.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script> | |
| <script> | |
| function GetRandomBackground() | |
| { | |
| var app_id = 'YOUR_APPLICATION_ID' | |
| var url = 'https://api.unsplash.com/photos/random?client_id=' + app_id; | |
| $.ajax({ | |
| url: url, | |
| dataType: 'json', | |
| success: function(json) { | |
| var src = json.urls.regular; | |
| $('#selector').css('background-image','url('+src+')').css('background-size','cover'); | |
| } | |
| }); | |
| } | |
| GetRandomBackground(); | |
| </script> | |
| </head> | |
| <div id="selector" style="width:100%; height:100%"></div> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment