APOD Background

Example of using NASA's APOD as a background splash.

by liderrick

HTML

<section id="header">
  <div class="inner">

    <h1>Example of Using NASA's APOD<br>
					as a Background Splash</h1>
    <br>

    <h4>Photo from <span id="date"></span></h4>

    <br>
    <p>By Derrick Li</p>

  </div>
</section>

CSS

@import url("font-awesome.min.css");
@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic");

/*
	Photon by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
*/


/* Reset */

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

body {
  line-height: 1;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

body {
  -webkit-text-size-adjust: none;
}


/* Box Model */

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}


/* Containers */

.container {
  margin-left: auto;
  margin-right: auto;
}

.container.\31 25\25 {
  width: 100%;
  max-width: 75em;
  min-width: 60em;
}

.container.\37 5\25 {
  width: 45em;
}

.container.\35 0\25 {
  width: 30em;
}

.container.\32 5\25 {
  width: 15em;
}

.container {
  width: 60em;
}

@media screen and (max-width: 1680px) {
  .container.\31 25\25 {
    width: 100%;
    max-width: 75em;
    min-width: 60em;
  }
  .container.\37 5\25 {
    width: 45em;
  }
  .container.\35 0\25 {
    width: 30em;
  }
  .container.\32 5\25 {
    width: 15em;
  }
 ...

JavaScript

var apiKey = "Q04R5uu3AmDLtMki1TxSp3QhLzIAbSH2qnmf7rT7";

/* Set up background splash */
function setBackgroundSplash() {

	/* Get today's date */
	var dateObj = new Date();
	dateObj = new Date(dateObj.toDateString());
	var dateString = dateObj.toISOString().split("T")[0];

	var foundImage = false;

	while (!foundImage)
	{
		var req = new XMLHttpRequest();
		req.open("GET", "https://api.nasa.gov/planetary/apod?date=" + dateString + "&api_key=" + apiKey, false);

		req.addEventListener("load", function() {
			if(req.status >= 200 && req.status < 400)
			{
				var response = JSON.parse(req.responseText);

				if (response.media_type == "image")
				{
					if(response.hasOwnProperty("hdurl"))
					{
						document.getElementById("header").style.backgroundImage = "url('" + response.hdurl + "')";
					}
					else
					{
						document.getElementById("header").style.backgroundImage = "url('" + response.url + "')";
					}

					document.getElementById("date").textContent = dateObj.toLocaleDateString();
					foundImage = true;
				}
				else /* if(response.media_type == "video") */
				{
					/* Decrements the date by one */
					dateObj.setDate(dateObj.getDate() - 1);
					dateString = dateObj.toISOString().split("T")[0];
				}

			}
			else
			{
				console.log("Error in network request: " + req.statusText);
			}

		});

		req.send(null);

	}

}

setBackgroundSplash();