/**
* 仅做到底自动刷新功能
* options: {
* page int 当前在第几页
* limit int 每页个数
* onBottom function 到底的回调
* whenEnd function 结束时的回调
* dontWait bool 不等待第一个回调执行完毕就可以执行第二个
* autoFull bool 是否在上一个回调执行完后自动定时再检测一次 这个与dontWait慎用
* delay int 上一个参数的时间(毫秒)
* toTop int 到元素下边框(不含)之前几像素就开始加载
* }
*/
class idkScroll {
constructor(node, { page = 0, limit = 10, onBottom = function () { }, whenEnd = function () { }, dontWait = false, autoFull = true, delay = 100, toTop = 0 } = {}) {
if (node.constructor === String) {
node = $(node).get(0);
}
this.node = node; this.length = page * limit; this.total = null; this.limit = limit; this.onBottom = onBottom; this.whenEnd = whenEnd; this.dontWait = dontWait; this.autoFull = autoFull; this.delay = delay; this.toTop = toTop; // 是否正在等待回复 当this.dontWait为true时忽略
this.waitForReturn = false; let that = this;
this.proxyFunc = function () {
that.checkBottom.apply(that);
}; // 开始检测
this.scroll("on");
}
// 开启或关闭滚动检测(是否停止检测)
scroll(str) {
if (str == "on") {
// 开始检测
// 默认执行一遍
this.checkBottom(); // 开启滚动检测
$(document).scroll(this.proxyFunc);
}
if (str == "off") {
// 停止检测
$(document).off("scroll", this.proxyFunc);
}
}
// 检查是否到底
checkBottom() {
// 合理运用表达式排序和短路来减少时间复杂度(省不了多少)
// 只算实际高度+内边距
if (
// 没有加载完成为true 利用编程语言对或的短路来规避this.total没有值的情况
(this.total == null || this.length < this.total) &&
// 没有正在等待回应或开启了不等待模式为true
(!this.waitForReturn || this.dontWait) &&
// 窗口下边框之后this.toTop像素的高度大于等于元素下边框(不含)的高度为true
$(document).scrollTop() + $(window).height() + this.toTop >= $(this.node).offset().top + $(this.node).innerHeight()
) {
this.waitForReturn = true;
this.onBottom(Math.ceil(this.length / this.limit) + 1, this.limit, this.node, this);
}
}
// 回调的回调 代表加载完成,可以继续检测
end(length, total) {
if (!this.dontWait && !this.waitForReturn) {
return;
} this.total = total; this.length += length; this.waitForReturn = false; if (this.isEnd()) {
// 停止检测
this.scroll("off"); // 运行结束函数
this.whenEnd(this.node, this);
} else {
if (this.autoFull) {
// 小心处理异步this,setTimeout里的this将会被替换为globalThis
let that = this;
setTimeout(function () {
that.checkBottom.apply(that);
}, this.delay);
}
}
}
// 检查结束
isEnd() {
return this.length >= this.total;
}
} // 实例
// 传入两个参数
// 一个是css选择器或者网页元素
// 一个是options选项
let idkscroll = new idkScroll("body", {
// 到元素的底部运行
onBottom: function () {console.log("bottom", arguments); idkscroll.end(1, 1)},
// 结束时运行
whenEnd: function () {console.log("end", arguments);},
// 每页两条数据
limit: 2,
// 窗口下边框之后toTop像素的高度大于等于元素下边框(不含)的高度就触发onBottom的函数
toTop: $(window).height()
});
console.log(idkscroll);

发这篇博客大部分是表达我还没死

哦吼吼,能看到这个链接就说明我的文章被爬虫爬了

请尊重原作者: https://www.cnblogs.com/dffxd/

最新文章

  1. JVM(java 虚拟机)内存设置
  2. 《机器学习实战》学习笔记——第2章 KNN
  3. navicat连接oracle报错ORA-12737: Instant Client Light: unsupported server character set CHS16GBK”
  4. gitlab web登入密码忘记以后可以用如下方式修改密码
  5. 从扩展方法到匿名方法再到LINQ
  6. ssh整合随笔(注解方式,Spring 管理action)
  7. linux tar打包
  8. Spring核心框架 - AOP的原理及源码解析
  9. nyoj VF函数
  10. spring拦截器
  11. [UWP小白日记-6]页面跳转过度动画
  12. Git的本地仓库与GitHub的远程仓库
  13. [HNOI2004]L语言
  14. .net基础学java系列(四)Console实操
  15. riakKV 配置
  16. js和jQuery的互相转换
  17. windows7 python3.4 安装scrapy
  18. SP10707 COT2 - Count on a tree II 莫队
  19. Spring Boot Mvc 单元测试
  20. Maven入门指南② :Maven 常用命令,手动创建第一个 Maven 项目

热门文章

  1. python加密解密之AES
  2. MQ 消息队列 比较
  3. Networking &amp;&amp; Internet 计网学习笔记一
  4. Xmake v2.7.7 发布,支持 Haiku 平台,改进 API 检测和 C++ Modules 支持
  5. python pip 升级失败解决方法
  6. CCRD_TOC_2007年12月_总第13期
  7. CSS3--通过Animation实现简单的手指点击动画
  8. OpenLayers地图标注及弹窗实现
  9. LeetCode算法训练-回溯总结
  10. 轻量级CI/CD发布部署环境搭建及使用_04_docker安装nexus3