Applying a lightbox to JSON Data
I'm trying to pull images from the spotify API and give those images a lightbox. I've created a lightbox for images before but just not with using JSON data. Not sure what my code is missing. Your help would be greatly appreciated.
This is an example of the JSON data that I'm trying to pull from. Below, I've bolded the areas in my HTML, CSS, and Javascript which are directly related to the lightbox.
HTML
CSS
- body {
- background-color:
- }
- .top {
- text-align: center;
- }
- #music {
- margin-left: 40px;
- margin-right: 40px;
- margin-top: 40px;
- }
- .album {
- display: inline-block;
- }
- li {
- list-style: none;
- padding: 10px;
- }
- img {
- width: 200px;
- }
- input {
- width: 400px;
- height: 30px;
- }
- #overlay {
- background-color: rgba(0,0,0, 0.7);
- width: 100%;
- height: 100%;
- position: absolute;
- top: 0;
- left:0;
- display: none;
- text-align: center;
- }
- #overlay img {
- margin-top: 10%;
- height: 360px;
- width: 360px;
- }
- #overlay p {
- color: white;
- }
JavaScript
- $(document).ready(function() {
- $('form').submit(function(evt) {
- evt.preventDefault();
- // var $searchField = $('#song');
- var spotifyAPI = "https://api.spotify.com/v1/search" // needs end point
- // var albums = $searchField.val();// Specifies type of user info to be retrieved and sent to Spotify API
- var spotifyOptions = {
- "type": "album",
- "query": $("#song").val(),
- "limit": "12"
- }; // Requests data from spotify API. Must be a JavaScript Object
- $.getJSON(spotifyAPI, spotifyOptions, displayAlbums); // Currently, these are only placeholders. The variables inserted as arguments have not been defined as variables
- function displayAlbums(data){
- console.log(data);
- var albumHTML = '<ul>'; // Opening ul tag to hold a single album result
- $.each(data.albums.items, function(i, album){
- albumHTML += '<div class="album col-md-3" album-id="' + album.index + '">';
- albumHTML += '<li>'; // You may need to use bootstrap to style array items correctly
- albumHTML += '<a href="#">';
- albumHTML += '<img src="' + album.images[0].url + '" alt="' + album.artists.name + '">';
- albumHTML += '</a>';
- albumHTML += '</li>';
- albumHTML += '</div>';
- return albumHTML;
- });
- albumHTML += '</ul>';
- $('#music').html(albumHTML);
- };
- var $overlay = $('<div id="overlay"></div>');
- var $image = $("<img>");
- var $caption = $("<p></p>");
- $overlay.append($image);
- $overlay.append($caption);
- $("body").append($overlay);
- $("#song a").click(function(event){
- event.preventDefault();
- var imageLocation = $(this).attr("src"); // May need to specify a second argument
- $image.attr("src", imageLocation);
- $overlay.show();
- var captionText = $(this).children(img).attr("alt");
- $caption.text(captionText);
- });
- $overlay.click(function(){
- $overlay.hide();
-
- });
- });
- });