Bricks.js简单方便的瀑布流插件

问说网 · 发布于 2016-03-17 · 字数8100 · 浏览 2106 · 评论 0

Bricks.js是一个用户固定宽度元素堆砌的插件,简单来说就是一个瀑布流插件,作者的说明总是这么绕口,不过也限制了是固定宽度的元素,所以插件的功能很简单,没有什么动画效果。

Bricks.js简单方便的瀑布流插件

通用问题

Current masonry solutions lack performance, and are bloated with unnecessary features. In addition, they:

  • Are built on dependencies, like jQuery
  • Mandate HTML markup, or require a CSS stylesheet
  • Position elements with transforms, making animation difficult

Bricks.js特点

Bricks capitalizes on batched reading of layout properties for blazing fast packing time, no matter how many items. Besides performance, it features:

  • No required HTML markup or CSS stylesheet
  • Minimal, powerful, and readable configuration
  • Optional resize handling
  • Optimized handling of dynamically added items

安装

Bricks was developed with a modern JavaScript workflow in mind. To use it, it’s recommended you have a build system in place that can transpile ES6, and bundle modules. For a minimal boilerplate that does so, check out outset.

Using NPM, install Bricks.js, and add it to your package.json dependencies.

$ npm install bricks.js --save

Refer to the releases page for version specific information.

Instantiate

Simply import Bricks, then instantiate it.

It’s recommended that you assign your Bricks instance to a variable. Using your instance, you can:

  • enable and disable the resize handler
  • add and remove event handlers
  • accommodate dynamically added elements
// import Bricks
import Bricks from 'bricks.js'

// create an instance
const instance = Bricks({
  // ...
})

Parameters passed to the constructor are detailed below.

参数

Note that all parameters are required:

container

A CSS selector that matches the grid wrapper.

const instance = Bricks({
  container: '.selector'
})

Note that the direct children of this element must be the grid items.

packed

An attribute added to items already positioned within the grid.

const instance = Bricks({
  packed: 'data-packed'
})

Note that if the attribute is not prefixed with data-, it will be added.

sizes

An array of objects describing the grid’s properties at different breakpoints.

When defining your sizes, note the following:

  • Sizes must be listed smallest to largest
  • Sizes must use min-width media queries (any unit)
  • Width of the grid items at each breakpoint should be set in your CSS (in px)
  • The size without the mq property is assumed to be your smallest breakpoint, and must appear first
// mq      - the minimum viewport width (any unit)
// columns - the number of vertical columns
// gutter  - the space (in px) between the columns and grid items

const sizes = [
  { columns: 2, gutter: 10 },
  { mq: '768px', columns: 3, gutter: 25 },
  { mq: '1024px', columns: 4, gutter: 50 }
]

const instance = Bricks({
  sizes: sizes
})

API / Events

Bricks instances are extended with Knot.js, a browser-based event emitter. Use the event emitter syntax to add and remove handlers for the events emitted by the API methods. Review the emitter syntax here.

Bricks exposes the following methods, and corresponding events:

Note that all methods, including those from the event emitter, are chainable.

.pack()

Used to pack all elements within the container.

// pack ALL grid items
instance.pack()

// 'pack' is emitted when ALL items have been packed
instance.on('pack', () => {
  // ...
})

Note that it should be called when creating your instance, to pack the initial items.

.update()

Used to pack elements without the packed attribute within the container.

// pack NEW grid items
instance.update()

// 'update' is emitted when NEW items have been packed
instance.on('update', () => {
  // ...
})

Note that this is the preferred method for handling dynamically added items, because it will only operate on items that have not yet been packed (i.e. don’t have the packed attribute).

.resize(flag)

Used to add or remove the resize event handler. It’s recommended that you add the resize handler when you create your instance.

// add or remove the resize handler
instance
  .resize(true)       // 'true' adds it
  .resize(false)      // 'false' removes it

// 'resize' is emitted when resizing has resulted in a new matching 'size' object
instance.on('resize', size => {
  // 'size' is the newly matching size object
  // ...
})

Note that the resize handler fires the pack method if the resulting screen size matches a size parameter other than the current one. In this case, the pack event will be fired immediately before the resize event. Use the resize event only for breakpoint specific code, not code meant for when the grid has been packed.

Browser Support

Bricks depends on the following browser APIs:

Consequently, it supports the following natively:

  • Chrome 24+
  • Firefox 23+
  • Safari 6.1+
  • Opera 15+
  • IE 10+
  • iOS Safari 7.1+
  • Android Browser 4.4+

To support older browsers, consider including polyfills/shims for the APIs listed above. There are no plans to include any in the library, in the interest of file size.

相关链接

本文系作者 问说网 授权问说网发表,并经问说网编辑,转载请注明出处和 本文链接

相关文章

  • 2013-11-22CSS3属性值中的content和attr的用法介绍
  • 2015-11-30浅谈多终端屏幕特性[转]
  • 2016-05-25绿毛水怪图标的简单绘制教程
  • 2016-06-11jQuery Collapse轻量级的手风琴菜单插件
  • 2016-06-12jQuery NailThumb高分辨率缩略图显示插件
  • 2016-06-24分享常用响应式Web UI开发框架
  • 2016-07-01SweetTooltip基于jQuery和CSS3链接工具提示插件
  • 2016-07-05angularAMD使用RequireJS和AngularJS快速构建WebApp
  • 发布评论

    为您推荐

    问说网 · 发布于 2015-11-07

    Solr基于Lucene的企业级搜索引擎服务器

    Solr是一个基于Lucene的Java搜索引擎服务器。Solr 提供了层面搜索、命中醒目显示并且支持多种输出格式,它易于安装和配置,而且附带了一个基于 HTT…

    问说网 · 发布于 2015-12-26

    PNotify基于JavaScript简单易用消息提示插件

    PNotify 是一个 JavaScript 通知插件,前身为 Pines Notify。它旨在提供无与伦比的灵活性,同时很容易使用。它可以提供无阻塞的通知,允…

    问说网 · 发布于 2016-03-08

    Jsoup功能强大的JavaHTML解析器

    Jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuer…

    问说网 · 发布于 2016-03-08

    使用JavaScript获取用户终端信息

    这里声明了一个名为Client的全局变量,用于保存相关信息。匿名函数内部定义了一个局部变量engine,它是一个包含默认设置的对象字面量。在这个对象字面量中,每…

    2014年网页设计的10个新趋势

    如果你想知道2014年首屈一指的网站设计趋势。那就是在网站的首页放一张大图片,并配以少量的文字。无论是背景中一张模糊的图片还是精心制作的版本。

    • 2014年网页设计的10个新趋势
    • 2014年网页设计的10个新趋势
    • 2014年网页设计的10个新趋势
    • 2014年网页设计的10个新趋势
    夙願 · 发布于 2016-05-17 · 浏览 1388 · 评论 0
    angularAMD使用RequireJS和AngularJS快速构建WebApp
    问说网 · 发布于 2016-07-05

    angularAMD使用RequireJS和AngularJS快速构建WebApp

    angularAMD是作者 marcoslin 使用 RequireJS + AngularJS开发的前端mvvm框架,因此你可以使用它快速创建一款Web Ap…

    Vincent · 发布于 2016-07-06

    类和对象

    定义 1. 类是用于描述同一类形的对象的一个抽象的概念,类中定义了这一类对象所因具有的静态属性(attibute)和动态行为(method) 2. 类可以看成一…

    Vincent · 发布于 2016-07-06

    正则表达式

    包 java.uti.regex   类 Pattern 正则表达式的编译表示形式 Matcher 通过解释 Pattern 对 character …

    问说网手机版

    躺着 站着 跪着轻松访问

    更多详情 关于作者

    问说网

    问说网分享与设计有关的文章素材界面和作品,提供设计教程、素材分享、界面欣赏、编程设计、设计书籍、设计师导航等内容,你可以在这里阅读、学习、分享、交流。

    13177 文章
    493 评论
    2140 人气

    更多 热门话题

    APP界面

    关注 APP界面

    文章 41506 · 浏览 4032

    APP欣赏

    关注 APP欣赏

    文章 41427 · 浏览 4066

    APP手机界面

    关注 APP手机界面

    文章 41417 · 浏览 4109

    图片素材

    关注 图片素材

    文章 29463 · 浏览 3274

    高清图片

    关注 高清图片

    文章 26530 · 浏览 3534

    更多 推荐作者

    关注 秋末残雪

    文章 106 · 评论 0

    关注 走了留下什么

    文章 110 · 评论 0

    关注 莪很迷茫

    文章 97 · 评论 0

    关注 流浪的脚步

    文章 86 · 评论 0

    关注 凉城梦镜

    文章 79 · 评论 0

    关注 思念像咒语

    文章 102 · 评论 0

    
    顶部 反馈 评论 底部

    意见反馈

    感谢您对问说网的支持,提出您在使用过程中遇到的问题或宝贵建议,您的反馈对我们产品的完善有很大帮助。

    您的反馈我们已收到!

    感谢您提供的宝贵意见,我们会在1-2个工作日,通过您留下的联系方式将处理结果反馈给您!