@appnest/masonry-layout

中文文档
2.2.3发布于 2023/09/29MITTypeScriptESM + CJS零依赖近 30 天下载 1万

An efficient and fast web component that gives you a beautiful masonry layout

原始使用文档
文档目录12 节
  1. ➤ Installation
  2. ➤ Example
  3. ➤ Customize
  4. Amount of columns
  5. Gap between columns and rows
  6. Max column width
  7. Change debounce time
  8. ➤ Trigger layout
  9. ➤ Overview
  10. masonry-layout
  11. ➤ Support for old browsers
  12. ➤ License

@appnest/masonry-layout

Downloads per month NPM Version Dependencies Contributors Published on webcomponents.org

An efficient and fast web component that gives you a beautiful masonry layout
Go here to see a demo https://appnest-demo.firebaseapp.com/masonry-layout.


  • Simple: Works right out of the box (just add it to your markup)
  • Lightweight: Super small (1kb minified & gzipped)
  • Zero dependencies: Created using only vanilla js - no dependencies and framework agnostic!
  • Customizable: Can customize almost everything (eg. columns, transitions, gap).
  • User friendly: Automatically re-distribute items when the size of the grid changes or new elements are added
  • Performant: Efficient & fast - Build with performance in mind

-----------------------------------------------------

➤ Installation

npm i @appnest/masonry-layout

If you prefer to use umd bundle you can load https://unpkg.com/@appnest/masonry-layout/umd/masonry-layout.min.js instead.

-----------------------------------------------------

➤ Example

Import @appnest/masonry-layout somewhere in your code and you're ready to go! Simply add the masonry-layout element to your html and then add your elements in between the start and closing tags.

<masonry-layout>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</masonry-layout>

-----------------------------------------------------

➤ Customize

Customize the functionality by applying the attributes.

Amount of columns

The cols attribute specifies the amount of columns. The default value is auto which ensures to distribute the elements based on the available width.

<masonry-layout cols="5">
  ...
</masonry-layout>

Gap between columns and rows

The gap attribute specifies how many pixels the gap between the elements should be. The default value is 24px.

<masonry-layout gap="50">
  ...
</masonry-layout>

Max column width

The maxcolwidth specifies how many pixels a column can maximum have when the cols attribute is set to auto. The default value is 400px.

<masonry-layout maxcolwidth="200">
  ...
</masonry-layout>

Change debounce time

The debounce attribute specifies the amount of time in ms the layout reflow debounces each time the size of the masonry layout changes. This reflow is debounced to avoid the layout algorithm being invoked too many times in a row. The default value is 300ms.

<masonry-layout debounce="500">
  ...
</masonry-layout>

-----------------------------------------------------

➤ Trigger layout

If you want to force layout to can call the layout() function on the masonry layout.

document.querySelector("masonry-layout").layout();

If you wish to debounce the layout you can call the scheduleLayout() function instead.

document.querySelector("masonry-layout").scheduleLayout();

-----------------------------------------------------

➤ Overview

Here's a complete overview of the element.

masonry-layout

Masonry layout web component. It places the slotted elements in the optimal position based on the available vertical space, just like mason fitting stones in a wall.

Example

<masonry-layout><div class="item"></div><div class="item"></div></masonry-layout>

Properties

PropertyAttributeTypeDescription
colscolsnumber | "auto"The amount of columns.
debouncedebouncenumberThe ms of debounce when the element resizes.
gapgapnumberThe gap in pixels between the columns.
layout
maxColWidthmaxcolwidthnumberThe maximum width of each column if cols are set to auto.
onResize
onSlotChange

Methods

MethodTypeDescription
layout(): voidLayouts the elements.
onResize(entries?: object | undefined): voidEach time the element resizes we need to schedule a layout
if the amount available columns has has changed.
onSlotChange(): void
renderCols(colCount: number): voidRender X amount of columns.
scheduleLayout(ms?: number): voidSchedules a layout.

Slots

NameDescription
Items that should be distributed in the layout.

CSS Shadow Parts

PartDescription
columnEach column of the masonry layout.
column-indexThe specific column at the given index (eg. column-0 would target the first column and so on)

-----------------------------------------------------

➤ Support for old browsers

If you are going to support older browsers that doesn't support Custom Elements, Shadow Dom or ResizeObservers you should polyfill the features. You can do this very easily by using the brilliant polfiller service. This can be done in one line of code by adding the following to your index.html before you import the masonry-layout.

<script crossorigin src="https://polyfill.app/api/polyfill?features=es,template,shadow-dom,custom-elements,resizeobserver"></script>

-----------------------------------------------------

➤ License

Licensed under MIT.

版本与发布记录

@appnest/masonry-layout 共发布 31 个版本,首次发布于 2019/01/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.3latest2023/09/293 年前
2.2.22023/09/293 年前
2.0.82020/03/236 年前
2.0.72020/02/186 年前
2.0.62019/12/046 年前
2.0.52019/12/046 年前

关于 @appnest/masonry-layout 的常见问题

@appnest/masonry-layout 是什么?

An efficient and fast web component that gives you a beautiful masonry layout

如何安装 @appnest/masonry-layout?

在项目目录运行 npm install @appnest/masonry-layout,或使用 pnpm add / yarn add / bun add。

@appnest/masonry-layout 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@appnest/masonry-layout 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@appnest/masonry-layout 可以商用吗?

@appnest/masonry-layout 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@appnest/masonry-layout 的下载量有多少?

根据 npm 官方下载统计,@appnest/masonry-layout 在 2026/10/07 – 2026/11/05 这 30 天内被下载 10,221 次,日均约 341 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appnest/masonry-layout 还在维护吗?

最新版本 2.2.3 发布于 2023/09/29(3 年前);至今共发布 31 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/01/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。