Dribbble 是全球設計師分享作品、獲取靈感與免費資源的寶藏平台。本教學將引導你使用 jQuery 插件組合,打造一個可即時抓取 Dribbble 熱門作品的響應式瀑布流應用,掌握動態佈局與圖片預載的核心技術。
使用 $.jribbble.getShotsByList 分頁抓取「popular」列表,支援 page(當前頁)與 per_page(每頁筆數)參數:
$.jribbble.getShotsByList("popular", function(data) {
$.each(data.shots, function(i, shot) {
// 處理每筆作品數據
});
}, {page: pagenum, per_page: 10});
在回調中提取標題、設計師、作品連結、縮圖等欄位,生成語意化的 <article> 元素:
var items = [];
$.each(data.shots, function(i, shot) {
items.push('<article>');
items.push('<div class="details"><h2>' + shot.title + '</h2></div>');
items.push('<a href="' + shot.url + '" target="_blank" class="linkc">');
items.push('<img src="' + shot.image_teaser_url + '" alt="' + shot.title + '">');
items.push('</a>');
items.push('<div class="author">設計師:<a href="' + shot.player.url + '">' + shot.player.name + '</a></div></article>');
});
建立容器並啟用 Isotope,指定 itemSelector: 'article':
$wallcontent.isotope({ itemSelector: 'article' });
大圖載入緩慢會導致元素重疊,必須使用 imagesLoaded 確保圖片就緒:
var newEls = items.join('');
var testcontent = $(newEls);
$wallcontent.append(testcontent);
$wallcontent.imagesLoaded(function() {
$wallcontent.isotope('appended', testcontent).isotope('reLayout');
$showmore.text('更多設計 (More)...').bind('click', loadshots);
});
當用戶瀏覽多頁作品後,提供平滑滾動返回頂端功能:
$(function() {
$('body').append('<div id="backtotop" class="showme"><div class="bttbg"></div></div>');
initGoToTop();
});
function initGoToTop() {
var orig_scroll_height = $("#footer").position().top - $(window).height() - 200;
$(window).scroll(function() {
if ($(this).scrollTop() > 100) $('#backtotop').addClass('showme');
else $('#backtotop').removeClass('showme');
});
$('#backtotop').click(function() {
$('body,html').animate({ scrollTop: 0 }, 400);
return false;
});
}

完成!你現在擁有一個動態載入、響應式佈局、體驗友善的 Dribbble 作品展示頁。完整代碼可依此邏輯整合擴展。
via gbtags | 來源:超酷的響應式 Dribbble 設計作品瀑布流佈局效果