js實現(xiàn)動態(tài)加載數(shù)據(jù)瀑布流
本文實例為大家分享了js實現(xiàn)動態(tài)加載數(shù)據(jù)瀑布流的具體代碼,供大家參考,具體內(nèi)容如下
實現(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() {
? ? ? // 這里默認一次獲取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ù)存在才進行下面的操作
? ? ? if (partList) {
? ? ? ? partList.forEach((li, index) => {
? ? ? ? ? const o_item = document.createElement('div')
? ? ? ? ? // 這里要給o_item設置高度
? ? ? ? ? // 不要想著用img撐開,這樣做會導致不能夠獲取到o_item的offsetWidth
? ? ? ? ? // 注意dom添加一個節(jié)點后,你是不能馬上獲取到其一些寬高的,
? ? ? ? ? // 所以后端在返回數(shù)據(jù)的時候要給出高度
? ? ? ? ? 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
? ? ? ? ? // 注意這里好像不能直接設置透明度,最好加個定時器觸發(fā)重繪
? ? ? ? ? // img.style.opacity = '1'
? ? ? ? ? o_item.appendChild(img)
? ? ? ? ? this.el.appendChild(o_item)
? ? ? ? ? // 設置第一行?
? ? ? ? ? // 必須是第一頁的數(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
? ? }
? }
? // 找最小下標
? 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.這里要注意幾點 就是 后端給的數(shù)據(jù) 除了img的地址 還要給出每個img的寬高
2.在設置過渡 也就是我這里面的opacity:1的時候 要么在這之前觸發(fā)回流操作 比如我這里有獲取offsetHeight ,要么用一個定時器包括,否則這個過渡是不會生效的,圖片會直接顯示
3.后端每次返回的數(shù)據(jù)最好夠多,否則會導致可能第一頁數(shù)據(jù)不夠多,導致沒有出現(xiàn)滾動條 觸發(fā)不了事件,當然最好是能自己寫邏輯判斷,后面有時間會完善這個代碼,比如請求的選項也寫到op里面,讓用戶手動傳入
主要是大家理解這思路就好。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript中yield實用簡潔實現(xiàn)方式
原以為是一個蠻復雜的題目,想了許久沒思路,當然要實現(xiàn)絕對能實現(xiàn),但如果分析JavaScript腳本或是動態(tài)產(chǎn)生代碼,都太復雜了。2010-06-06
為什么JS中eval處理JSON數(shù)據(jù)要加括號
這篇文章主要介紹了為什么JS中eval處理JSON數(shù)據(jù)要加括號的相關資料,需要的朋友可以參考下2015-04-04
基于BootStrap multiselect.js實現(xiàn)的下拉框聯(lián)動效果
當option特別多時,一般的下拉框選擇起來就有點力不從心了,所以使用multiselect是個很好的選擇。在網(wǎng)上找了半天找到了解決方案,具體實現(xiàn)代碼大家參考下本文吧2017-07-07
JS 非圖片動態(tài)loading效果實現(xiàn)代碼
功能說明:譬如在按某個button時,顯示消息"Loading”,然后每隔一秒后后面加上".",至一定數(shù)量的"."時如:"Loading...",再重置此消息為"Loading",繼續(xù)動態(tài)顯示,直至按鈕事件處理完成。2010-04-04

