$(function () { // 图片懒加载 $("img").lazyload(); // 显示区高度 let ch = document.documentElement.clientHeight || document.body.clientHeight, lock = false, // 加载 数据 currentPage = 3, totalPage = 3, // 判断请求参数 url = window.location.href, path = lxd.parseUrl(url).pathname , reg = /(?<=zt).*?(?=.html)/, special_category = !path.match(reg) ? '' : path.match(reg)[0]; $(window).scroll(function () { if (lock) return false; // 滑动区高度 let sh = document.documentElement.scrollHeight || document.body.scrollHeight, // 滑动位置 st = document.documentElement.scrollTop || document.body.scrollTop, c = sh * 1 - ch * 1 - st * 1; if (c == 0 && currentPage <= totalPage) { lock = true; $(".my-loading").show(); data({ currentPage, special_category }).then(res => { totalPage = res.totalPage; currentPage++; lock = res.lock; $(".my-loading").hide(); }) } if (c == 0 && currentPage > totalPage) { $(".my-loading").html("已经没有可以加载的数据了").show(); } }) // 加载数据 function data (data) { return new Promise(resolve => { $.ajax({ url: "http://api.gaokaoapp.net/news/h/", data: { ...data, pageSize: 10 }, type: "get", error: function (error) { $(".my-loading").html("已经没有可以加载的数据了") }, success: function (res) { let { dataList, totalPage } = res.data; let father = document.createDocumentFragment(); Array.from(dataList).forEach( item => { let str = `
${item.title}
${item.source}${item.delay_time}
` $(father).append(str); } ) $(".list").append(father); $("img").lazyload(); resolve({ lock: false, totalPage }) } }) }) } })