js實(shí)現(xiàn)動態(tài)加載數(shù)據(jù)瀑布流
本文實(shí)例為大家分享了js實(shí)現(xiàn)動態(tài)加載數(shù)據(jù)瀑布流的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的功能
1.每次下拉到底部會自動加載下一頁的數(shù)據(jù)
2.圖片逐漸顯示
首先html
<!DOCTYPE html> <html lang="zh-CN"> ?? ?<head> ?? ??? ?<meta charset="UTF-8" /> ?? ??? ?<meta name="viewport" content="width=device-width, initial-scale=1.0" /> ?? ??? ?<title>Document</title> ?? ??? ?<style> ?? ??? ??? ?* { ?? ??? ??? ??? ?margin: 0; ?? ??? ??? ??? ?padding: 0; ?? ??? ??? ?} ?? ??? ??? ?#wapper { ?? ??? ??? ??? ?width: 1200px; ?? ??? ??? ??? ?margin: 0 auto; ?? ??? ??? ??? ?position: relative; ?? ??? ??? ?} ?? ??? ??? ?.wr_item { ?? ??? ??? ??? ?position: absolute; ?? ??? ??? ??? ?overflow: hidden; ?? ??? ??? ?} ?? ??? ??? ?img { ?? ??? ??? ??? ?display: block; ?? ??? ??? ??? ?width: 100%; ?? ??? ??? ??? ?opacity: 0; ?? ??? ??? ??? ?transition: opacity 3s; ?? ??? ??? ?} ?? ??? ?</style> ?? ?</head> ?? ?<body> ?? ??? ?<div id="wapper"></div> ?? ??? ?<script src="./scroll.js"></script> ?? ??? ?<script src="./data.js"></script> ?? ??? ?<script src="./warpper.js"></script> ?? ??? ?<script type="text/javascript"> ?? ??? ??? ?new Wapper({ ?? ??? ??? ??? ?el: "wapper", ?? ??? ??? ??? ?el_itemClassName: "wr_item", ?? ??? ??? ??? ?colum: 8, ?? ??? ??? ??? ?gap: 10, ?? ??? ??? ?}).init(); ?? ??? ?</script> ?? ?</body> </html>
接著是主要的js
; (function (doc) { ? // console.log('list', list); ? var Wapper = function (op) { ? ? this.el = doc.getElementById(op.el) ? ? this.el_itemClassName = op.el_itemClassName ? ? this.colum = op.colum ? ? this.gap = op.gap ? ? // 1.首先獲取到每個照片外層盒子 也就是wr_item 的寬度 ? ? this.wr_item_w = (this.el.offsetWidth - (this.colum - 1) * this.gap) / this.colum ? ? this.pageNum = 0 ? ? this.hightArr = [] ? ? this.pageSize = 4 ? } ? Wapper.prototype = { ? ? init() { ? ? ? this.bindEvetn() ? ? ? this.getData() ? ? }, ? ? getData() { ? ? ? // 這里默認(rèn)一次獲取30個照片 ,我這里了是假數(shù)據(jù)所以就不做別的了 ? ? ? // 一般這里是向后端請求數(shù)據(jù) ? ? ? // list一共是有120 ? ? ? const partList = getPartList(this.pageNum) ? ? ? if (partList) { ? ? ? ? this.render(partList) ? ? ? ? return true ? ? ? } else { ? ? ? ? return false ? ? ? } ? ? }, ? ? render(partList) { ? ? ? // 只有數(shù)據(jù)存在才進(jìn)行下面的操作 ? ? ? if (partList) { ? ? ? ? partList.forEach((li, index) => { ? ? ? ? ? const o_item = document.createElement('div') ? ? ? ? ? // 這里要給o_item設(shè)置高度 ? ? ? ? ? // 不要想著用img撐開,這樣做會導(dǎo)致不能夠獲取到o_item的offsetWidth ? ? ? ? ? // 注意dom添加一個節(jié)點(diǎn)后,你是不能馬上獲取到其一些寬高的, ? ? ? ? ? // 所以后端在返回?cái)?shù)據(jù)的時(shí)候要給出高度 ? ? ? ? ? const imgW = li.width ? ? ? ? ? const imgH = li.height ? ? ? ? ? o_item.style.width = this.wr_item_w + 'px' ? ? ? ? ? // 高度等于 盒子寬度x圖片高度/圖片寬度 ? ? ? ? ? const oitemH = (this.wr_item_w * imgH) / imgW ? ? ? ? ? o_item.style.height = oitemH + 'px' ? ? ? ? ? o_item.className = this.el_itemClassName ? ? ? ? ? const img = new Image() ? ? ? ? ? img.src = li.thumbURL ? ? ? ? ? // 注意這里好像不能直接設(shè)置透明度,最好加個定時(shí)器觸發(fā)重繪 ? ? ? ? ? // img.style.opacity = '1' ? ? ? ? ? o_item.appendChild(img) ? ? ? ? ? this.el.appendChild(o_item) ? ? ? ? ? // 設(shè)置第一行? ? ? ? ? ? // 必須是第一頁的數(shù)據(jù) ? ? ? ? ? if (index < this.colum && this.pageNum === 0) { ? ? ? ? ? ? this.hightArr.push(o_item.offsetHeight) ? ? ? ? ? ? o_item.style.top = '0' ? ? ? ? ? ? if (index + 1 % this.colum === 0) { ? ? ? ? ? ? ? // 說明這是第一個 ? ? ? ? ? ? ? o_item.style.left = '0' ? ? ? ? ? ? } else { ? ? ? ? ? ? ? o_item.style.left = index * (this.wr_item_w + this.gap) + 'px' ? ? ? ? ? ? } ? ? ? ? ? } else { ? ? ? ? ? ? const items = this.el.getElementsByClassName(this.el_itemClassName) ? ? ? ? ? ? const minIndex = getMinIdx(this.hightArr) ? ? ? ? ? ? const c_item = items[minIndex] ? ? ? ? ? ? o_item.style.left = c_item.offsetLeft + 'px' ? ? ? ? ? ? o_item.style.top = this.hightArr[minIndex] + this.gap + 'px' ? ? ? ? ? ? this.hightArr[minIndex] += (o_item.offsetHeight + this.gap) ? ? ? ? ? } ? ? ? ? ? img.style.opacity = '1' ? ? ? ? }) ? ? ? ? console.log('this.hightArr', this.hightArr); ? ? ? ? this.el.style.height = this.hightArr[getMaxIdx(this.hightArr)] + 'px' ? ? ? } ? ? }, ? ? bindEvetn() { ? ? ? var that = this ? ? ? window.onscroll = function () { ? ? ? ? ? if (getWindowHeight() + getScrollTop() === getHtmlScrollHeight()) { ? ? ? ? ? console.log(that.pageNum); ? ? ? ? ? that.pageNum++; ? ? ? ? ? let hasNext = that.getData() ? ? ? ? ? hasNext || that.pageNum-- ? ? ? ? } ? ? ? } ? ? } ? } ? function getPartList(pageNum) { ? ? switch (pageNum) { ? ? ? case 0: ? ? ? ? return list.slice(0, 30) ? ? ? ? break; ? ? ? case 1: ? ? ? ? return list.slice(30, 60) ? ? ? case 2: ? ? ? ? return list.slice(60, 90) ? ? ? case 3: ? ? ? ? return list.slice(90, 120) ? ? ? default: ? ? ? ? return null ? ? } ? } ? // 找最小下標(biāo) ? function getMinIdx(arr) { ? ? const minHeight = Math.min.apply(null, arr) ? ? return [].indexOf.call(arr, minHeight) ? } ? // 找最大 ? function getMaxIdx(arr) { ? ? const maxHeight = Math.max.apply(null, arr) ? ? return [].indexOf.call(arr, maxHeight) ? } ? window.Wapper = Wapper })(document)
getWindowHeight() + getScrollTop()
是來檢測瀏覽是不是滾動到底部的。
1.這里要注意幾點(diǎn) 就是 后端給的數(shù)據(jù) 除了img的地址 還要給出每個img的寬高
2.在設(shè)置過渡 也就是我這里面的opacity:1的時(shí)候 要么在這之前觸發(fā)回流操作 比如我這里有獲取offsetHeight ,要么用一個定時(shí)器包括,否則這個過渡是不會生效的,圖片會直接顯示
3.后端每次返回的數(shù)據(jù)最好夠多,否則會導(dǎo)致可能第一頁數(shù)據(jù)不夠多,導(dǎo)致沒有出現(xiàn)滾動條 觸發(fā)不了事件,當(dāng)然最好是能自己寫邏輯判斷,后面有時(shí)間會完善這個代碼,比如請求的選項(xiàng)也寫到op里面,讓用戶手動傳入
主要是大家理解這思路就好。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js圖片加載效果實(shí)例代碼(延遲加載+瀑布流加載)
- 利用JS實(shí)現(xiàn)簡單的瀑布流加載圖片效果
- JavaScript實(shí)現(xiàn)瀑布流以及加載效果
- javascript實(shí)現(xiàn)瀑布流動態(tài)加載圖片原理
- javascript實(shí)現(xiàn)仿百度圖片的瀑布流加載效果
- JavaScript實(shí)現(xiàn)圖片自動加載的瀑布流效果
- 解析javascript瀑布流原理實(shí)現(xiàn)圖片滾動加載
- javascript瀑布流式圖片懶加載實(shí)例解析與優(yōu)化
- javascript瀑布流式圖片懶加載實(shí)例
- javascript實(shí)現(xiàn)瀑布流加載圖片原理
相關(guān)文章
Jsonp 關(guān)鍵字詳解及json和jsonp的區(qū)別,ajax和jsonp的區(qū)別
這篇文章主要介紹了Jsonp 關(guān)鍵字詳解及json和jsonp的區(qū)別,ajax和jsonp的區(qū)別 的相關(guān)資料,需要的朋友可以參考下2015-12-12微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08通過action傳過來的值在option獲取進(jìn)行驗(yàn)證的方法
通過action傳過來的值在option獲取進(jìn)行驗(yàn)證,下面有個不錯的示例,需要的朋友不要錯過2013-11-11JS 根據(jù)子網(wǎng)掩碼,網(wǎng)關(guān)計(jì)算出所有IP地址范圍示例
這篇文章主要介紹了JS 根據(jù)子網(wǎng)掩碼,網(wǎng)關(guān)計(jì)算出所有IP地址范圍,涉及IP地址、子網(wǎng)的正則驗(yàn)證,子網(wǎng)掩碼計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2016-09-09