JavaScript實(shí)現(xiàn)滾動加載更多
更新時(shí)間:2020年12月27日 11:25:34 作者:予傾
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滾動加載更多,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JavaScript實(shí)現(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ù)
// 判斷出什么時(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){
// 滾動到了底部
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)一個(gè)html頁面,不是渲染模板
app.get("/",(req,res)=>{
res.sendFile(path.resolve(__dirname,"./views/index.html"))
})
// 當(dāng)訪問/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)前是第幾頁
// 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ù)柔和在一起,形成一個(gè)有真實(shí)數(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實(shí)現(xiàn)滾動條滾動到頁面底部繼續(xù)加載
- 基于AngularJS實(shí)現(xiàn)頁面滾動到底自動加載數(shù)據(jù)的功能
- js/jquery控制頁面動態(tài)加載數(shù)據(jù) 滑動滾動條自動加載事件的方法
- 使用jQuery或者原生js實(shí)現(xiàn)鼠標(biāo)滾動加載頁面新數(shù)據(jù)
- JavaScript實(shí)現(xiàn)頁面滾動圖片加載(仿lazyload效果)
- 原生Js頁面滾動延遲加載圖片實(shí)現(xiàn)原理及過程
- 滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
- Angularjs 滾動加載更多數(shù)據(jù)
- AngularJS基于ngInfiniteScroll實(shí)現(xiàn)下拉滾動加載的方法
相關(guān)文章
基于layui數(shù)據(jù)表格以及傳數(shù)據(jù)的方式
今天小編就為大家分享一篇基于layui數(shù)據(jù)表格以及傳數(shù)據(jù)的方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
js定時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了js定時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JavaScript實(shí)現(xiàn)的一個(gè)日期格式化函數(shù)分享
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的一個(gè)日期格式化函數(shù)分享,本文給出了實(shí)現(xiàn)代碼和使用例子,需要的朋友可以參考下2014-12-12
js實(shí)現(xiàn)在頁面上彈出蒙板技巧簡單實(shí)用
蒙板是兩個(gè)div,其中popWindow樣式的div用于遮住整個(gè)頁面并半透明,感興趣的朋友可以參考下哈,希望對你學(xué)習(xí)js蒙版有所幫助2013-04-04
JS監(jiān)聽dom高度變化幾種常用方法總結(jié)
我們在開發(fā)中會遇到一些需求,需要監(jiān)聽元素變化,比如元素屬性變化,元素大小變化,這篇文章主要給大家介紹了關(guān)于JS監(jiān)聽dom高度變化幾種常用方法的相關(guān)資料,需要的朋友可以參考下2023-10-10
JavaScript和TypeScript中的void的具體使用
這篇文章主要介紹了JavaScript和TypeScript中的void的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

