2. x 版本


基本用法

將 Gridzy 檔案上載去你嘅伺服器,然後將佢哋加入你嘅網站(通常喺頭部分):

<link rel="stylesheet" href="gridzy/gridzy.min.css" />
<script src="gridzy/gridzy.min.js"></script>

加入 HTML 代碼(喺正文部分嘅任何位置):

<div class="gridzy">
  <img src="https://loremflickr.com/640/480?random=1" alt="a random image" />
  <img src="https://loremflickr.com/480/640?random=2" alt="another random image" />
  <img src="https://loremflickr.com/640/640?random=3" alt="yet another random image" />
  <img src="https://loremflickr.com/640/480?random=4" alt="a random image again" />
  <img src="https://loremflickr.com/640/480?random=5" alt="one last random image" />
</div>

就係咁啦!呢個係一個全功能嘅例子。唔需要額外嘅初始化。

配置

配置 Gridzy 最簡單嘅方法係,只係用 HTML 屬性。你只需要喺選項名稱前面加data-gridzy- 。所以,要設定spaceBetween選項,只需要加入屬性data-gridzy-spaceBetween 。

<div class="gridzy" data-gridzy-spaceBetween="20" data-gridzy-desiredHeight="400">
  <img src="https://loremflickr.com/640/480?random=1" alt="a random image" />
  <img src="https://loremflickr.com/480/640?random=2" alt="another random image" />
  <img src="https://loremflickr.com/640/640?random=3" alt="yet another random image" />
  <img src="https://loremflickr.com/640/480?random=4" alt="a random image again" />
  <img src="https://loremflickr.com/640/480?random=5" alt="one last random image" />
</div>

主要選項

預設值描述
animatetrue決定網格變化嗰陣係咪應該用動畫(例如調整瀏覽器視窗大細嗰陣或者設定篩選器嗰陣)。
layoutjustified定義用咗嘅版面配置。 Gridzy waterfall js 2支援兩種唔同嘅版面配置justified佢哋每個都有自己嘅額外選項參數,你可以喺呢個表格下面搵到。
filter*決定應該顯示邊啲圖片。任何 CSS 選擇器都可以喺呢度用作值。通常會透過filterControls自動設定(睇下一個選項)。
睇下過濾器
filterControls決定邊啲表格元素可以用嚟篩選圖片。任何選擇 HTML 表單元素嘅 CSS 選擇器都可以喺呢度用作值。
睇下過濾器
autoConsiderScrollBarstrue預測滾動條出現或消失嘅可用寬度。除非呢種自動化會引起問題,否則建議永遠將佢保持為true 。

Justified布局選項

預設值描述
autoFontSizefalse根據原始方塊嘅大小,自動更新每個方塊嘅字體大小屬性。預設情況下,字體大小屬性唔會明確設定。
desiredHeight190定義元素行嘅所需高度。實際高度會根據可用嘅闊度同埋行入面嘅元素計算。
hideOnMissingImagetrue隱藏包含載入唔到嘅圖片嘅框。
spaceBetween4定義元素之間嘅距離(以像素計)。
lastRowAlign
由2.3.0版本開始
justified定義最後一行嘅行為。允許嘅值係left 、 center 、 right同埋justified 。
singleRowAlign
由2.3.0版本開始
justified定義如果只有一行嘅行為。允許嘅值係left 、 center 、 right同埋justified 。
fillLastRow
由2.1.0版本開始
true由2.4.0版本開始移除咗(由2.3.0版本開始已經唔再棄用)
排列圖片,令到最後一行永遠都完全填滿。如果你打算動態附加圖片,最好停用呢個功能。
對單行冇影響(睇fillSingleRow )。
fillSingleRow
由2.1.0版本開始
true由2.4.0版本開始移除咗(由2.3.0版本開始已經唔再棄用)
如果只有一行,佢會排列啲圖像,令到嗰行永遠都完全填滿。如果你喺圖庫入面只有一至兩張圖片,停用呢張圖片可能係一個好主意。

布局Waterfall

預設值描述
autoFontSizefalse根據原始方塊嘅大小,自動更新每個方塊嘅字體大小屬性。預設情況下,字體大小屬性唔會明確設定。
desiredWidth250定義所需嘅欄闊度。實際嘅闊度會根據可用嘅闊度同欄數計算。
hideOnMissingImagetrue隱藏包含載入唔到嘅圖片嘅框。
horizontalOrderfalse定義元素由左到右嚴格放喺每欄一個元素,然後喺最左邊嘅欄重新開始,獨立於邊欄係更多位置。預設情況下,元素會一直放喺高度最細嘅欄入面。
spaceBetween4定義元素之間嘅距離(以像素計)。
singleRowAlign
since version 2.7.0
justifiedDefines the behavior when not all columns can be filled due to too few images. Allowed values are left, center, right and justified.

更多進階選項

如果想知道更進階嘅選項,請睇下API 。

語義 HTML

Gridzy 最簡單嘅 HTML 結構係:

<div class="gridzy">
  <img src="https://loremflickr.com/640/480?random=1" alt="a random image" />
  <img src="https://loremflickr.com/480/640?random=2" alt="another random image" />
  <img src="https://loremflickr.com/640/640?random=3" alt="yet another random image" />
  <img src="https://loremflickr.com/640/480?random=4" alt="a random image again" />
  <img src="https://loremflickr.com/640/480?random=5" alt="one last random image" />
</div>

不過, Gridzy 非常靈活,你幾乎可以用任何其他 HTML 結構。只有幾點要考慮:

  • 容器元素嘅每個子元素代表網格入面嘅一個項目。
  • 容器元素同埋佢嘅子元素都可以係任何標籤類型,例如ul同li (只需要考慮重設瀏覽器預設值)。
  • 如果img元素本身唔係直接子元素,佢就需要gridzyImage呢個班。

所以呢個亦都係一個可以運作嘅 HTML 結構:

<ul class="gridzy">
  <li>
    <span>
      <img class="gridzyImage" src="https://loremflickr.com/640/480?random=1" alt="a random image" />
    </span>
  </li>
  <li>
    <span>
      <img class="gridzyImage" src="https://loremflickr.com/480/640?random=2" alt="another random image" />
    </span>
  </li>
  <li>
    <span>
      <img class="gridzyImage" src="https://loremflickr.com/640/640?random=3" alt="yet another random image" />
    </span>
  </li>
  <li>
    <span>
      <img class="gridzyImage" src="https://loremflickr.com/640/480?random=4" alt="a random image again" />
    </span>
  </li>
  <li>
    <span>
      <img class="gridzyImage" src="https://loremflickr.com/640/480?random=5" alt="one last random image" />
    </span>
  </li>
</ul>

尤其係如果你想加字幕,你需要一個先進嘅 HTML 結構。如果想知多啲,可以睇下字幕同皮膚。

字幕同皮膚

如果你想喺你嘅圖庫加字幕,你需要定義外觀。所以,有啲預先定義好嘅皮膚係可以用嘅。每個皮膚由一個 CSS 檔案組成。除咗主要嘅 Gridzy 檔案之外,你仲需要加入呢個檔案(通常喺頭部分):

<link rel="stylesheet" href="gridzy/skins/gridzySkinClassic/style.min.css" />

而且你需要一個先進嘅 HTML 結構:

<div class="gridzy gridzySkinClassic">
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/640/480?random=1" alt="a random image" />
    <figcaption class="gridzyCaption">a caption text</figcaption>
  </figure>
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/480/640?random=2" alt="another random image" />
    <figcaption class="gridzyCaption">another caption text</figcaption>
  </figure>
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/640/640?random=3" alt="yet another random image" />
    <figcaption class="gridzyCaption">yet another caption text</figcaption>
  </figure>
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/640/480?random=4" alt="a random image again" />
    <figcaption class="gridzyCaption">a caption text again</figcaption>
  </figure>
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/640/480?random=5" alt="one last random image" />
    <figcaption class="gridzyCaption">one last caption text</figcaption>
  </figure>
</div>

如果你想知 HTML 結構,可以睇下語義 HTML 。

你可以用任何類型嘅標籤嚟做字幕,只需要確保佢有gridzyCaption類別。 img元素需要gridzyImage類。

上面個例子畀你睇到皮膚gridzySkinClassic 。如果要用唔同嘅,只需要用以下其中一個取代皮膚名稱:

包括皮膚

可用嘅班級
gridzy 皮膚空白
創造自己嘅皮膚嘅好出發點
即時預覽
gridzySkinBlank
gridzySkinClassic
即時預覽
gridzySkinClassic
gridzySkinClassicLight(自2.6.0版本起已經停用)
gridzySkinFullOverlay
即時預覽
gridzySkinFullOverlay
gridzySkinFullOverlayLight(自2.6.0版本起已經停用)
gridzySkinLift
即時預覽
gridzySkinLift
gridzySkinLiftLight(自2.6.0版本起已經停用)
gridzySkinMagnifier
唔包括縮放功能
即時預覽
gridzySkinMagnifier
gridzySkinMagnifierLight(由2.6.0版本開始已經停用)

以上所有皮膚只需要一個 CSS 檔案,但係仲有其他皮膚亦都需要一個 JavaScript 檔案:

<link rel="stylesheet" href="gridzy/skins/gridzySkinBlur/style.min.css" />
<script src="gridzy/skins/gridzySkinBlur/script.min.js"></script>
可用嘅班級
gridzySkinBlur
喺 IE 入面唔運作
即時預覽
gridzySkinBlur
gridzySkinBlurLight(自2.6.0版本起已經停用)
gridzySkinCard
即時預覽
gridzySkinCard
gridzySkinCardLight(自2.6.0版本起已經停用)

皮膚選項

由2.6.0版本開始,就有選項可以配置內置嘅皮膚。根據特定嘅皮膚,你可以設定背景顏色、文字顏色同文字陰影。你只需要用 CSS 自訂屬性,你可以喺呢度睇到:

<div class="gridzy gridzySkinClassic" style="--background: rgba(0, 80, 117, .8); --color: rgb(238, 238, 238); ">
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/640/480?random=1" alt="a random image" />
    <figcaption class="gridzyCaption">a caption text</figcaption>
  </figure>
  <figure>
    <img class="gridzyImage" src="https://loremflickr.com/480/640?random=2" alt="another random image" />
    <figcaption class="gridzyCaption">another caption text</figcaption>
  </figure>

  ...

</div>

喺呢度睇下每個皮膚嘅支援變數同預設值:

gridzySkinClassic
變數預設值資訊
--backgroundrgba(0, 0, 0, .7)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值
--text-shadownone允許任何CSS 文字陰影值
gridzySkinFullOverlay
變數預設值資訊
--backgroundrgba(0, 0, 0, .7)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值
--text-shadownone允許任何CSS 文字陰影值
gridzySkinLift
變數預設值資訊
--backgroundrgba(0, 0, 0, .7)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值
--text-shadow0 0 10px rgb(0, 0, 0), 0 0 20px rgb(0, 0, 0), 0 0 30px rgb(0, 0, 0)允許任何CSS 文字陰影值
gridzySkinMagnifier
變數預設值資訊
--backgroundrgba(0, 0, 0, .7)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值

呢個皮膚唔支援--text-shadow

gridzySkinBlur
變數預設值資訊
--backgroundrgba(0, 0, 0, .7)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值
--text-shadow0 0 10px rgb(0, 0, 0)允許任何CSS 文字陰影值
gridzySkinCard
變數預設值資訊
--backgroundrgba(0, 0, 0, 1)允許任何CSS 背景值
--colorrgb(255, 255, 255)允許任何CSS 顏色值
--text-shadownone允許任何CSS 文字陰影值

所有預先定義嘅皮膚只支援簡單嘅文字字幕。你可以創造你自己同更複雜嘅皮膚。請睇下風格格仔。

風格 Gridzy

如果冇預先定義嘅皮膚適合你嘅需要,你可以自己整。

最簡單嘅方法係先複製一個現有嘅皮膚,然後將佢適應你自己嘅需要。預先定義嘅皮膚gridzySkinBlank通常會提供一個好好嘅基礎,同埋喺 CSS 檔案入面提供一個好好嘅文件:

/** SKIN Blank **/

/* all gallery items (direct children of the main container element) */
.gridzySkinBlank > .gridzyItem {
  overflow: hidden;
}

/* all gallery images (predefined in Gridzy's main CSS file, but can be overridden) */
.gridzySkinBlank .gridzyImage {
}

/* all gallery captions (just create your very own style) */
.gridzySkinBlank .gridzyCaption {
  margin: 0;
  padding: .5em;
  opacity: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  transition: opacity .5s;
  overflow: auto;
  background: rgba(0, 0, 0, .7);
  color: rgb(255, 255, 255);
}

.gridzySkinBlank > .gridzyItem:hover .gridzyCaption {
  opacity: 1;
}

喺 CSS 檔案入面,你會搵到gridzySkinBlank 、 gridzyImage同gridzyCaption呢啲類。 Gridzy 本身唔需要呢啲堂。佢哋只係用嚟做造型,所以你可以根據需要移除或者重新命名佢哋。

另外你會搵到 CSS 類gridzyItem 。 Gridzy 會自動將呢個班設定為主容器元素嘅所有直接子項目。所以,你可以用呢個班嚟為你嘅圖庫嘅項目設定風格。

心靈重要嘅事

  1. 項目內容應該係完全有反應嘅。考慮下元素大小一定會改變。用百分比值而唔係像素值。同埋睇下 Gridzy 版面配置選項autoFontSize ,佢會將字體大小綁定到項目框大小。
  2. 每個項目都應該有一個明確嘅長寬比。項目嘅長寬比永遠唔會改變,所以需要計算格。所以如果某個項目唔係自動有明確定義嘅長寬比,就手動預先定義佢(例如透過 css )。

喺某啲情況下,喺 Gridzy 初始化之前,可能會更容易為項目嘅內容設定樣式。只需暫時移除gridzy類或者 Gridzy 嘅 JavaScript 檔案就得喇。

就係咁啦!你只需要噉樣去建立你自己嘅皮膚。大部分預先定義嘅皮膚都唔會用多過呢個。

但係如果你鍾意嘅話,可能會有更多 ...

進階皮膚

Gridzy 用嘅 CSS 班多過淨係用gridzyItem 。大部分都會自動設定,你亦可以用佢哋嚟造型。

主要集裝箱類別

班名描述
gridzy
手動設定
Gridzy 會自動搵呢個班嘅元素,然後初始化佢哋。
gridzyAnimated如果選項animate係true就會自動設定。如果呢個班係手動設定,選項animate亦會自動設定為true (除非明確設定為false )。

項目類別

班名描述
gridzyItem項目開始初始化過程之後就會設定。永遠唔會被移除。
gridzyItemInitializing項目開始初始化過程之後就會設定。一旦項目完全初始化,就會被gridzyItemReady取代。唔代表圖片已經完全載入(睇gridzyItemLoading ),而係項目已經準備好顯示。
gridzyItemReady一旦項目完全初始化,就會取代gridzyItemInitializing 。永遠唔會被移除。唔代表圖片已經滿載(睇gridzyItemComplete ),但係項目已經準備好顯示。
gridzyItemLoading項目開始初始化過程之後就會設定。一旦包含嘅圖片(通常只有一張)完全載入,就會被gridzyItemComplete取代。
gridzyItemComplete當包含嘅圖片(通常只有一張)完全載入之後,就會取代gridzyItemLoading 。永遠唔會被移除。
gridzyItemInitialToVisible會喺項目第一次出現嗰陣即刻設定。當個項目完全可見之後就會移除。
gridzyItemVisible當個項目完全可見之後就會設定。當個項目開始消失之後就會移除。
gridzyItemVisibleToHidden項目開始消失之後就會設定。當個項目完全隱藏之後就會移除。
gridzyItemHidden當個項目完全隱藏之後就會設定。項目開始出現之後就會移除。
gridzyItemHiddenToVisible喺項目開始出現嗰陣就會設定(除咗第一次出現之外 – 睇 class gridzyItemInitialToVisible )。當個項目完全可見之後就會移除。
gridzyItemVisibleToVisible會喺網格入面有啲嘢改變嗰陣即刻設定,但係個項目本身就會顯示,而且會保持可見。變更動畫完成之後就會移除。

預設情況下,過程指示燈同埋圖像出現同消失嘅動畫係由 Gridzy 主 CSS 檔案預先定義嘅。

透過上面提到嘅呢啲 CSS 班,你可以為你自己嘅流程指示燈同動畫設定風格。敢於睇下 Gridzy 主要 CSS 檔案,了解你需要覆寫啲咩。唔使驚,嗰度所有嘢都有好好嘅記錄。

優化載入

如果你用好多圖片,幫 Gridzy 更快建立網格可能係一個好主意。

防止跳躍圖片

Gridzy 絕對需要所有圖像嘅長寬比嚟計算格。如果有啲圖片準備好,而其他圖片冇準備好,呢啲事實會導致跳轉元素。圖片越多,呢個效果就會越煩。

如果要防止呢個行為,只需要為所有img元素設定width同height屬性。為每張個別圖片設定正確嘅值好重要,如果唔係啲圖片會失真。正確嘅值係各自圖片嘅原始闊度同高度:

<div class="gridzy">
  <img src="https://loremflickr.com/640/480?random=1" width="640" height="480" alt="a random image" />
  <img src="https://loremflickr.com/480/640?random=2" width="480" height="640" alt="another random image" />
  <img src="https://loremflickr.com/640/640?random=3" width="640" height="640" alt="yet another random image" />
  <img src="https://loremflickr.com/640/480?random=4" width="640" height="480" alt="a random image again" />
  <img src="https://loremflickr.com/640/480?random=5" width="640" height="480" alt="one last random image" />
</div>

之後 Gridzy 可以即刻計算出個網格,而唔需要知道啲圖像嘅實際大小。

懶人載入

如果要只載入可見視窗入面嘅圖片,只需要按照以下步驟操作:

  1. 如上所述插入width同height屬性(確保用正確嘅值)
  2. 將所有src屬性換成data-gridzylazysrc ,如下所示(如果你亦有srcset屬性,請將佢哋換成data-gridzylazysrcset )。
<div class="gridzy">
  <img data-gridzylazysrc="https://loremflickr.com/640/480?random=1" width="640" height="480" alt="a random image" />
  <img data-gridzylazysrc="https://loremflickr.com/480/640?random=2" width="480" height="640" alt="another random image" />
  <img data-gridzylazysrc="https://loremflickr.com/640/640?random=3" width="640" height="640" alt="yet another random image" />
  <img data-gridzylazysrc="https://loremflickr.com/640/480?random=4" width="640" height="480" alt="a random image again" />
  <img data-gridzylazysrc="https://loremflickr.com/640/480?random=5" width="640" height="480" alt="one last random image" />
</div>

喺呢啲步驟之後,啲圖片會喺視窗附近或者入面即刻載入。

過程指標

過程指示燈係喺載入圖片嗰陣出現嘅小型動畫圖示。如果你用字幕同皮膚,過程指示燈會自動顯示。如果唔係你需要做啲調整:

  1. 將每個img元素放入一個div元素
    (或者入任何其他元素 – 睇語義 HTML了解更多)
  2. 將 class gridzyImage加入每個img元素
<div class="gridzy">
  <div><img src="https://loremflickr.com/640/480?random=1" class="gridzyImage" alt="a random image" /></div>
  <div><img src="https://loremflickr.com/480/640?random=2" class="gridzyImage" alt="another random image" /></div>
  <div><img src="https://loremflickr.com/640/640?random=3" class="gridzyImage" alt="yet another random image" /></div>
  <div><img src="https://loremflickr.com/640/480?random=4" class="gridzyImage" alt="a random image again" /></div>
  <div><img src="https://loremflickr.com/640/480?random=5" class="gridzyImage" alt="one last random image" /></div>
</div>

之後,如果圖片需要時間載入,你應該會睇到過程指示燈。

過濾器

Gridzy 入面嘅過濾器幾簡單,但係功能強大。無論你需要咩類型嘅過濾器,都好有可能係可以實現嘅。你只需要一點 CSS 同幾個 HTML 表單元素 …

以下係一個功能例子:

<div id="myFilterControls">
  <button value="*">All</button>
  <button value=".animals">Animals</button>
  <button value=".architecture">Architecture</button>
  <button value=".people">People</button>
</div>

<div class="gridzy" data-gridzy-filterControls="#myFilterControls button">
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=1" alt="a random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=1" alt="a random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=1" alt="a random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=2" alt="another random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=2" alt="another random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=2" alt="another random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=3" alt="yet another random animal image" />
</div>

我哋一步一步噉去建立佢

1.首先我哋需要 Gridzy 嘅一般 HTML 結構,同埋為所有圖庫項目設定班名,等我哋之後可以過濾佢哋

<div class="gridzy">
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=1" alt="a random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=1" alt="a random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=1" alt="a random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=2" alt="another random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=2" alt="another random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=2" alt="another random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=3" alt="yet another random animal image" />
</div>

喺呢個例子入面,我哋只會為每個元素使用一個類別。但係你亦可以為單一元素設定多個類別,如果嗰個元素應該可以透過多個篩選器搵到。

2.然後我哋加啲掣,我哋可以透過呢啲掣改過濾器

<button value="*">All</button>
<button value=".animals">Animals</button>
<button value=".architecture">Architecture</button>
<button value=".people">People</button>

<div class="gridzy">
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=1" alt="a random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=1" alt="a random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=1" alt="a random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=2" alt="another random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=2" alt="another random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=2" alt="another random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=3" alt="yet another random animal image" />
</div>

你亦可以用其他表格元素,例如選框、圓形按鈕、選取欄位等等,而唔係用掣。

正如你所見,啲掣嘅值係通常嘅 CSS 選擇器。我哋一完成,撳一下呢個掣就會顯示同嗰個 CSS 選擇器相符嘅圖片。技術上佢只係將filter選項設定為呢個值。

你亦可以用複雜得多嘅選擇器。例如:not(.animals)都可以,同埋.animals.people同.animals, .people 。

3.最後,我哋將啲掣封裝喺個div容器入面,然後將佢哋綁定去 Gridzy 圖庫,方法係設定filterControls選項:

<div id="myFilterControls">
  <button value="*">All</button>
  <button value=".animals">Animals</button>
  <button value=".architecture">Architecture</button>
  <button value=".people">People</button>
</div>

<div class="gridzy" data-gridzy-filterControls="#myFilterControls button">
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=1" alt="a random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=1" alt="a random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=1" alt="a random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=2" alt="another random animal image" />
  <img class="architecture" src="https://loremflickr.com/640/480/arch?random=2" alt="another random architecture image" />
  <img class="people" src="https://loremflickr.com/640/480/people?random=2" alt="another random people image" />
  <img class="animals" src="https://loremflickr.com/640/480/animals?random=3" alt="yet another random animal image" />
</div>

正如你所見,我哋亦都會用 CSS 選擇器嚟做filterControls選項。帶有id屬性嘅div容器只係用嚟令到啲掣更容易透過嗰個 CSS 選擇器選擇。

就係咁啦!

如果你想用 JavaScript 解決方案,可以睇下API 。

API

雖然 Gridzy 有自己嘅 API ,但係大部分嘢都可以唔使佢就可以完成,只需要操縱 DOM 。 Gridzy 會留意到佢,然後調整個網格。但係仲有個好強大嘅 API …

操縱緊 DOM

對於所有文件對象模型( DOM )操作,我哋首先需要容器元素:

// get the container element
var gridzyElement = document.querySelector('.gridzy');

加入圖片

喺將新嘅子元素加入容器元素之前,要完全構建同填充佢好重要。否則 Gridzy 偵測唔到正確嘅尺寸。

// create a new img element
var newImage = document.createElement('img');

// add src attribute (!! before you add it to the container element !!)
newImage.src = 'https://loremflickr.com/640/480';

// add it as last element to the container element
gridzyElement.appendChild(newImage);

// or as first element (or anywhere in between)
gridzyElement.insertBefore(newImage, gridzyElement.firstChild);

移除圖片

// remove the first image
gridzyElement.removeChild(gridzyElement.firstElementChild);

更改選項

// change the spaceBetween option to a specific value
gridzyElement.setAttribute('data-gridzy-spaceBetween', '10');

// change the layout option to a specific value
gridzyElement.setAttribute('data-gridzy-layout', 'waterfall');

// reset the spaceBetween option to its default value
gridzyElement.removeAttribute('data-gridzy-spaceBetween');

你可以喺MDN 網絡文件搵到更多有關操縱 DOM 嘅資訊。仲有一個關於 DOM 嘅介紹可以用。

專有 API

要用 API ,我哋首先需要 Gridzy 實例,呢個實例係綁定到 Gridzy 容器元素:

// get the Gridzy instance
var gridzyInstance = document.querySelector('.gridzy').gridzy;

攞選項

// get the value of a single option
var currentLayout = gridzyInstance.getOption('layout');

攞所有選項

// get all option values as an object
var currentOptions = gridzyInstance.getOptions();

設定選項

// set specific option values
gridzyInstance.setOptions({
  spaceBetween: 10,
  layout: 'waterfall'
});

// or reset options to their default values
gridzyInstance.setOptions({
  spaceBetween: null,
  layout: null
});

套用篩選器

套用篩選器只係設定選項filter ,可以係任何有效嘅 CSS 選擇器:

// set the filter to match items with class "animals"
gridzyInstance.setOptions({
  filter: '.animals'
});

// reset the filter to match all items again
gridzyInstance.setOptions({
  filter: '*'
});

// set the filter to match items without class "animals"
gridzyInstance.setOptions({
  filter: ':not(.animals)'
});

// set the filter to match items with class "animals" or "people"
gridzyInstance.setOptions({
  filter: '.animals, .people'
});

// set the filter to match items that have both classes, "animals" and "people"
gridzyInstance.setOptions({
  filter: '.animals.people'
});

加入回調函數

由2.4.0版本開始已經唔再使用
改用現代同更靈活嘅活動。詳情請睇下一節。

Gridzy.js 2支援部分回調函數選項。佢哋只可以透過專有 API 設定,但係就好似其他選項噉:

// same as set other options
gridzyInstance.setOptions({
  onOptionsChanged: function() {
    // will be executed each time the options have changed.
  },
  onBeforeOptionsChanged: function() {
    // will be executed, before new options take effect.
  }
});
描述
onBeforeOptionsChanged喺設定選項之前直接調用嘅回調函數。
onOptionsChanged設定選項之後直接調用嘅回調函數。
onBeforeRender喺渲染之前直接調用嘅回調函數。
onRender喺渲染之後直接調用嘅回調函數。

用事件

由2.4.0版本開始

你可以同一般 JavaScript 事件一樣噉用 Gridzy 事件:

document.addEventListener('gridzyOptionsChanged', function(event) {
    // will be executed each time the options have changed.
});

document.addEventListener('gridzyBeforeOptionsChanged', function(event) {
    // will be executed, before new options take effect.
});

有啲事件會喺 Gridzy 容器元素上面觸發,而其他事件會喺容器元素入面嘅個別項目元素上面觸發。

喺 Gridzy 容器元素上面發射嘅事件:

描述
gridzyBeforeInit喺圖庫初始化之前觸發。
gridzyInit圖庫初始化之後會觸發。
gridzyBeforeOptionsChanged喺設定選項之前觸發。
gridzyOptionsChanged設定選項之後會觸發。
gridzyBeforeRender喺渲染之前觸發。
gridzyRender渲染之後會起火。
如果你錯過咗呢度嘅任何活動,請發送支援要求。

event.target提供 Gridzy 容器元素。
event.detail.instance提供 Gridzy 實例。

喺項目元素上面發射嘅事件:

描述
gridzyItemBeforeInit喺項目初始化之前觸發。
gridzyItemInit項目初始化之後會觸發。
gridzyItemLoading當項目開始載入嗰陣就會觸發。
gridzyItemReady當項目準備好用嚟計算網格嗰陣就會觸發。所以長寬比係已知嘅,但係圖片可能仲係載入緊。
gridzyItemComplete當項目圖片滿載嗰陣就會觸發。
如果你錯過咗呢度嘅任何活動,請發送支援要求。

event.target提供相應嘅項目元素。
event.detail.instance提供 Gridzy 實例。

API – 全域

有啲函數係獨立於 Gridzy 實例。所以我哋唔需要之前攞個實例。

攞預設選項

// get the default value of a single option
var defaultLayout = Gridzy.getDefaultOption('layout');

攞所有預設選項

// get all default option values as an object, including the default layout options of the default layout
var defaultOptions = Gridzy.getDefaultOptions();

// or get all default option values, including the default layout options of a specific layout
var defaultOptions = Gridzy.getDefaultOptions('waterfall');

設定預設選項

呢個唔會設定所有已經初始化咗嘅 Gridzy 實例嘅選項,而係定義新實例嘅基礎。但係,如果現有實例嘅選項會透過值null重設,佢會用新嘅預設值。

// set default option values
Gridzy.setDefaultOptions({
  spaceBetween: 10,
  layout: 'waterfall'
});

如果你喺 DOM 準備好之前調用佢,你可以用呢個嚟全域設定選項。所以你唔需要再將選項設定為屬性。

自動化

只要 Gridzy.js 2嘅所有自動化都係有效嘅,就唔需要以下方法。但係可以閂咗啲自動化功能,以更傳統嘅方式使用 Gridzy :

// disable automatic initializations
Gridzy.setDefaultOptions({
  autoInitOnDomReady: false,
  autoInitOnDomMutation: false
});

// or switch off all mutation observers
Gridzy.setDefaultOptions({
  autoInitOnDomMutation: false,
  autoSyncChildListMutation: false,
  autoSyncAttributesMutation: false,
  autoSyncChildClassMutation: false,
  autoSyncImageMutation: false
});

由於邏輯原因,呢啲選項有啲唔可以透過data-gridzy-屬性或者setOptions()設定:

預設值描述
autoInitOnDomReadytrue喺頁面載入嗰陣偵測到有 class gridzy元素,然後初始化佢哋。

可通過Gridzy.setDefaultOptions()設置
autoInitOnDomMutation
突變觀察者
true喺 DOM 入面出現嗰陣,就會用gridzy類嘅新元素偵測到佢哋,然後將佢哋初始化。 (例如使用頁面轉換)

可通過Gridzy.setDefaultOptions()設置
autoSyncChildListMutation
突變觀察者
true偵測子元素(圖片)係咪加入定係移除,同埋自動更新網格。

可以透過data-grizy-設定, new Gridzy() , setOptions() , Gridzy.setDefaultOptions()
useOptionAttributestrue定義data-gridzy-屬性係咪一般使用。如果呢個係false話,只可以透過專有 API 設定選項。

可以透過new Gridzy() 、 setOptions() 、 Gridzy.setDefaultOptions()設定
autoSyncAttributesMutation
突變觀察者
true偵測data-gridzy-屬性嘅變化,同埋自動更新網格。亦會偵測到gridzyAnimated班係咪加入咗或者移除咗,同埋係咪發生咗style屬性嘅變更。如果useOptionAttributes係false ,佢只會偵測到style屬性嘅變更。

可以透過data-grizy-設定, new Gridzy() , setOptions() , Gridzy.setDefaultOptions()
autoSyncChildClassMutation
突變觀察者
true偵測所有子元素(圖片)嘅class屬性嘅變化。關於過濾器嘅相關。

可以透過data-grizy-設定, new Gridzy() , setOptions() , Gridzy.setDefaultOptions()
autoSyncImageMutation
突變觀察者

由2.5.0版本開始
true偵測所有圖片嘅src屬性嘅變化。同 React 或者類似嘅庫一齊用嗰陣相關。

可以透過data-grizy-設定, new Gridzy() , setOptions() , Gridzy.setDefaultOptions()
autoRenderOnResize
調整大小觀察者

自從2.6.0版本之後
true偵測元素寬度嘅變化。關於回應式設計。 (喺2.6.0版本之前,用咗個窗口調整大小嘅事件監聽器)

可以透過data-grizy-設定, new Gridzy() , setOptions() , Gridzy.setDefaultOptions()

手動初始化

// initialize a new Gridzy instance
var gridzyInstance = new Gridzy(document.querySelector('.gridzy'), {
  spaceBetween: 10,
  layout: 'waterfall'
});

破壞個實例

大致上講,同初始化 Gridzy 相反。

gridzyInstance.destroy();
gridzyInstance = null;

手動同步子清單

// after child elements (images) are added to or removed from the DOM.
gridzyInstance.syncChildList();

手動同步單一子元素

通常唔需要,除非好少見。

// after child elements (images) are added to the DOM and only one of them should be synced to Gridzy.
gridzyInstance.syncChild(childElement);

手動同步屬性

請注意, useOptionAttributes選項必須為true 。

// after data-gridzy- attributes have changed
gridzyInstance.syncAttributes();

強制渲染

通常唔需要,就算所有自動功能都閂咗

gridzyInstance.render();