この記事は 約 0分26秒 で読めます。
jquery.instagram.jsに以下の機能を加えたバージョンを置いときます。
- コメントとタグをキーワードで検索、ヒットした記事を表示
- 親要素の横幅に併せて画像がフィット、画像の高さが足りない場合は高さがフィットして真ん中にトリミング
- ウンコIEの画像キャッシュ回避
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
/* * Instagram jQuery plugin * v0.2.1(original) * http://potomak.github.com/jquery-instagram/ * Copyright (c) 2012 Giovanni Cappellotto * * Licensed MIT * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * v0.2.2 * http://115design.main.jp/ * Copyright (c) 2014 115design */ (function ($) { $.fn.instagram = function (options) { var that = this, apiEndpoint = "https://api.instagram.com/v1", settings = { hash: null, userId: null, locationId: null, search: null, accessToken: null, clientId: null, request: null, show: null, onLoad: null, onComplete: null, maxId: null, minId: null, next_url: null, image_size: null, photoLink: true, keyword: null }; options && $.extend(settings, options); var isIE = (/(msie|MSIE)/gi).test(navigator.appVersion) || (/(T|t)rident/gi).test(navigator.appVersion); function createPhotoElement(photo, count) { var image_url = photo.images.thumbnail.url; if (settings.image_size == 'low_resolution') { image_url = photo.images.low_resolution.url; } else if (settings.image_size == 'thumbnail') { image_url = photo.images.thumbnail.url; } else if (settings.image_size == 'standard_resolution') { image_url = photo.images.standard_resolution.url; } if (isIE) { image_url += '?' + (new Date()).getTime(); } var innerHtml = $('<img>') .addClass('instagram-image') .attr('src', image_url) .css({ 'position': 'relative', 'visibility': 'hidden' }); if (settings.photoLink) { innerHtml = $('<a>') .attr('target', '_blank') .attr('href', photo.link) .append('<span></span>') .append(innerHtml); } innerHtml = $('<div>') .attr('id', 'timestamp-' + (new Date()).getTime()) .addClass('instagram-image-link') .css('overflow', 'hidden') .append(innerHtml); innerHtml = $('<div>') .addClass('instagram-image-link-cover') .append(innerHtml); var preloading = [ image_url ]; preload(preloading, innerHtml, //name function () { //initialize }, function () { //progress }, function (targetElement) { //complete removeWithKey(nowPercent, targetElement.attr('id')); fit(targetElement); } ); var captionText = 'No caption'; if (photo.caption !== null) { var content; var maxContentLength = 48; var textLength = photo.caption.text.length; if (maxContentLength < textLength) { var textTrim = photo.caption.text.substr(0, maxContentLength); content = textTrim + '...'; } else { content = photo.caption.text; } captionText = content; } var caption = $('<div>') .addClass('instagram-caption') .append(captionText); return $('<li>') .addClass('instagram-placeholder') .attr('id', photo.id) .append(innerHtml) .append(caption); } function createEmptyElement() { return $('<div>') .addClass('instagram-placeholder') .attr('id', 'empty') .append($('<p>').html('No photos for this query')); } var nowPercent = {}; function preload(images, preloader, initialize, progress, complete) { var img = []; var total = images.length; var decrementCount = total; var counterName = preloader.attr('id'); nowPercent[counterName] = 0; $(images).each(function (i) { var src = this; img[i] = new Image(); img[i].onload = function () { decrementCount--; nowPercent[counterName] = Math.ceil(100 * (total - decrementCount) / total); if (decrementCount === total - 1) { //initialize initialize(preloader); } if (0 < decrementCount) { //progress progress(preloader); } else { //complete complete(preloader); } }; img[i].src = src; }); } function removeWithKey(target, keyName) { for (var key in target) { if (key === keyName) { delete target[key]; } } } function fit(target) { target.find('img').removeAttr('style'); var ratio = target.width() / target.find('img').width(); var width = Math.round(target.find('img').width() * ratio); var height = Math.round(target.find('img').height() * ratio); if (target.height() > height) { ratio = target.height() / height; width = Math.round(width * ratio); height = Math.round(height * ratio); } target.find('img').width(width); target.find('img').height(height); target.find('img').css({ top: -target.find('img').height() / 2 + target.height() / 2, left: -target.find('img').width() / 2 + target.width() / 2 }); target.find('img').css('visibility', 'visible'); } function composeRequestURL(maxID) { var url = apiEndpoint, params = {}; if (settings.next_url !== null) { return settings.next_url; } if (settings.hash !== null) { url += "/tags/" + settings.hash + "/media/recent"; } else if (settings.search !== null) { url += "/media/search"; params.lat = settings.search.lat; params.lng = settings.search.lng; settings.search.max_timestamp !== null && (params.max_timestamp = settings.search.max_timestamp); settings.search.min_timestamp !== null && (params.min_timestamp = settings.search.min_timestamp); settings.search.distance !== null && (params.distance = settings.search.distance); } else if (settings.userId !== null) { url += "/users/" + settings.userId + "/media/recent"; } else if (settings.locationId !== null) { url += "/locations/" + settings.locationId + "/media/recent"; } else { url += "/media/popular"; } settings.accessToken !== null && (params.access_token = settings.accessToken); settings.clientId !== null && (params.client_id = settings.clientId); settings.minId !== null && (params.min_id = settings.minId); settings.maxId !== null && (params.max_id = settings.maxId); settings.request !== null && (params.count = settings.request); if(maxID !== ''){ params.max_id = maxID; } url += "?" + $.param(params); return url; } settings.onLoad !== null && typeof settings.onLoad == 'function' && settings.onLoad(); var counter = 0; function loadData(maxID){ $.ajax({ type: "GET", dataType: "jsonp", cache: false, url: composeRequestURL(maxID) }) .done(function(res){ var length = typeof res.data != 'undefined' ? res.data.length : 0; if (length > 0) { for (var i = 0; i < length; i++) { if (settings.keyword !== null) { for (var key in settings.keyword) { if ((new RegExp(settings.keyword[key], 'gi')).test(res.data[i].caption.text) || (new RegExp(settings.keyword[key], 'gi')).test(res.data[i].tags.join())) { that.append(createPhotoElement(res.data[i], counter)); counter++; break; } } } else { that.append(createPhotoElement(res.data[i], counter)); counter++; } if (counter >= settings.show) { break; } } } if (length === 0) { //該当件数が0件だった場合 that.append(createEmptyElement()); settings.onComplete !== null && typeof settings.onComplete == 'function' && settings.onComplete(res.data, res); } if(counter !== settings.show && typeof res.pagination.next_max_id === 'string'){ loadData(res.pagination.next_max_id); } else{ //全データ読み込み完了または表示件数を満たした場合 settings.onComplete !== null && typeof settings.onComplete == 'function' && settings.onComplete(res.data, res); } }); } loadData(''); return this; }; })(jQuery); |
★使用例
1 2 3 4 5 6 7 8 |
$('セレクタ').instagram({ userId: '', //事前に取得しておく accessToken: '', //事前に取得しておく image_size: 'standard_resolution', //予め決められた画像サイズのどれを使うか選べます show: 50, //最大表示数 keyword: ['風景','柴犬'], //[''] 設定したuserIdページ内でキーワードを複数検索できます clientId: '' //事前に取得しておく }); |