JavaScript實(shí)現(xiàn)滾動(dòng)加載更多
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)滾動(dòng)加載更多的具體代碼,供大家參考,具體內(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頁(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 ="";
// 客戶端渲染,客戶端自己通過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頁(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)訪問/ 響應(yīng)一個(gè)html頁(yè)面,不是渲染模板
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)前是第幾頁(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~")
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 當(dāng)滾動(dòng)條滾動(dòng)到頁(yè)面底部自動(dòng)加載增加內(nèi)容的js代碼
- js實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到頁(yè)面底部繼續(xù)加載
- 基于AngularJS實(shí)現(xiàn)頁(yè)面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
- js/jquery控制頁(yè)面動(dòng)態(tài)加載數(shù)據(jù) 滑動(dòng)滾動(dòng)條自動(dòng)加載事件的方法
- 使用jQuery或者原生js實(shí)現(xiàn)鼠標(biāo)滾動(dòng)加載頁(yè)面新數(shù)據(jù)
- JavaScript實(shí)現(xiàn)頁(yè)面滾動(dòng)圖片加載(仿lazyload效果)
- 原生Js頁(yè)面滾動(dòng)延遲加載圖片實(shí)現(xiàn)原理及過程
- 滑輪滾動(dòng)到頁(yè)面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
- Angularjs 滾動(dòng)加載更多數(shù)據(jù)
- AngularJS基于ngInfiniteScroll實(shí)現(xiàn)下拉滾動(dòng)加載的方法
相關(guān)文章
dess中一個(gè)簡(jiǎn)單的多路委托的實(shí)現(xiàn)
這個(gè)SDelegate用起來可能會(huì)比較詭異,比如很多操作都要重新賦值。Dess中,SDelegate主要用于一些特定場(chǎng)合,如DOM事件派發(fā)。2010-07-07
基于JS實(shí)現(xiàn)前端壓縮上傳圖片的實(shí)例代碼
這篇文章主要介紹了基于JS實(shí)現(xiàn)前端壓縮上傳圖片的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
JavaScript中的this陷阱的最全收集并整理(沒有之一)
這篇文章主要介紹了JavaScript中的this陷阱的最全收集,需要的朋友可以參考下2017-02-02
JS獲取當(dāng)前時(shí)間的年月日時(shí)分秒及時(shí)間的格式化的方法
這篇文章主要介紹了js獲取當(dāng)前時(shí)間的年月日時(shí)分秒及時(shí)間的格式化,本文通過實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Js中parentNode,parentElement,childNodes,children之間的區(qū)別
這篇文章主要是對(duì)Js中parentNode,parentElement,childNodes,children的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11
javascript比較語義化版本號(hào)的實(shí)現(xiàn)代碼
這篇文章先是給大家簡(jiǎn)單的介紹了下語義化版本號(hào),而后再用實(shí)例代碼演示語義化版本號(hào)的比較方法,有需要的朋友們可以參考借鑒。2016-09-09
常用的JavaScript驗(yàn)證正則表達(dá)式匯總
這篇文章主要是對(duì)常用的JavaScript驗(yàn)證正則表達(dá)式進(jìn)行了詳細(xì)的匯總介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11
js隱式轉(zhuǎn)換的知識(shí)實(shí)例講解
在本篇文章中,小編給大家分享了關(guān)于js隱式轉(zhuǎn)換的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。2018-09-09

