JavaScript實現(xiàn)滾動加載更多
更新時間:2020年12月27日 11:25:34 作者:予傾
這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)滾動加載更多,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現(xiàn)滾動加載更多的具體代碼,供大家參考,具體內(nèi)容如下
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; // 默認(rèn)是第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(); // 整個文檔(整個內(nèi)容)的高度
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")))
// 當(dāng)訪問/ 響應(yīng)一個html頁面,不是渲染模板
app.get("/",(req,res)=>{
res.sendFile(path.resolve(__dirname,"./views/index.html"))
})
// 當(dāng)訪問/news 響應(yīng)一個json數(shù)據(jù)
// 如果一個路由,返回了一個Json數(shù)據(jù),我們叫http://localhost:3000/news是api接口
// api:1)別人封裝的方法,我們直接去調(diào)用 2)指一個url地址 叫api接口
app.get("/news",(req,res)=>{
let page = req.query.page || 1; // page表示當(dāng)前是第幾頁
// 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)
// 服務(wù)端渲染: 核心是使用模板引擎 jsp php asp out
// 是把模板和數(shù)據(jù)柔和在一起,形成一個有真實數(shù)據(jù)的頁面,響應(yīng)給客戶端
res.json({
news:result,
total, // 總共多少條數(shù)據(jù)
pageSize, // 每頁顯示多少條
page, // 當(dāng)前是第幾頁
size, // 一菜有多少頁
})
});
db.close();
});
});
})
app.listen(3000,()=>{
console.log("3000 ok~")
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 當(dāng)滾動條滾動到頁面底部自動加載增加內(nèi)容的js代碼
- js實現(xiàn)滾動條滾動到頁面底部繼續(xù)加載
- 基于AngularJS實現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- js/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法
- 使用jQuery或者原生js實現(xiàn)鼠標(biāo)滾動加載頁面新數(shù)據(jù)
- JavaScript實現(xiàn)頁面滾動圖片加載(仿lazyload效果)
- 原生Js頁面滾動延遲加載圖片實現(xiàn)原理及過程
- 滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
- Angularjs 滾動加載更多數(shù)據(jù)
- AngularJS基于ngInfiniteScroll實現(xiàn)下拉滾動加載的方法
相關(guān)文章
JavaScript中的this陷阱的最全收集并整理(沒有之一)
這篇文章主要介紹了JavaScript中的this陷阱的最全收集,需要的朋友可以參考下2017-02-02
JS獲取當(dāng)前時間的年月日時分秒及時間的格式化的方法
這篇文章主要介紹了js獲取當(dāng)前時間的年月日時分秒及時間的格式化,本文通過實例代碼講解的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
Js中parentNode,parentElement,childNodes,children之間的區(qū)別
這篇文章主要是對Js中parentNode,parentElement,childNodes,children的區(qū)別進行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11

