เว็บไซต์ได้รับการแปลโดยอัตโนมัติ หากไม่ชัดเจน โปรดดูเอกสารภาษาอังกฤษ
การใช้งานพื้นฐาน
อัปโหลดไฟล์ 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>ตัวเลือกหลัก
Justified ตัวเลือกเค้าโครง
Waterfall ตัวเลือกเค้าโครง
ตัวเลือกขั้นสูงเพิ่มเติม
สำหรับตัวเลือกขั้นสูงเพิ่มเติม โปรดดู API
HTML เชิงความหมาย
โครงสร้าง HTML ที่เรียบง่ายที่สุดของ Gridzy คือ:
<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 ลองดูที่ Semantic HTML
คุณสามารถใช้แท็กประเภทใดก็ได้สำหรับคำอธิบายภาพ เพียงตรวจสอบให้แน่ใจว่ามีคลาส gridzyCaption องค์ประกอบ img จำเป็นต้องมีคลาส gridzyImage
ตัวอย่างด้านบนแสดงสกิน gridzySkinClassic หากต้องการใช้สกินอื่น ให้เปลี่ยนชื่อสกินเป็นสกินใดสกินหนึ่งดังต่อไปนี้:
รวมสกิน
| ชั้นเรียนที่มีให้บริการ | |
|---|---|
| กริดซี่สกินแบลงค์ จุดเริ่มต้นที่ดีสำหรับการสร้างสกินของตัวเอง ดูตัวอย่างสด | gridzySkinBlank |
| gridzySkinClassic ดูตัวอย่างสด | gridzySkinClassic(เลิกใช้ตั้งแต่เวอร์ชัน 2.6.0) |
| gridzySkinFullOverlay ดูตัวอย่างสด | gridzySkinFullOverlay(เลิกใช้ตั้งแต่เวอร์ชัน 2.6.0) |
| gridzySkinLift ดูตัวอย่างสด | gridzySkinLift(เลิกใช้ตั้งแต่เวอร์ชัน 2.6.0) |
| gridzySkinMagnifier ไม่รวมฟังก์ชันการซูม ดูตัวอย่างสด | gridzySkinMagnifier(เลิกใช้ตั้งแต่เวอร์ชัน 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(เลิกใช้ตั้งแต่เวอร์ชัน 2.6.0) |
| gridzySkinCard ดูตัวอย่างสด | gridzySkinCard(เลิกใช้ตั้งแต่เวอร์ชัน 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
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, .7) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
--text-shadow | none | อนุญาตให้ ใช้ค่า CSS text-shadow ใดๆ |
gridzySkinFullOverlay
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, .7) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
--text-shadow | none | อนุญาตให้ ใช้ค่า CSS text-shadow ใดๆ |
gridzySkinLift
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, .7) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
--text-shadow | 0 0 10px rgb(0, 0, 0), 0 0 20px rgb(0, 0, 0), 0 0 30px rgb(0, 0, 0) | อนุญาตให้ ใช้ค่า CSS text-shadow ใดๆ |
gridzySkinMagnifier
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, .7) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
สกินนี้ไม่รองรับ --text-shadow
gridzySkinBlur
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, .7) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
--text-shadow | 0 0 10px rgb(0, 0, 0) | อนุญาตให้ ใช้ค่า CSS text-shadow ใดๆ |
gridzySkinCard
| ตัวแปร | ค่าเริ่มต้น | ข้อมูล |
|---|---|---|
--background | rgba(0, 0, 0, 1) | อนุญาตให้ ใช้ค่าพื้นหลัง CSS ใด ๆ |
--color | rgb(255, 255, 255) | อนุญาตให้ ใช้ค่าสี CSS ใด ๆ |
--text-shadow | none | อนุญาตให้ ใช้ค่า CSS text-shadow ใดๆ |
สกินที่กำหนดไว้ล่วงหน้าทั้งหมดรองรับเฉพาะคำบรรยายข้อความธรรมดาเท่านั้น คุณสามารถสร้างสกินของคุณเองได้และซับซ้อนยิ่งขึ้น โปรดดูที่ Style 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 จะตั้งค่าคลาสนี้ให้กับองค์ประกอบย่อยทั้งหมดขององค์ประกอบคอนเทนเนอร์หลักโดยอัตโนมัติ ดังนั้น คุณสามารถใช้คลาสนี้เพื่อกำหนดสไตล์ให้กับไอเท็มในแกลเลอรีของคุณได้
จิตใจสิ่งสำคัญ
- เนื้อหาของรายการควรตอบสนองได้เต็มที่ โปรดทราบว่าขนาดขององค์ประกอบจะเปลี่ยนแปลงอย่างแน่นอน ให้ใช้ค่าเปอร์เซ็นต์แทนค่าพิกเซล และลองดูตัวเลือกเค้าโครง Gridzy
autoFontSizeซึ่งจะผูกขนาดตัวอักษรเข้ากับขนาดกล่องรายการ - แต่ละรายการควรมีอัตราส่วนภาพที่กำหนดไว้อย่างชัดเจน อัตราส่วนภาพของรายการจะไม่เปลี่ยนแปลงและจำเป็นต่อการคำนวณกริด ดังนั้นหากรายการไม่มีอัตราส่วนภาพที่กำหนดไว้อย่างชัดเจนโดยอัตโนมัติ ให้กำหนดไว้ล่วงหน้าด้วยตนเอง (เช่น ผ่าน CSS)
ในบางกรณี อาจง่ายกว่าที่จะกำหนดรูปแบบของเนื้อหาของรายการก่อนที่ Gridzy จะเริ่มต้น เพียงลบคลาส gridzy หรือไฟล์ javascript ของ Gridzy ชั่วคราวสำหรับกรณีนี้
เท่านี้ก็เรียบร้อยแล้ว! นั่นคือทั้งหมดที่คุณต้องการเพื่อสร้างสกินของคุณเอง สกินที่กำหนดไว้ล่วงหน้าส่วนใหญ่ไม่ได้ใช้สกินนี้มากไปกว่านี้
แต่ก็อาจมีมากกว่านั้นได้หากคุณต้องการ…
สกินขั้นสูง
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 | จะถูกตั้งค่าทันทีที่รายการเริ่มปรากฏ (ยกเว้นการปรากฏครั้งแรก – ดูคลาส gridzyItemInitialToVisible ) จะถูกลบออกทันทีที่รายการปรากฏจนครบถ้วน |
gridzyItemVisibleToVisible | จะถูกตั้งค่าทันทีที่มีการเปลี่ยนแปลงบางอย่างในกริด แต่รายการนั้นจะยังมองเห็นได้ จะถูกลบออกทันทีที่แอนิเมชั่นการเปลี่ยนแปลงเสร็จสิ้น |
โดยค่าเริ่มต้น ตัวบ่งชี้กระบวนการและแอนิเมชันสำหรับภาพที่ปรากฏและหายไปถูกกำหนดไว้ล่วงหน้าโดยไฟล์ CSS หลักของ Gridzy
ด้วยคลาส CSS ที่กล่าวถึงข้างต้นนี้ คุณสามารถออกแบบตัวบ่งชี้กระบวนการและแอนิเมชันของคุณเองได้ ลองดูไฟล์ CSS หลักของ Gridzy เพื่อทำความเข้าใจว่าคุณจะต้องแก้ไขอะไรบ้าง ไม่ต้องกังวล ทุกอย่างมีการบันทึกไว้เป็นอย่างดี
เพิ่มประสิทธิภาพการโหลด
หากคุณใช้รูปภาพจำนวนมาก การช่วยให้ Gridzy สร้างกริดได้เร็วขึ้นก็อาจเป็นความคิดที่ดี
ป้องกันภาพกระโดด
Gridzy จำเป็นต้องใช้อัตราส่วนภาพของภาพทั้งหมดเพื่อคำนวณกริด ข้อเท็จจริงดังกล่าวส่งผลให้องค์ประกอบต่างๆ กระโดดข้ามไปหากภาพบางภาพพร้อมแล้วและบางภาพไม่พร้อม ยิ่งมีภาพมากขึ้นเท่าไร เอฟเฟกต์ดังกล่าวก็จะยิ่งน่ารำคาญมากขึ้นเท่านั้น
เพื่อป้องกันพฤติกรรมดังกล่าว เพียงตั้งค่าแอตทริบิวต์ width และ height สำหรับองค์ประกอบ img ทั้งหมด สิ่งสำคัญคือต้องตั้งค่าที่ถูกต้องสำหรับรูปภาพแต่ละภาพ มิฉะนั้น รูปภาพจะผิดเพี้ยน ค่าที่ถูกต้องคือความกว้างและความสูงดั้งเดิมของรูปภาพที่เกี่ยวข้อง:
<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 สามารถคำนวณกริดได้ทันทีโดยไม่จำเป็นต้องทราบขนาดจริงของรูปภาพ
การโหลดแบบขี้เกียจ
หากต้องการโหลดเฉพาะรูปภาพที่อยู่ในช่องมองภาพที่มองเห็นได้ ให้ทำตามขั้นตอนเหล่านี้:
- แทรกแอตทริบิวต์
widthและheightตามที่อธิบายไว้ข้างต้น (ตรวจสอบให้แน่ใจว่าใช้ค่าที่ถูกต้อง) - แทนที่แอตทริบิวต์
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>หลังจากขั้นตอนเหล่านี้ รูปภาพจะโหลดทันทีเมื่ออยู่ใกล้หรืออยู่ภายในช่องมองภาพ
ตัวบ่งชี้กระบวนการ
ตัวบ่งชี้กระบวนการคือไอคอนเคลื่อนไหวขนาดเล็กที่ปรากฏขึ้นในขณะที่กำลังโหลดภาพ หากคุณใช้ คำบรรยายและสกิน ตัวบ่งชี้กระบวนการจะแสดงโดยอัตโนมัติ มิฉะนั้น คุณจะต้องปรับเปลี่ยนบางอย่าง:
- ใส่องค์ประกอบ
imgแต่ละองค์ประกอบลงในองค์ประกอบdiv
(หรือในองค์ประกอบอื่น ๆ – ดู HTML เชิงความหมาย สำหรับข้อมูลเพิ่มเติม) - เพิ่มคลาส
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. ขั้นแรก เราต้องมีโครงสร้าง HTML ทั่วไปของ Gridzy และตั้งชื่อคลาสสำหรับรายการในแกลเลอรีทั้งหมด ดังนั้นเราจึงสามารถกรองรายการเหล่านี้ในภายหลังได้
<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 ด้วย คอนเทนเนอร์ div ที่มีแอตทริบิวต์ id ทำหน้าที่เพียงเพื่อให้เลือกปุ่มได้ง่ายขึ้นผ่านตัวเลือก CSS เท่านั้น
แค่นั้นแหละ!
หากคุณชอบโซลูชัน JavaScript โปรดดู API
เอพีไอ
แม้ว่า Gridzy จะมี API ของตัวเอง แต่ก็สามารถทำสิ่งต่างๆ ได้มากมายโดยไม่ต้องใช้ API นี้ เพียงแค่จัดการ DOM เท่านั้น Gridzy จะสังเกตเห็นและปรับแต่งกริด แต่ยังมี API ที่ทรงพลังมากอีกด้วย …
การจัดการ DOM
สำหรับการจัดการ Document Object Model (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');คุณสามารถค้นหาข้อมูลเพิ่มเติมเกี่ยวกับการจัดการ DOM ได้ที่ เอกสารบนเว็บของ MDN นอกจากนี้ยังมี คำแนะนำเกี่ยวกับ 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.
}
});การใช้กิจกรรม
ตั้งแต่เวอร์ชัน 2.4.0
คุณสามารถใช้เหตุการณ์ Gridzy ได้เหมือนกับเหตุการณ์ JavaScript ทั่วไป:
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:
event.target ให้องค์ประกอบคอนเทนเนอร์ Gridzy
event.detail.instance จัดเตรียมอินสแตนซ์ Gridzy
เหตุการณ์ที่เกิดขึ้นกับองค์ประกอบของรายการ:
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() เนื่องจากเหตุผลทางตรรกะ:
เริ่มต้นด้วยตนเอง
// 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();