您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用JavaScript怎么編寫(xiě)一個(gè)滾動(dòng)加載更多功能,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
vscode
index:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * {margin:0; padding: 0;} li {list-style: none;} body {background: #eee;} .wrapper {background: #fff;width: 970px; margin:20px auto; padding: 15px;} h2 {text-align: center; border-bottom: 1px solid #ddd; padding-bottom: 20px;} li {margin:20px 0; border-bottom: 1px dotted #eee; padding-bottom: 20px;} p { line-height: 25px;} </style> <script src="/jquery-1.11.3.js"></script> </head> <body> <div class="wrapper"> <h2>新聞列表(加載更多)<script>document.write(new Date().toLocaleString())</script></h2> <ul> </ul> <div class="footer" > <img src="" alt=""> </div> </div> <script> let page = 1; // 默認(rèn)是第1頁(yè) let load = true; function getNewsList(page) { $(".footer img").attr("src","/timg.gif") load = false; $.get("/news",{page},res=>{ // console.log(res) if(res.news.length){ let str =""; // 客戶端渲染,客戶端自己通過(guò)ajax獲取數(shù)據(jù),自已渲染數(shù)據(jù) res.news.forEach(item=>{ str += ` <li> <h3><a href="#" rel="external nofollow" >${item.title}</a></h3> <p class="time">${item.time}</p> <p class="summary">${item.summary}</p> </li> ` }) $("ul").append(str) load = true; }else{ $(".footer").html("--------------- 我是有底線的 --------------- ") load = false; } }) } getNewsList(page); // 一上來(lái)就獲取第1頁(yè)的數(shù)據(jù) // 判斷出什么時(shí)候到底部 $(document).scroll(function () { let st = $(window).scrollTop(); // 卷上去的高度 let ch = $(window).height(); // 一屏的高度 let dh = $(document).height(); // 整個(gè)文檔(整個(gè)內(nèi)容)的高度 if((st+ch) >= dh && load){ // 滾動(dòng)到了底部 getNewsList(++page) } }) </script> </body> </html>
JS:
let express = require("express"); let path = require("path"); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; let app = express(); // 托管靜態(tài)資源 app.use(express.static(path.resolve(__dirname, "./public"))) // 當(dāng)訪問(wèn)/ 響應(yīng)一個(gè)html頁(yè)面,不是渲染模板 app.get("/",(req,res)=>{ res.sendFile(path.resolve(__dirname,"./views/index.html")) }) // 當(dāng)訪問(wèn)/news 響應(yīng)一個(gè)json數(shù)據(jù) // 如果一個(gè)路由,返回了一個(gè)Json數(shù)據(jù),我們叫http://localhost:3000/news是api接口 // api:1)別人封裝的方法,我們直接去調(diào)用 2)指一個(gè)url地址 叫api接口 app.get("/news",(req,res)=>{ let page = req.query.page || 1; // page表示當(dāng)前是第幾頁(yè) // page如果小于等于0 (page <= 0) && (page = 1) // console.log(page) let pageSize = 5; // 每頁(yè)顯示多少條 let offset = (page-1)*pageSize; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("news"); dbo.collection("newslist").find({}).skip(offset).limit(pageSize).toArray(function(err, result) { if (err) throw err; // console.log(result); // 獲取每一頁(yè)的數(shù)據(jù) dbo.collection("newslist").count().then(result2=>{ // result2表示一共有多少條數(shù)據(jù) let total = result2; let size = Math.ceil(total / pageSize) // 服務(wù)端渲染: 核心是使用模板引擎 jsp php asp out // 是把模板和數(shù)據(jù)柔和在一起,形成一個(gè)有真實(shí)數(shù)據(jù)的頁(yè)面,響應(yīng)給客戶端 res.json({ news:result, total, // 總共多少條數(shù)據(jù) pageSize, // 每頁(yè)顯示多少條 page, // 當(dāng)前是第幾頁(yè) size, // 一菜有多少頁(yè) }) }); db.close(); }); }); }) app.listen(3000,()=>{ console.log("3000 ok~") })
上述就是小編為大家分享的使用JavaScript怎么編寫(xiě)一個(gè)滾動(dòng)加載更多功能了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。