vue原生div做触底加载

server/2025/2/12 3:40:46/

第一种:

触底加载和图片懒加载的思路一样,屏幕的高度加上滚动的高度快要大于最后一个元素距离顶部的高度的时候就开始加载数据;

(1)clientHeight:屏幕的高度;

(2)scrollTop:滚动的高度;

(3)offsetTop:最后一个元素距离顶部的高度;

if ((clientHeight + scrollTop + 快要到底的高度) >= offsetTop) 获取数据;

以下代码实例:

html代码:

<el-tooltip content="打开搜索框" placement="bottom" effect="light"><i class="el-icon-search" @click.stop="openSearch"></i>
</el-tooltip><!-- 搜索弹框 -->
<div class="searchMask" v-if="searchTemplateShow" @click.stop><div class="searchBox" @click.stop><div class="searchHeader"><i class="el-icon-close" @click.stop="searchTemplateShow = false"></i><el-inputv-model="searchInfo.title"placeholder="搜索模版"clearableprefix-icon="el-icon-search"@keyup.enter.native="search"@keyup.ctrl.enter.native="openNewLink"style="width: 100%;"class="input-with-select"><el-select v-model="templateType" slot="prepend" placeholder="请选择模版类型" @change="search"><el-option v-for="item in templateTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option></el-select></el-input></div><div class="emptySearch" v-if="wtf"><img src="@/assets/img/emptySearch.png" /><div>搜索无结果</div></div><div class="searchCenter" v-else><div class="searchContent" v-loading="templateLoading" :style="`height:${templateLoading ? '452px' : 'auto'};`"><div class="searchContentTitle">选择模版</div><div class="searchContentBox" ref="searchContentBox" @scroll="scrollBottom"><div class="searchItem" v-for="(item, index) in templateData" :key="item.id" @click.stop="createDoc(item.id)" :ref="`searchItem${index}`"><div class="searchItemIcon"><img src="@/assets/img/templateSearchIcon.png" /></div><div class="searchItemContent"><div class="searchItemTitle one" v-if="item.title">{{ item.title }}</div><div class="searchItemDesc" v-if="item.desc">{{ item.desc }}</div></div></div></div></div><div class="searchFooter"><span>{{ total || 0 }}</span><span>结果</span><span>支持ENTER搜索、CTRL+ENTER新窗口打开</span></div></div></div>
</div>

js变量代码:

javascript">// 搜索模版弹框
searchTemplateShow: false,
// 模版类型 1:热门 2:我的
templateType: 1,
templateTypeData: [{label: "热门",value: 1
},{label: "我的",value: 2
}],
// 搜索条件对象
searchInfo: {title: ""
},
// 搜索模版loading
templateLoading: false,
// 搜索模版数据
templateData: [],
page: 1,
total: null,
mask: true,
wtf: false

js方法代码:

javascript">/*** 搜索弹框显示*/
openSearch() {if (!this.userInfo.type) {this.showLogin = true;return false;};Object.keys(this.searchInfo).forEach(key => {this.searchInfo[key] = "";});this.searchTemplateShow = true;this.search();
},
/*** 搜索*/
search() {this.templateLoading = true;this.page = 1;this.templateData = [];this.mask = true;this.wtf = false;this.getTemplateData();
},
/*** 滚动到下面*/
scrollBottom() {let clientHeight = this.$refs.searchContentBox.clientHeight;let scrollTop = this.$refs.searchContentBox.scrollTop;let offsetTop = this.$refs[`searchItem${this.templateData.length - 1}`][0].offsetTop;if (((clientHeight + scrollTop + 300) >= offsetTop) && this.mask) this.getTemplateData();
},
/*** 获取模版数据*/
getTemplateData: debounce(async function () {this.templateLoading = true;let params = {...this.searchInfo,page: this.page,size: 10,is_not_template: 1}let res = await document.getTemplateData(params, this.templateType);if (res.code == 200) {this.templateLoading = false;res.data.data.forEach(item => {this.templateData.push(item);});this.total = res.data.total;this.wtf = this.templateData.length == 0 ? true : false;this.page == res.data.last_page ? this.mask = false : this.page++;};
}, 300),
/*** 打开新链接搜索*/
openNewLink() {window.open(window.location.href);
},
/*** 创建文档*/
createDoc(id) {if (!this.userInfo.type) {this.showLogin = true;return false;};this.$parent.createDoc(id);this.searchTemplateShow = false;
}

css代码:

.el-icon-search, .el-icon-plus {width: 24px;display: flex;align-items: center;justify-content: center;&:hover {background-color: rgba(0,0,0,.04);border-radius: 2px;cursor: pointer;}
}.searchMask {width: 100vw;height: 100vh;background-color: rgba(30, 30, 30, .8);position: fixed;bottom: 0;left: 0;z-index: 11;display: flex;align-items: center;flex-direction: column;justify-content: center;.searchBox {width: 600px;height: auto;background: #FFFFFF;box-shadow: 0px 6px 28px -3px rgba(165,165,165,0.36);border-radius: 10px;.searchHeader, .searchContentBox, .emptySearch, .searchContent, .searchFooter {width: 100%;box-sizing: border-box;}.searchHeader {height: 117px;padding: 60px 28px 13px 28px;border-bottom: 1px solid #F5F5F5;position: relative;.el-icon-close {position: absolute;top: 24px;right: 23px;font-size: 18px;color: #111111;cursor: pointer;}/deep/ .el-select .el-input {width: 95px;}/deep/ .input-with-select .el-input-group__prepend {background-color: #fff;}}.searchCenter {height: auto;.searchContent {height: auto;.searchContentTitle {width: 100%;box-sizing: border-box;padding: 20px 28px 10px 28px;font-weight: 400;font-size: 15px;color: #999999;}.searchContentBox {width: 100%;height: auto;max-height: 400px;overflow-y: auto;.searchItem {width: 100%;height: auto;box-sizing: border-box;padding: 12px 28px;display: flex;cursor: pointer;&:hover {background-color: rgba(51, 77, 102, 0.06);}.searchItemIcon {width: 16px;height: 16px;margin: 2px 12px 0 0;img {width: 100%;height: 100%;}}.searchItemContent {max-width: 508px;.searchHeader, .searchContent {max-width: 508px;font-weight: 400;}.searchItemTitle {font-size: 15px;color: #333333;line-height: 21px;}.searchItemDesc {font-size: 13px;color: #999999;line-height: 18px;}}}}}.searchFooter {height: 40px;padding: 10px 28px;box-shadow: 0 6px 28px -3px rgba(165,165,165,0.36);font-weight: 400;font-size: 14px;color: #999999;line-height: 20px;& > span:first-of-type {color: rgba(0, 0, 0, 0.64);margin-right: 5px;}& > span:last-of-type {color: rgba(0, 0, 0, 0.64);margin-left: 16px;}}}.emptySearch {height: 295px;font-size: 14px;color: rgba(0, 0, 0, 0.4);display: flex;flex-direction: column;justify-content: center;align-items: center;img {width: 128px;height: 128px;margin-bottom: 20px;}}}
}

 第二种:

使用element ui的v-infinite-scroll属性添加触底加载事件:

v-infinite-scroll=触底加载事件

例:

javascript"><template><ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto"><li v-for="i in count" class="infinite-list-item">{{ i }}</li></ul>
</template><script>export default {data () {return {count: 0}},methods: {load () {this.count += 2}}}
</script>

http://www.ppmy.cn/server/39198.html

相关文章

WPF之自定义绘图

1&#xff0c;创建自定义控件类 class CustomDrawnElement:FrameworkElement{public static readonly DependencyProperty BackgroundColorProperty;static CustomDrawnElement(){FrameworkPropertyMetadata meta new FrameworkPropertyMetadata(Colors.SkyBlue);meta.Affects…

[muduo网络库]——muduo库的Reactor模型(剖析muduo网络库核心部分、设计思想)

一、前言 在学习 C 服务端的过程中&#xff0c;必不可少的一项就是熟悉一个网络库&#xff0c;包括网络库的应用和其底层实现。我们熟知的网络库有 libevent、libev、muduo、Netty 等&#xff0c;其中 muduo 是由陈硕大佬个人开发的 TCP 网络库&#xff0c;最近跟着课程正在深…

leetcode尊享面试——二叉树(python)

250.统计同值子树 使用dfs深度搜索&#xff0c;同值子树&#xff0c;要满足三个条件&#xff1a; 对于当前节点node&#xff0c;他的左子树血脉纯净&#xff08;为同值子树&#xff09;&#xff0c;右子树血脉纯净&#xff08;为同值子树&#xff09;&#xff0c;node的值等于…

上亿用户面临风险!小米、WPS等知名安卓应用竟藏有“文件覆盖”漏洞

Google Play商店中的几款热门安卓应用程序容易受到与路径遍历相关的漏洞攻击&#xff0c;该漏洞的代号为“Dirty Stream”攻击&#xff0c;恶意应用程序可能会利用此漏洞覆盖易受攻击的应用程序主目录中的任意文件。 微软威胁情报团队的Dimitrios Valsamaras在周三发布的一份报…

Pytorch中的self.parameters()

文章目录 1. 作用2. 例子3.与.state_dict()的区别4.一个对比的例子 1. 作用 在 PyTorch 中&#xff0c;self.parameters() 是一个模型方法&#xff0c;它返回模型中所有需要优化的参数。这些参数通常是模型中的权重和偏置项。 当你定义一个 PyTorch 模型类时&#xff0c;你会…

【XR806开发板试用】XR806与鸿蒙,创建任务,串口转发TCPServer收到的数据

很荣幸获得评测开发板的机会&#xff0c;XR806的程序资料做的还是挺不错的。 目标&#xff1a; 1、学习用鸿蒙创建2个任务&#xff1b; 2、创建TCP Server收发数据。 任务ledThread&#xff1a;LED每秒亮灭一次&#xff0c;代表程序在运行。 任务MainThread&#xff1a;创建TCP…

echarts指标盘属性概括

echarts指标盘属性概括 代码 有模拟数据可以直接使用const options {animation: true,title: {top: "35%",left: "center",// text: "单元测试覆盖度", // 主标题itemGap: 15,textStyle: {// 主标题样式color: "#666666",fontSize:…

AVL树的原理及其实现

文章目录 前言了解AVL树AVL树的特点AVL树的节点调整方案右单旋为什么要右单旋呢&#xff1f;右单旋代码 左单旋为什么要左单旋&#xff1f;左单旋代码 左右双旋左右双旋之后平衡因子的情况左右双旋代码实现 右左双旋右左双旋代码&#xff1a; 简单测试 前言 回顾我们对于二叉搜…