JavaScript實現(xiàn)滾動加載更多
更新時間:2020年12月27日 11:25:34 作者:予傾
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)滾動加載更多,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現(xiàn)滾動加載更多的具體代碼,供大家參考,具體內容如下
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;} h1 {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"> <h1>新聞列表(加載更多)<script>document.write(new Date().toLocaleString())</script></h1> <ul> </ul> <div class="footer" style="text-align: center"> <img style="width: 40px" src="" alt=""> </div> </div> <script> let page = 1; // 默認是第1頁 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 =""; // 客戶端渲染,客戶端自己通過ajax獲取數(shù)據(jù),自已渲染數(shù)據(jù) res.news.forEach(item=>{ str += ` <li> <h2><a href="#" rel="external nofollow" >${item.title}</a></h2> <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); // 一上來就獲取第1頁的數(shù)據(jù) // 判斷出什么時候到底部 $(document).scroll(function () { let st = $(window).scrollTop(); // 卷上去的高度 let ch = $(window).height(); // 一屏的高度 let dh = $(document).height(); // 整個文檔(整個內容)的高度 if((st+ch) >= dh && load){ // 滾動到了底部 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"))) // 當訪問/ 響應一個html頁面,不是渲染模板 app.get("/",(req,res)=>{ res.sendFile(path.resolve(__dirname,"./views/index.html")) }) // 當訪問/news 響應一個json數(shù)據(jù) // 如果一個路由,返回了一個Json數(shù)據(jù),我們叫http://localhost:3000/news是api接口 // api:1)別人封裝的方法,我們直接去調用 2)指一個url地址 叫api接口 app.get("/news",(req,res)=>{ let page = req.query.page || 1; // page表示當前是第幾頁 // page如果小于等于0 (page <= 0) && (page = 1) // console.log(page) let pageSize = 5; // 每頁顯示多少條 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); // 獲取每一頁的數(shù)據(jù) dbo.collection("newslist").count().then(result2=>{ // result2表示一共有多少條數(shù)據(jù) let total = result2; let size = Math.ceil(total / pageSize) // 服務端渲染: 核心是使用模板引擎 jsp php asp out // 是把模板和數(shù)據(jù)柔和在一起,形成一個有真實數(shù)據(jù)的頁面,響應給客戶端 res.json({ news:result, total, // 總共多少條數(shù)據(jù) pageSize, // 每頁顯示多少條 page, // 當前是第幾頁 size, // 一菜有多少頁 }) }); db.close(); }); }); }) app.listen(3000,()=>{ console.log("3000 ok~") })
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 當滾動條滾動到頁面底部自動加載增加內容的js代碼
- js實現(xiàn)滾動條滾動到頁面底部繼續(xù)加載
- 基于AngularJS實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- js/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法
- 使用jQuery或者原生js實現(xiàn)鼠標滾動加載頁面新數(shù)據(jù)
- JavaScript實現(xiàn)頁面滾動圖片加載(仿lazyload效果)
- 原生Js頁面滾動延遲加載圖片實現(xiàn)原理及過程
- 滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
- Angularjs 滾動加載更多數(shù)據(jù)
- AngularJS基于ngInfiniteScroll實現(xiàn)下拉滾動加載的方法
相關文章
JavaScript中的this陷阱的最全收集并整理(沒有之一)
這篇文章主要介紹了JavaScript中的this陷阱的最全收集,需要的朋友可以參考下2017-02-02Js中parentNode,parentElement,childNodes,children之間的區(qū)別
這篇文章主要是對Js中parentNode,parentElement,childNodes,children的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11