这篇文章咱们主要作三件事es6
假设咱们手里有20张照片,这些照片能够在保持宽高比的状况下进行放大或者缩小。选定一个基准高度好比200pxweb
以上,就是木桶布局的原理。json
但现实场景远比仅实现基本效果的DEMO更复杂,以 500px 官网 和 百度图片 为例,主要考虑如下状况api
为了让产品功能更强大咱们还须要加入即时检索功能,用户输入关键字便可当即用木桶布局的方式展现搜索到底图片,当页面滚动到底部时会加载更多数据,当调整浏览器尺寸时会从新渲染,效果在这里。下图是效果图浏览器
你们一块儿来理一理思路,看如何实现:服务器
按照这个思路,咱们能够勉强写出效果,但确定会遇到不少恼人的细节,好比app
当这些细节处理完成以后,咱们会发现代码已经被改的面目全非,逻辑复杂,其余人(可能包括明天的本身)很难看懂。frontend
咱们能够换一种思路,使用一些方法让代码解耦,加强可读性和扩展性。最经常使用的方法就是使用「发布-订阅模式」,或者说叫「事件机制」。发布订阅模式的思路本质上是:对于每个模块,听到命令后,作好本身的事,作完后发个通知异步
第一,咱们先实现一个事件管理器函数
class Event { static on(type, handler) { return document.addEventListener(type, handler) } static trigger(type, data) { return document.dispatchEvent(new CustomEvent(type, { detail: data })) } } // useage Event.on('search', e => {console.log(e.detail)}) Event.trigger('search', 'study frontend in jirengu.com')
若是对 ES6不熟悉,能够先看看语法介绍参考这里,你们也可使用传统的模块模式来写参考这里。固然,咱们还能够不借助浏览器内置的CustomEvent,手动写一个发布订阅模式的事件管理器,参考这里 。
第二,咱们来实现交互模块
class Interaction { constructor() { this.searchInput = document.querySelector('#search-ipt') this.bind() } bind() { this.searchInput.oninput = this.throttle(() => { Event.trigger('search', this.searchInput.value) }, 300) document.body.onresize = this.throttle(() => Event.trigger('resize'), 300) document.body.onscroll = this.throttle(() => { if (this.isToBottom()) { Event.trigger('bottom') } },3000) } throttle(fn, delay) { let timer = null return () => { clearTimeout(timer) timer = setTimeout(() => fn.bind(this)(arguments), delay) } } isToBottom() { return document.body.scrollHeight - document.body.scrollTop - document.documentElement.clientHeight < 5 } } new Interaction()
以上代码逻辑很简单:
须要注意上述代码中 Class 的写法 和 箭头函数里 this 的用法,这里不作过多讲解。还须要注意代码中节流函数 throttle 的实现方式,以及页面是否滚动到底部的判断 isToBottom,咱们能够直接读代码来理解,而后本身动手写 demo 测试。
第三,咱们来实现数据加载模块
class Loader { constructor() { this.page = 1 this.per_page = 10 this.keyword = '' this.total_hits = 0 this.url = '//pixabay.com/api/' this.bind() } bind() { Event.on('search', e => { this.page = 1 this.keyword = e.detail this.loadData() .then(data => { console.log(this) this.total_hits = data.totalHits Event.trigger('load_first', data) }) .catch(err => console.log(err)) }) Event.on('bottom', e => { if(this.loading) return if(this.page * this.per_page > this.total_hits) { Event.trigger('load_over') return } this.loading = true ++this.page this.loadData() .then(data => Event.trigger('load_more', data)) .catch(err => console.log(err)) }) } loadData() { return fetch(this.fullUrl(this.url, { key: '5856858-0ecb4651f10bff79efd6c1044', q: this.keyword, image_type: 'photo', per_page: this.per_page, page: this.page })) .then((res) => { this.loading = false return res.json() }) } fullUrl(url, json) { let arr = [] for (let key in json) { arr.push(encodeURIComponent(key) + '=' + encodeURIComponent(json[key])) } return url + '?' + arr.join('&') } } new Loader()
由于加载首页数据与加载后续数据两者的流程是有差别的,全部对于 Loader 模块,咱们根据定义了3个事件。流程以下:
第4、咱们来实现布局模块
class Barrel { constructor() { this.mainNode = document.querySelector('main') this.rowHeightBase = 200 this.rowTotalWidth = 0 this.rowList = [] this.allImgInfo = [] this.bind() } bind() { Event.on('load_first', e => { this.mainNode.innerHTML = '' this.rowList = [] this.rowTotalWidth = 0 this.allImgInfo = [...e.detail.hits] this.render(e.detail.hits) }) Event.on('load_more', e => { this.allImgInfo.push(...e.detail.hits) this.render(e.detail.hits) }) Event.on('load_over', e => { this.layout(this.rowList, this.rowHeightBase) }) Event.on('resize', e => { this.mainNode.innerHTML = '' this.rowList = [] this.rowTotalWidth = 0 this.render(this.allImgInfo) }) } render(data) { if(!data) return let mainNodeWidth = parseFloat(getComputedStyle(this.mainNode).width) data.forEach(imgInfo => { imgInfo.ratio = imgInfo.webformatWidth / imgInfo.webformatHeight imgInfo.imgWidthAfter = imgInfo.ratio * this.rowHeightBase if (this.rowTotalWidth + imgInfo.imgWidthAfter <= mainNodeWidth) { this.rowList.push(imgInfo) this.rowTotalWidth += imgInfo.imgWidthAfter } else { let rowHeight = (mainNodeWidth / this.rowTotalWidth) * this.rowHeightBase this.layout(this.rowList, rowHeight) this.rowList = [imgInfo] this.rowTotalWidth = imgInfo.imgWidthAfter } }) } layout(row, rowHeight) { row.forEach(imgInfo => { var figureNode = document.createElement('figure') var imgNode = document.createElement('img') imgNode.src = imgInfo.webformatURL figureNode.appendChild(imgNode) figureNode.style.height = rowHeight + 'px' figureNode.style.width = rowHeight * imgInfo.ratio + 'px' this.mainNode.appendChild(figureNode) }) } } new Barrel()
对于布局模块来讲考虑流程很简单,就是从事件源拿数据本身去作布局,流程以下:
当监听到"resize"事件时,清空页面内容,使用暂存的数据从新布局
完整代码在这里
以上代码实现了逻辑解耦,每一个模块仅有单一职责原则,若是新增更能扩展性也很强。
若是你喜欢这篇文章或者以为有用,点个赞给个鼓励。