自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)現(xiàn)瀑布流布局,就這幾行代碼?

開發(fā) 前端
瀑布流布局是一種比較流行的頁面布局方式,表現(xiàn)為參差不齊的多欄卡片。跟網(wǎng)格布局相比,顯得更靈動(dòng),更具藝術(shù)氣息。

[[416106]]

瀑布流布局是一種比較流行的頁面布局方式,表現(xiàn)為參差不齊的多欄卡片。跟網(wǎng)格布局相比,顯得更靈動(dòng),更具藝術(shù)氣息。

瀑布流布局

實(shí)現(xiàn)瀑布流布局的方式有多種,比如multi-column布局,grid布局,flex 布局等。但是這些實(shí)現(xiàn)方式都有各自的局限性,代碼也略復(fù)雜。

其實(shí),有個(gè)最原始、最簡單,也是兼容性最好的實(shí)現(xiàn)方式,那就是使用絕對(duì)定位。瀑布流布局的元素是一些等寬不等高的卡片,只要根據(jù)元素的實(shí)際寬高計(jì)算出自己的坐標(biāo)位置就行了。

要計(jì)算坐標(biāo)自然要用到 JavaScript,這就不是純 CSS 方案,對(duì)某些前端極客來講顯得不那么純粹。不過只要理清思路了,也用不了幾行代碼。本文就給出最近實(shí)現(xiàn)的一個(gè)版本。

  1. // 計(jì)算每個(gè)卡片的坐標(biāo) 
  2. export function calcPositions({ columns = 2, gap = 7, elements }) { 
  3.   if (!elements || !elements.length) { 
  4.     return []; 
  5.   } 
  6.   const y = []; //上一行卡片的底部縱坐標(biāo)數(shù)組,用于找到新卡片填充位置 
  7.   const positions = []; // 每個(gè)卡片的坐標(biāo)數(shù)組 
  8.   elements.forEach((item, index) => { 
  9.     if (y.length < columns) { // 還未填滿一行 
  10.       y.push(item.offsetHeight); 
  11.       positions.push({ 
  12.         left: (index % columns) * (item.offsetWidth + gap), 
  13.         top: 0 
  14.       }); 
  15.     } else { 
  16.       const min = Math.min(...y); // 最小縱坐標(biāo) 
  17.       const idx = y.indexOf(min); // 縱坐標(biāo)最小的卡片索引 
  18.       y.splice(idx, 1, min + gap + item.offsetHeight); // 替換成新卡片的縱坐標(biāo) 
  19.       positions.push({ 
  20.         left: idx * (item.offsetWidth + gap), 
  21.         topmin + gap 
  22.       }); 
  23.     } 
  24.   }); 
  25. // 由于采用絕對(duì)定位,容器是無法自動(dòng)撐開的。因此需要計(jì)算實(shí)際高度,即最后一個(gè)卡片的top加上自身高度 
  26.   return { positions, containerHeight: positions[positions.length - 1].top + elements[elements.length - 1].offsetHeight }; 

上面這段代碼的作用就是計(jì)算每個(gè)卡片的left、top,以及容器的總高度。關(guān)鍵位置都有注釋,應(yīng)該不難理解。

有了這幾行核心代碼,要想封裝成瀑布流組件就很容易了。以 Vue 為例,可以這樣封裝:

MasonryLite.vue

  1. <template> 
  2.   <div class="masonry-lite"
  3.     <slot></slot> 
  4.   </div> 
  5. </template> 
  6. <script> 
  7. import { calcPositions } from './index.js'
  8. export default { 
  9.   name'MasonryLite'
  10.   props: { 
  11.     gap: { 
  12.       type: Number, 
  13.       default: 12, 
  14.     }, 
  15.     columns: { 
  16.       type: Number, 
  17.       default: 2, 
  18.     }, 
  19.   }, 
  20.   data() { 
  21.     return {}; 
  22.   }, 
  23.   mounted() { 
  24.     this.doLayout(); 
  25.   }, 
  26.   methods: { 
  27.     doLayout() { 
  28.       const children = [...this.$el.querySelectorAll('.masonry-item')]; 
  29.       if (children.length === 0) { 
  30.         return
  31.       } 
  32.       const { positions, containerHeight } = calcPositions({ 
  33.         elements: children, 
  34.         columns: this.columns, 
  35.         gap: this.gap, 
  36.       }); 
  37.       children.forEach((item, index) => { 
  38.         item.style.cssText = `left:${positions[index].left}px;top:${positions[index].top}px;`; 
  39.       }); 
  40.       this.$el.style.height = `${containerHeight}px`; 
  41.     }, 
  42.   }, 
  43. }; 
  44. </script> 
  45. <style lang="scss" scoped> 
  46. .masonry-lite{ 
  47.   position: relative
  48. .masonry-item { 
  49.   position: absolute
  50. </style> 

使用組件:

  1. <MasonryLite> 
  2.   <div class="product-card masonry-item" v-v-for="(item, index) in items" :key="index"
  3.     <img :src="item.imageUrl" /> 
  4.     <header>{{ item.title }}</header> 
  5.   </div> 
  6. </MasonryLite> 

不過這樣其實(shí)還會(huì)有點(diǎn)問題,就是doLayout的執(zhí)行時(shí)機(jī)。因?yàn)樵摲桨富诮^對(duì)定位,需要元素在渲染完成后才能獲取到實(shí)際寬高。如果卡片內(nèi)有延遲加載的圖片或者其他動(dòng)態(tài)內(nèi)容,高度會(huì)發(fā)生變化。這種情況下就需要在DOM更新后主動(dòng)調(diào)用一次doLayout重新計(jì)算布局。

如果大家有更好的實(shí)現(xiàn)方案,歡迎交流!

代碼倉庫:https://github.com/kaysonli/masonry-lite

npm 包:masonry-lite

如果覺得對(duì)你有幫助,幫忙點(diǎn)個(gè)不要錢的star。

本文轉(zhuǎn)載自微信公眾號(hào)「1024譯站」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系1024譯站公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 1024譯站
相關(guān)推薦

2013-02-19 10:24:47

瀑布流布局CSS

2024-08-19 14:01:00

2012-05-02 13:53:00

JavaScript

2020-08-10 06:36:21

強(qiáng)化學(xué)習(xí)代碼深度學(xué)習(xí)

2021-10-18 09:09:16

數(shù)據(jù)庫

2021-06-22 09:55:05

代碼圖像技術(shù)

2020-09-29 10:09:43

Python文字識(shí)別編程語言

2021-04-15 11:10:40

GitHub代碼開發(fā)者

2020-07-07 07:55:53

web app數(shù)據(jù)科學(xué)機(jī)器學(xué)習(xí)

2024-09-03 17:04:15

前端算法布局

2023-11-01 10:36:19

2021-01-29 10:57:57

新基建政策解讀智慧物流

2020-10-24 20:10:40

Python 開發(fā)編程語言

2024-06-04 14:20:47

數(shù)字化轉(zhuǎn)型數(shù)字化

2024-11-12 13:41:49

2021-12-02 09:31:22

Python 代碼進(jìn)度條

2021-02-23 07:01:24

js小游戲技術(shù)

2023-10-27 11:38:09

PythonWord

2024-01-10 14:45:46

Redis數(shù)據(jù)庫存儲(chǔ)

2023-12-24 22:52:26

PythonPPT代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)