JQuery Thumbnail Scroller with Blogger JSON

http://hompimpaalaihumgambreng.blogspot.com

by Taufik Nurrohman

HTML

<div id="thumbnail-scroller">
    <script type="text/javascript">
    //<![CDATA[
    var thumbnails = 30;   
    function showThumbs(json) {
        var entry = json.feed.entry,
            title, url;
        document.write('<div class="container">');
        for (var i = 0; i < thumbnails; i++) {
            for (var j = 0; j < entry[i].link.length; j++) {
                if (entry[i].link[j].rel == 'alternate') {
                    url = entry[i].link[j].href;
                    break;
                }
            }
            title = entry[i].title.$t;
            if ("media$thumbnail" in entry[i]) {
                img = entry[i].media$thumbnail.url.replace(/\/s72\-c/, "/s100-c");
            } else {
                img = "http://reader-download.googlecode.com/svn/trunk/images/no-image-72x72.png";
            }
            document.write('<figure><a href="' + url + '" title="' + title + '"><img src="' + img + '" alt="" /></a></figure>');
        }
        document.write('</div>');
    }
    //]]>
    </script>
    <script type="text/javascript" src="http://latitudu.blogspot.com/feeds/posts/summary?alt=json-in-script&callback=showThumbs"></script>
</div>

CSS

body {background-color:#0D4883}

#thumbnail-scroller {
  height:110px;
  background-color:#810A0A;
  border:10px solid #12559D;
  position:relative;
  margin:50px 50px;
  overflow:auto;
}

/* Create shadow effect on the left and right of container */
#thumbnail-scroller:before,
#thumbnail-scroller:after {
  content:"";
  display:block;
  position:absolute;
  top:0;
  bottom:0;
  left:-4px;
  width:4px;
  height:100%;
  box-shadow:0 0 4px black;
  z-index:10;
}

#thumbnail-scroller:after {
  left:auto;
  right:-4px;
}

#thumbnail-scroller .container {
  position:absolute;
  top:0;
  left:0;
  margin:5px 0 0 5px;
  width:300%;
  height:100px;
}

#thumbnail-scroller figure {
  display:block;
  background-color:white;
  float:left;
  width:100px;
  height:100px;
  margin:0 5px 0 0;
  position:relative;
  overflow:hidden;
}

#thumbnail-scroller figcaption {
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
  background-color:black;
  font:italic normal 10px Arial,Sans-Serif;
  color:white;
  padding:5px 10px;
  text-align:left;
  overflow:hidden;
  display:none;
}

#thumbnail-scroller figure img {
  display:block;
  border:none;
  margin:0 0;
  width:100px;
  height:100px;
}

JavaScript

(function($) {

    var config = {
        itemMargins: 5 // Distance between the thumbnails
    };

    var $thumbnailScroller = $('#thumbnail-scroller'),
        $container = $thumbnailScroller.find('.container'),
        $item = $container.find('figure'),
        item_length = $item.length,
        item_width = $item.outerWidth(),
        item_margin = config.itemMargins,
        total_width = (item_width + item_margin) * item_length,
        $window = $(window);

    $thumbnailScroller.css('overflow', 'hidden');
    $container.css('width', total_width);

/*    // Auto caption builder & hover effect
    $item.each(function(cap) {
        if ($(this).children().attr('title')) {
            cap = $(this).children().attr('title');
            $(this).children().removeAttr('title').append('<figcaption>' + cap + '</figcaption>');
        }
    }).hover(function() {
        $(this).find('figcaption').stop(true, true).fadeIn('fast');
    }, function() {
        $(this).find('figcaption').stop(true, true).fadeOut('fast');
    }); */

    $window.on("resize", function() {
        var o_l = $thumbnailScroller.offset().left,
            t_w = $thumbnailScroller.width(),
            c_w = total_width;
        $thumbnailScroller.on("mousemove", function(e) {
            if ($(this).width() < $container.width()) {
                $container.css('left', -((e.pageX - o_l) * (c_w - t_w) / t_w));
            }
        });
    }).trigger("resize");

})(jQuery);