萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> 互動設計

打造響應式 Dribbble 設計作品瀑布流:jQuery 插件實戰教學

Dribbble 是全球設計師分享作品、獲取靈感與免費資源的寶藏平台。本教學將引導你使用 jQuery 插件組合,打造一個可即時抓取 Dribbble 熱門作品的響應式瀑布流應用,掌握動態佈局與圖片預載的核心技術。

核心插件介紹

  • Isotope:強大的魔術佈局引擎,實現瀑布流動態定位(需付費授權)
  • Jribbble:便捷呼叫 Dribbble API 的 jQuery 封裝
  • imagesLoaded:確保圖片完全載入後再觸發佈局,避免元素重疊

步驟一:呼叫 Jribbble 獲取熱門作品數據

使用 $.jribbble.getShotsByList 分頁抓取「popular」列表,支援 page(當前頁)與 per_page(每頁筆數)參數:

$.jribbble.getShotsByList("popular", function(data) {
  $.each(data.shots, function(i, shot) {
    // 處理每筆作品數據
  });
}, {page: pagenum, per_page: 10});

組裝 HTML 結構

在回調中提取標題、設計師、作品連結、縮圖等欄位,生成語意化的 <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 初始化與動態佈局

建立容器並啟用 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 設計作品瀑布流佈局效果

copyright © 萬盛學電腦網 all rights reserved