2015-01-03

Flickr:Setの写真を表示する(Masonryを使ってタイル風)

サンプル

「Flickr:Setの写真を表示する」をMasonryに対応するように変更した。
場合によっては、「Flickr:Setの写真を表示する」に記載した方法が良い場合もある。

参考:jQueryで可変グリッド・Masonryプラグイン

(なぜか、http://www.geocities.jp/winchester_cathedral_nostalgy/tips/FlickrPhotoSet_tile/index.html では、window.resizeが機能しない。
http://cgi.geocities.jp/winchester_cathedral_nostalgy/tips/FlickrPhotoSet_tile/index.html を使用。)

◆ html

コードは下記のとおり。

以下のjavascriptファイルを使用する。
    jquery.js - ここからダウンロード
    masonry.pkgd.min.js - ここからダウンロード

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset=UTF-8">
  5. <title>FlckrPhotoSet</title>
  6. <link rel="stylesheet" href="Flickr_photoset.css">
  7. <script src="jquery.js"></script>
  8. <script src="masonry.pkgd.min.js"></script>
  9. <script src="Flickr_photoset.js"></script>
  10.  
  11. <script>
  12. var photoset_id = '72157635974998255'
  13. var consumerKey = 'f4bc240df0b0418fb80e3719ec173dc0';
  14. </script>
  15.  
  16. </head>
  17.  
  18. <body>
  19. <div id="header">Loading...</div>
  20. <div id="contents">Loading...</div>
  21. </body>
  22.  
  23. </html>

Masonryを使用するため、8行目を追加した。


◆ javascript(Flickr_photoset.js)



  1. var list;
  2. var photoset_owner;
  3.  
  4. window.onresize = function(){
  5.  
  6. // #contentsの高さを全体の高さからtopの高さと20pxとを引いた高さにする
  7. var document_height = $(document).height();
  8. var header_height = $(header).height();
  9. document.getElementById('contents').style.height = (document_height - header_height-20) + "px";
  10.  
  11. }
  12.  
  13. window.onload = function () {
  14. ShowPhotos();
  15. }

6~9行目:
リサイズにはMasonryが対応してくれるので処理は不要。
#contentsの高さをウインドウに合わせて定義している。
こうしないと、#headerを固定して#contents部分だけをスクロールすることができない。



ShowPhotos()
htmlで定義した(photoset_id)photosetの情報を取得する。
  1. function ShowPhotos () {
  2.  
  3. // Header
  4. // callback関数の生成
  5. window[ 'jsonHeader'] = function(obj){
  6.  
  7. var description_ = obj.photoset.description._content.replace(/\n/g,"<br/>");
  8. var $title = $("<div>").html(obj.photoset.title._content).addClass("header_title"); // タイトルのタグを作成
  9. var $caption = $("<p>").html(description_).addClass("header_caption"); // キャプションのタグを作成
  10. var $div = $("<div>").append($title).append($caption); // div要素にタイトルのタグとキャプションのタグを追加
  11. $("#header").html($div); // 上記で作成したタグでid="header"の要素のタグを置き換え
  12.  
  13. };
  14.  
  15. // リクエスト
  16. var arguments = {
  17. api_key: api_key,
  18. photoset_id: photoset_id,
  19. format: "json",
  20. jsoncallback: "jsonHeader",
  21. method: "flickr.photosets.getInfo"
  22. };
  23. flickrRest(arguments);
  24.  
  25. // Photos
  26. // callback関数の生成
  27. window[ 'jsonPhotos'] = function(data){
  28. list = data.photoset.photo;
  29. photoset_owner = data.photoset.owner;
  30. Album();
  31.  
  32. };
  33.  
  34. // リクエスト
  35. var arguments = {
  36. api_key: api_key,
  37. photoset_id: photoset_id,
  38. format: "json",
  39. jsoncallback: "jsonPhotos",
  40. method: "flickr.photosets.getPhotos",
  41. extras: "geo,date_taken,owner_name,description,o_dims"
  42. };
  43. flickrRest(arguments);
  44.  
  45. }

ここは変更なし。


Album()
photosetの写真、タイトル(title)、descriptionを表示する。
  1. function Album () {
  2.  
  3. // #contentsの高さを全体の高さからtopの高さと20pxとを引いた高さにする
  4. var document_height = $(document).height();
  5. var header_height = $(header).height();
  6. document.getElementById('contents').style.height = (document_height - header_height-20) + "px";
  7.  
  8. var $box = [];
  9. var $contents = $('#contents').html("");
  10.  
  11. for( var i=0; i<list.length; i++ ) {
  12.  
  13. var photo = list[i];
  14.  
  15. var src = 'http://www.flickr.com/photos/' + photoset_owner + '/' + photo.id + '/in/set-' + photoset_id;
  16. var $ahref = $('<a>', { href: src, target: '_blank' });
  17. var src = 'http://static.flickr.com/' + photo.server + '/' + photo.id+ '_' + photo.secret + '.jpg';
  18. var $img = $('<img>', { 'src': src });
  19. var $div = $('<div class="box">').append($ahref).append($img);
  20. if ( photo.title != "") {
  21. var $photoTitle = $('<div >', { class: "photo_title", text: photo.title });
  22. $div.append($photoTitle);
  23. }
  24. if ( photo.description._content != "" ){
  25. var caption = photo.description._content.replace(/\n/g,"<br/>")
  26. var $photoCaption = $('<div>', { class: "photo_caption", text: caption });
  27. $div.append($photoCaption);
  28. }
  29. $contents.append($div);
  30. $box[i] = $div;
  31. }
  32.  
  33. $('#contents img').bind('load',function(){
  34. masonryBox ($contents,$box);
  35. });
  36.  
  37. }

8,9行目を変更。
30,31行目を変更。
34~36行目を変更。

19行目:画像を読み込み中にfloatで表示させるため、classをしています。

尚、4~6行目では、#contentsの高さをウインドウに合わせて定義している。
こうしないと、#headerを固定して#contents部分だけをスクロールすることができない。


masonryBox()
masonryを用いて表示する。
  1. function masonryBox ($container,$box) {
  2.  
  3. var $div = $('<div id="masonry_list">');
  4.  
  5. for( var i=0; i<$box.length; i++ ) {
  6. var $tmp = $('<div>', { class: "item", style: "margin:5px;" }).append($box[i]);
  7. $div.append($tmp);
  8. }
  9.  
  10. $container.html("")
  11. .append($div);
  12.  
  13. $('#masonry_list').masonry({
  14. isAnimated: true,
  15. duration: 400,
  16. itemSelector: '.item',
  17. isFitWidth: true
  18. });
  19.  
  20. }


6行目:"margin:5px;"にしないと、写真の間に間隔がなく、くっついて表示されてしまう。


flickrRest()
FlickrにArgumentsを送付する関数
  1. function flickrRest (arguments) {
  2.  
  3. // APIリクエストURLの生成
  4. var url = 'https://www.flickr.com/services/rest/?'+
  5. obj2query(arguments);
  6.  
  7. //  script 要素の発行
  8. var script = document.createElement( 'script' );
  9. script.type = 'text/javascript';
  10. script.src = url;
  11. document.body.appendChild( script );
  12.  
  13. }
  14.  
  15. // オブジェクトからクエリー文字列を生成する関数
  16. function obj2query ( obj ) {
  17. var list = [];
  18. for( var key in obj ) {
  19. var k = encodeURIComponent(key);
  20. var v = encodeURIComponent(obj[key]);
  21. list[list.length] = k+'='+v;
  22. }
  23. var query = list.join( '&' );
  24. return query;
  25.  
  26. }


ここは変更なし。


◆ css

  1. * {
  2. margin: 0px;
  3. padding: 0px;
  4. }
  5.  
  6. html, body {
  7. height: 100%;
  8. margin: 0px;
  9. padding: 0px;
  10. overflow: hidden;
  11. }
  12.  
  13. #header {
  14. background-color: white;
  15. }
  16.  
  17. #contents {
  18. overflow: scroll;
  19. background-color: #EFE;
  20. }
  21.  
  22. .box {
  23. float: left;
  24. margin: 5px;
  25. }
  26.  
  27. .header_title {
  28. text-align: left;
  29. margin: 0px;
  30. font: bold 32px arial,sans-serif;
  31. color: limegreen;
  32. }
  33.  
  34. .header_caption {
  35. text-align: left;
  36. margin: 10px;
  37. font: 14px arial,sans-serif;
  38. color: black;
  39. }
  40.  
  41. img {
  42. border: none;
  43. }
  44.  
  45. .photo_title {
  46. text-align: left;
  47. margin: 0px;
  48. font: bold 16px arial,sans-serif;
  49. text-decoration: none;
  50. color: black;
  51. }
  52.  
  53. .photo_caption {
  54. text-align: left;
  55. margin: 0px;
  56. font: 16px arial,sans-serif;
  57. color: black;
  58. }
  59.  
  60.  

22~25行目:画像を読み込み中にfloatで表示させています。




以上