当前位置:   article > 正文

vue原生div做触底加载_vue触底加载

vue触底加载

第一种:

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

(1)clientHeight:屏幕的高度;

(2)scrollTop:滚动的高度;

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

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

以下代码实例:

html代码:

  1. <el-tooltip content="打开搜索框" placement="bottom" effect="light">
  2. <i class="el-icon-search" @click.stop="openSearch"></i>
  3. </el-tooltip>
  4. <!-- 搜索弹框 -->
  5. <div class="searchMask" v-if="searchTemplateShow" @click.stop>
  6. <div class="searchBox" @click.stop>
  7. <div class="searchHeader">
  8. <i class="el-icon-close" @click.stop="searchTemplateShow = false"></i>
  9. <el-input
  10. v-model="searchInfo.title"
  11. placeholder="搜索模版"
  12. clearable
  13. prefix-icon="el-icon-search"
  14. @keyup.enter.native="search"
  15. @keyup.ctrl.enter.native="openNewLink"
  16. style="width: 100%;"
  17. class="input-with-select">
  18. <el-select v-model="templateType" slot="prepend" placeholder="请选择模版类型" @change="search">
  19. <el-option v-for="item in templateTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option>
  20. </el-select>
  21. </el-input>
  22. </div>
  23. <div class="emptySearch" v-if="wtf">
  24. <img src="@/assets/img/emptySearch.png" />
  25. <div>搜索无结果</div>
  26. </div>
  27. <div class="searchCenter" v-else>
  28. <div class="searchContent" v-loading="templateLoading" :style="`height:${templateLoading ? '452px' : 'auto'};`">
  29. <div class="searchContentTitle">选择模版</div>
  30. <div class="searchContentBox" ref="searchContentBox" @scroll="scrollBottom">
  31. <div class="searchItem" v-for="(item, index) in templateData" :key="item.id" @click.stop="createDoc(item.id)" :ref="`searchItem${index}`">
  32. <div class="searchItemIcon">
  33. <img src="@/assets/img/templateSearchIcon.png" />
  34. </div>
  35. <div class="searchItemContent">
  36. <div class="searchItemTitle one" v-if="item.title">{{ item.title }}</div>
  37. <div class="searchItemDesc" v-if="item.desc">{{ item.desc }}</div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="searchFooter">
  43. <span>{{ total || 0 }}</span>
  44. <span>结果</span>
  45. <span>支持ENTER搜索、CTRL+ENTER新窗口打开</span>
  46. </div>
  47. </div>
  48. </div>
  49. </div>

js变量代码:

  1. // 搜索模版弹框
  2. searchTemplateShow: false,
  3. // 模版类型 1:热门 2:我的
  4. templateType: 1,
  5. templateTypeData: [{
  6. label: "热门",
  7. value: 1
  8. },{
  9. label: "我的",
  10. value: 2
  11. }],
  12. // 搜索条件对象
  13. searchInfo: {
  14. title: ""
  15. },
  16. // 搜索模版loading
  17. templateLoading: false,
  18. // 搜索模版数据
  19. templateData: [],
  20. page: 1,
  21. total: null,
  22. mask: true,
  23. wtf: false

js方法代码:

  1. /**
  2. * 搜索弹框显示
  3. */
  4. openSearch() {
  5. if (!this.userInfo.type) {
  6. this.showLogin = true;
  7. return false;
  8. };
  9. Object.keys(this.searchInfo).forEach(key => {
  10. this.searchInfo[key] = "";
  11. });
  12. this.searchTemplateShow = true;
  13. this.search();
  14. },
  15. /**
  16. * 搜索
  17. */
  18. search() {
  19. this.templateLoading = true;
  20. this.page = 1;
  21. this.templateData = [];
  22. this.mask = true;
  23. this.wtf = false;
  24. this.getTemplateData();
  25. },
  26. /**
  27. * 滚动到下面
  28. */
  29. scrollBottom() {
  30. let clientHeight = this.$refs.searchContentBox.clientHeight;
  31. let scrollTop = this.$refs.searchContentBox.scrollTop;
  32. let offsetTop = this.$refs[`searchItem${this.templateData.length - 1}`][0].offsetTop;
  33. if (((clientHeight + scrollTop + 300) >= offsetTop) && this.mask) this.getTemplateData();
  34. },
  35. /**
  36. * 获取模版数据
  37. */
  38. getTemplateData: debounce(async function () {
  39. this.templateLoading = true;
  40. let params = {
  41. ...this.searchInfo,
  42. page: this.page,
  43. size: 10,
  44. is_not_template: 1
  45. }
  46. let res = await document.getTemplateData(params, this.templateType);
  47. if (res.code == 200) {
  48. this.templateLoading = false;
  49. res.data.data.forEach(item => {
  50. this.templateData.push(item);
  51. });
  52. this.total = res.data.total;
  53. this.wtf = this.templateData.length == 0 ? true : false;
  54. this.page == res.data.last_page ? this.mask = false : this.page++;
  55. };
  56. }, 300),
  57. /**
  58. * 打开新链接搜索
  59. */
  60. openNewLink() {
  61. window.open(window.location.href);
  62. },
  63. /**
  64. * 创建文档
  65. */
  66. createDoc(id) {
  67. if (!this.userInfo.type) {
  68. this.showLogin = true;
  69. return false;
  70. };
  71. this.$parent.createDoc(id);
  72. this.searchTemplateShow = false;
  73. }

css代码:

  1. .el-icon-search, .el-icon-plus {
  2. width: 24px;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. &:hover {
  7. background-color: rgba(0,0,0,.04);
  8. border-radius: 2px;
  9. cursor: pointer;
  10. }
  11. }
  12. .searchMask {
  13. width: 100vw;
  14. height: 100vh;
  15. background-color: rgba(30, 30, 30, .8);
  16. position: fixed;
  17. bottom: 0;
  18. left: 0;
  19. z-index: 11;
  20. display: flex;
  21. align-items: center;
  22. flex-direction: column;
  23. justify-content: center;
  24. .searchBox {
  25. width: 600px;
  26. height: auto;
  27. background: #FFFFFF;
  28. box-shadow: 0px 6px 28px -3px rgba(165,165,165,0.36);
  29. border-radius: 10px;
  30. .searchHeader, .searchContentBox, .emptySearch, .searchContent, .searchFooter {
  31. width: 100%;
  32. box-sizing: border-box;
  33. }
  34. .searchHeader {
  35. height: 117px;
  36. padding: 60px 28px 13px 28px;
  37. border-bottom: 1px solid #F5F5F5;
  38. position: relative;
  39. .el-icon-close {
  40. position: absolute;
  41. top: 24px;
  42. right: 23px;
  43. font-size: 18px;
  44. color: #111111;
  45. cursor: pointer;
  46. }
  47. /deep/ .el-select .el-input {
  48. width: 95px;
  49. }
  50. /deep/ .input-with-select .el-input-group__prepend {
  51. background-color: #fff;
  52. }
  53. }
  54. .searchCenter {
  55. height: auto;
  56. .searchContent {
  57. height: auto;
  58. .searchContentTitle {
  59. width: 100%;
  60. box-sizing: border-box;
  61. padding: 20px 28px 10px 28px;
  62. font-weight: 400;
  63. font-size: 15px;
  64. color: #999999;
  65. }
  66. .searchContentBox {
  67. width: 100%;
  68. height: auto;
  69. max-height: 400px;
  70. overflow-y: auto;
  71. .searchItem {
  72. width: 100%;
  73. height: auto;
  74. box-sizing: border-box;
  75. padding: 12px 28px;
  76. display: flex;
  77. cursor: pointer;
  78. &:hover {
  79. background-color: rgba(51, 77, 102, 0.06);
  80. }
  81. .searchItemIcon {
  82. width: 16px;
  83. height: 16px;
  84. margin: 2px 12px 0 0;
  85. img {
  86. width: 100%;
  87. height: 100%;
  88. }
  89. }
  90. .searchItemContent {
  91. max-width: 508px;
  92. .searchHeader, .searchContent {
  93. max-width: 508px;
  94. font-weight: 400;
  95. }
  96. .searchItemTitle {
  97. font-size: 15px;
  98. color: #333333;
  99. line-height: 21px;
  100. }
  101. .searchItemDesc {
  102. font-size: 13px;
  103. color: #999999;
  104. line-height: 18px;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. .searchFooter {
  111. height: 40px;
  112. padding: 10px 28px;
  113. box-shadow: 0 6px 28px -3px rgba(165,165,165,0.36);
  114. font-weight: 400;
  115. font-size: 14px;
  116. color: #999999;
  117. line-height: 20px;
  118. & > span:first-of-type {
  119. color: rgba(0, 0, 0, 0.64);
  120. margin-right: 5px;
  121. }
  122. & > span:last-of-type {
  123. color: rgba(0, 0, 0, 0.64);
  124. margin-left: 16px;
  125. }
  126. }
  127. }
  128. .emptySearch {
  129. height: 295px;
  130. font-size: 14px;
  131. color: rgba(0, 0, 0, 0.4);
  132. display: flex;
  133. flex-direction: column;
  134. justify-content: center;
  135. align-items: center;
  136. img {
  137. width: 128px;
  138. height: 128px;
  139. margin-bottom: 20px;
  140. }
  141. }
  142. }
  143. }

 第二种:

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

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

例:

  1. <template>
  2. <ul class="infinite-list" v-infinite-scroll="load" style="overflow:auto">
  3. <li v-for="i in count" class="infinite-list-item">{{ i }}</li>
  4. </ul>
  5. </template>
  6. <script>
  7. export default {
  8. data () {
  9. return {
  10. count: 0
  11. }
  12. },
  13. methods: {
  14. load () {
  15. this.count += 2
  16. }
  17. }
  18. }
  19. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/698614
推荐阅读
相关标签
  

闽ICP备14008679号