Search code examples
javascriptjqueryhtmlfancyboxhref

Problems returning href class from Jquery


I have downloaded and implemented some jquery code that makes a photo gallery on my website from an instagram hashtag. I have been trying to edit this code so that clicking on the images in the gallery show up in a lightbox rather than following a link back to instagram.

Here is the the source for the code: http://www.jqueryscript.net/social-media/jQuery-Plugin-To-Create-An-Endless-Instagram-Gallery-embedstagram.html

The Lightbox code I have been using is fancybox: http://www.jqueryscript.net/lightbox/Responsive-jQuery-Lightbox-With-Amazing-CSS3-Effects-Fancy-Box-2.html

On my site the necessary components are loaded for both Jqueries and the function is run like so:

<script>
            $(document).ready(function(){      
                $(".ins_popup").fancybox({   
                    openEffect : 'fade',
                    closeEffect : 'fade'
                });  
            });   
        </script>

I have found the href in the original gallery jquery that links to instagram and edited it to include the class 'ins_popup' like so:

function renderPhotos(data) {
      $.each(data.data, function (i, photo) {
        photo = '<li class="photo"><a href="' + photo.images.standard_resolution.url.replace(/\\/, "") + '" class= "ins_popup"><img src="' + photo.images.standard_resolution.url.replace(/\\/, "") + '"></a></li>';

This does not seem to work however and the link now just opens an enlarged version of the picture rather than opening it in fancybox. I have tested each of the components (fancybox / hashtag gallery) separetly without problem, I just can't seem to get them to talk to one another.

And the associated code:

<!DOCTYPE html>
<html>
<head>
  <title>Embedstagram jQuery Plugin Demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />

  <!-- Include CSS Fies -->
  <link href="https://www.googledrive.com/host/0B_9gRWttmryISVpkTGFhVV8yajQ" rel="stylesheet" type="text/css" >
  <link href="http://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
  <link href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css" rel="stylesheet" type="text/css">
  <link href="http://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css"> 

  <!-- Include jQuery -->
  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

  <!-- Include fancybox library -->
  <script src="http://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
  <script>
   $(document).ready(function(){ 
    $(".ins_popup").fancybox({
     openEffect : 'fade',
     closeEffect : 'fade'
    });
   });
  </script>


</head>
<body>

<div class="jquery-script-clear"></div>


<h1 style="margin:150px auto 30px auto;">Embedstagram jQuery Plugin Demo</h1>
   <div id="embedstagram"></div>


 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <script src="https://www.googledrive.com/host/0B_9gRWttmryIbE0yNUJaeHNOQVk"></script>
   <script type="text/javascript">
    $(document).ready(function () {

      $('#embedstagram').embedstagram({
       client_id: '6f3244d01df94bb98785d04861561307',
       hashtag: '#neverlutionsoundsystem' ,
  thumb: 'tile'
       });

    });
  </script>
  </body>
</html>

and the Jquery for the gallery which I have edited:

/*global jQuery, window*/
(function ($) {
  $.fn.embedstagram = function (options) {

    var $this = this,
      next_url = '';

    var settings = $.extend({
      client_id: '',
      username: '',
      hashtag: '#neverlutionsoundsystem',
      count: 20,
      responsive: true,
      load_more: true,
      thumb: 'default',
      theme: 'default'
    }, options);

    settings.hashtag = settings.hashtag.replace('#', '');

    $this
      .addClass('embedstagram')
      .addClass('thumb-' + settings.thumb)
      .addClass('theme-' + settings.theme)
      .append('<ul></ul>')
      .append('<a href="#" id="function-button" class="function-button">Loading..</a>');

    var $photos = $this.find('ul'),
      $button = $this.find('#function-button');

    function resize() {
      if (settings.responsive) {
        var e = $this.width();
        var $photo = $('.photo');
        if (1024 >= e && e > 768) {
          $photo.css({'width': '25%', 'padding-bottom': '25%'});
        } else if (768 >= e && e > 570) {
          $photo.css({'width': '33.3%', 'padding-bottom': '33.3%'});
        } else if (570 >= e && e > 400) {
          $photo.css({'width': '50%', 'padding-bottom': '50%'});
        } else if (400 > e) {
          $photo.css({'width': '100%', 'padding-bottom': '100%'});
        } else {
          $photo.css({'width': '20%', 'padding-bottom': '20%'});
        }
      }
    }

    function renderPhotos(data) {
      $.each(data.data, function (i, photo) {
        photo = '<li class="photo"><a href="' + photo.images.standard_resolution.url.replace(/\\/, "") + '" class= "ins_popup"><img src="' + photo.images.standard_resolution.url.replace(/\\/, "") + '"></a></li>';

        $photos.append(photo);
        return i <= (settings.count);
      });
    }

    function getUrl(user_id) {
      var modifier;
      if (user_id) {
        modifier = 'users/' + user_id;
      } else if (settings.hashtag) {
        modifier = 'tags/' + settings.hashtag;
      }
      if (next_url) {
        return next_url;
      }
      return 'https://api.instagram.com/v1/' + modifier + '/media/recent/?&client_id=' + settings.client_id + '&count=' + settings.count;
    }

    function getPhotos(user_id) {
      $button.text('Loading..');
      $.ajax({
        type: "GET",
        dataType: 'jsonp',
        url: getUrl(user_id),
        success: function (data) {

          if (data.meta.error_message) {
            var error_message = 'Error: ' + data.meta.error_message.toLowerCase();
            $button.text(error_message).addClass('error');
            return false;
          }

          if (!settings.load_more) {
             $this.addClass('hide-load-more');
           }
          $button.text('Load More');
          renderPhotos(data);
          next_url = data.pagination.next_url;
          if (!next_url) {
            $button.text('No more images').addClass('disabled');
          }
          resize();
        },
        error: function () {
          $button.text('Error: unknown').addClass('error');
        }
      });
    }

    if (settings.username) {
      $.ajax({
        type: "GET",
        dataType: 'jsonp',
        url: 'https://api.instagram.com/v1/users/search?q=' + settings.username + '&client_id=' + settings.client_id,
        success: function (data) {

          if (data.meta.error_message) {
            var error_message = 'Error: ' + data.meta.error_message.toLowerCase();
            $button.text(error_message).addClass('error');
            return false;
          }

          if (data.data.length) {

            var num_items = data.data.length - 1;
            $.each(data.data, function (i, user) {
              if (settings.username === user.username) {
                var user_id = user.id;
                getPhotos(user_id);
                return false;
              }
              if (i === num_items) {
                $button.text('Error: no users found').addClass('error');
              }
            });

          } else {
            $button.text('Error: no users found').addClass('error');
          }
        },
        error: function () {
          $button.text('Error: unknown').addClass('error');
        }
      });
    } else if (settings.hashtag) {
      getPhotos();
    } else {
      $button.text('Error: missing username or hashtag').addClass('error');
    }

    $button.click(function (e) {
      e.preventDefault();
      if (!$(this).is('.disabled, .error')) {
        getPhotos();
      }
    });

    $(window).on('load resize', resize);

  };
}(jQuery));

If you need to see any of the other code please ask! Any help is much appreciated as you may have well guessed - I'm new to all of this

Thanks!


Solution

  • when you are calling the fancybox, images may not have been loaded to the DOM. call the fancybox plugin after the image load, not on the $(document).ready();

    function renderPhotos(data) {
      $.each(data.data, function (i, photo) {
        photo = '<li class="photo"><a href="' + photo.images.standard_resolution.url.replace(/\\/, "") + '" class= "ins_popup"><img src="' + photo.images.standard_resolution.url.replace(/\\/, "") + '"></a></li>';
    
        $photos.append(photo);
        return i <= (settings.count);
      });
      $(".ins_popup").fancybox({
         openEffect : 'fade',
         closeEffect : 'fade'
      });
    }
    

    Also I see in your sample website that you have included jquery twice in the page

    <!-- Include jQuery -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    

    Remove the 2nd Jquery include. this might be the issue.