JavaScript+NodeJS+mongonDB实现瀑布流加载
来源:互联网 发布:php开源websocket 编辑:程序博客网 时间:2024/05/22 07:56
1.前端js文件
//获取滚动条到顶端的高度
function getScrollTop() {
var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
}
//获取滚动条的高度(固定值,不随滚动条改变)
function getScrollHeight() {
var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
}
//获取windows视窗高度
function getWindowHeight() {
var windowHeight = 0;
if (document.compatMode == 'CSS1Compat') {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
var windowHeight = 0;
if (document.compatMode == 'CSS1Compat') {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
}
//绑定windows事件
window.onscroll = function () {
if (getScrollTop() == getScrollHeight() - getWindowHeight()) {
//发送请求
postSomething();
}
}
}
2.AJAX提交数据
function postSomething() {
var xhr = new XMLHttpRequest();
if (xhr) {
// xhr.open([string]method,[string]url,[boolen optional]async,[string optional]user, [sting optional]password)
xhr.open('POST', '/article', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function () {
if ((xhr.readyState == 4) && (xhr.status == 200)) {
//return xhr.responseText、xhr.responseType、xhr.responseXML
var responseData = JSON.parse(xhr.responseText);
page = parseInt(JSON.stringify(responseData.page));
console.log(parseInt(JSON.stringify(responseData.page)));
for (i = 0; i < responseData.article.length; i++) {
// console.log(responseData.article[i].title)
addContent(responseData.article[i].id, responseData.article[i].title, responseData.article[i].createTime, responseData.article[i].username, responseData.article[i].content);
}
}
return true;
}
}
else {
return false;
}
xhr.send("page=" + page);
return true;
}
var xhr = new XMLHttpRequest();
if (xhr) {
// xhr.open([string]method,[string]url,[boolen optional]async,[string optional]user, [sting optional]password)
xhr.open('POST', '/article', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function () {
if ((xhr.readyState == 4) && (xhr.status == 200)) {
//return xhr.responseText、xhr.responseType、xhr.responseXML
var responseData = JSON.parse(xhr.responseText);
page = parseInt(JSON.stringify(responseData.page));
console.log(parseInt(JSON.stringify(responseData.page)));
for (i = 0; i < responseData.article.length; i++) {
// console.log(responseData.article[i].title)
addContent(responseData.article[i].id, responseData.article[i].title, responseData.article[i].createTime, responseData.article[i].username, responseData.article[i].content);
}
}
return true;
}
}
else {
return false;
}
xhr.send("page=" + page);
return true;
}
3.NodeJS后台
router.route('/')
.get(function (req, res, next) {
.get(function (req, res, next) {
var perPage = 5;
var page = 0;
article.find({})
.limit(perPage)
.skip(perPage * page)
.exec(function (err, results) {
article.count().exec(function (err, count) {
if (err)return console.error(err);
for (i = 0; i < results.length; i++) {
results[i].content = (JSON.stringify(results[i].content)).substr(0, 200);
}
res.render('article', {
article: results,
count: count,
page: page
});
})
})
})
.post(function (req, res, next) {
var perPage = 5;
var page = Math.max(0, req.body.page);
article.find({})
.limit(perPage)
.skip(perPage * page)
.exec(function (err, results) {
article.count().exec(function (err, count) {
.limit(perPage)
.skip(perPage * page)
.exec(function (err, results) {
article.count().exec(function (err, count) {
if (err)return console.error(err);
for (i = 0; i < results.length; i++) {
results[i].content = (JSON.stringify(results[i].content)).substr(0, 200);
}
res.render('article', {
article: results,
count: count,
page: page
});
})
})
})
.post(function (req, res, next) {
var perPage = 5;
var page = Math.max(0, req.body.page);
article.find({})
.limit(perPage)
.skip(perPage * page)
.exec(function (err, results) {
article.count().exec(function (err, count) {
if (err)return console.error(err);
//重新组装content.只选取前200个字符
for (i = 0; i < results.length; i++) {
results[i].content = (JSON.stringify(results[i].content)).substr(0, 200);
}
res.jsonp({
article: results,
count: count,
page: page + 1
});
})
})
});
results[i].content = (JSON.stringify(results[i].content)).substr(0, 200);
}
res.jsonp({
article: results,
count: count,
page: page + 1
});
})
})
});
0 0
- JavaScript+NodeJS+mongonDB实现瀑布流加载
- JavaScript、jQuery、css3实现瀑布流加载
- Javascript实现瀑布流
- JavaScript动态加载瀑布流
- javascript实现瀑布流效果及图片加载样式
- JavaScript实现瀑布流效果
- 原生javascript 实现瀑布流
- javascript实现瀑布流效果
- JavaScript实现瀑布流效果
- 原生JavaScript实现瀑布流
- JavaScript实现瀑布流布局以及页面的自动加载
- js实现瀑布流排序加载效果
- Ajax实现瀑布流图片加载
- RecyclerView瀑布流加载图片实现
- JS实现无限加载瀑布流
- 缓存+懒加载实现瀑布流效果
- javascript实现自适应宽度的瀑布流
- 瀑布流三部曲之javascript实现
- ZMQ指南
- Poj2960 S-Nim
- PHP环境搭建
- 归并排序
- thinkphp入门学习笔记(一)
- JavaScript+NodeJS+mongonDB实现瀑布流加载
- Kafka设计解析(二)- Kafka High Availability (上)
- mac osx 10.11.6 安装eclipse
- 1042. Shuffling Machine (20)-PAT甲级真题
- ZeroMQ进阶
- 找不到更好的?为什么不试一试呢?
- 自定义组合控件:textView+checkBox
- linux账户管理
- C++ :类中的静态私有数据成员