..._vue 上滑翻页,下拉刷新">
当前位置:   article > 正文

Vue 使用better-scroll 上拉刷新,下拉加载_vue 上滑翻页,下拉刷新

vue 上滑翻页,下拉刷新

当 better-scroll 遇见 Vue - 知乎

Vue中引入better-scroll - 简书

基于better-scroll实现移动端vue上拉加载下拉刷新 - 简书

better-scroll&vue实现上拉加载更多下拉刷新_inyiyi的博客-CSDN博客_vue下拉加载更多

better-scroll.js文档_汇而不置的博客-CSDN博客_better-scroll文档

Vue pc端下拉加载更多

Vue自定义指令实现pc端加载更多 - wmui - 博客园

vue实现pc端无限加载功能 - wmui - 博客园

  1. <template>
  2. <div class="tabbar">
  3. <div class="wrapper" ref="wrapper">
  4. <div class="bscroll-container">
  5. <!-- 刷新提示信息 -->
  6. <div class="top-tip">
  7. <span class="refresh-hook">{{ pulldownMsg }}</span>
  8. </div>
  9. <!-- 内容列表 -->
  10. <ul class="content">
  11. <li v-for="(item, i) in data" :key="i">{{ item }}</li>
  12. </ul>
  13. <!-- 底部提示信息 -->
  14. <div class="bottom-tip">
  15. <span class="loading-hook">{{ pullupMsg }}</span>
  16. </div>
  17. </div>
  18. </div>
  19. <!-- alert提示刷新成功 -->
  20. <div class="alert-hook" :style="{ display: alertHook }">刷新成功</div>
  21. </div>
  22. </template>
  23. <script>
  24. import BScroll from "better-scroll";
  25. export default {
  26. data() {
  27. return {
  28. data: [0, 1, 2, 3, 4, 5, 6],
  29. pulldownMsg: "下拉刷新",
  30. pullupMsg: "加载更多",
  31. alertHook: "none",
  32. count: 1,
  33. };
  34. },
  35. created() {
  36. const that = this;
  37. this.$nextTick(() => {
  38. this.scroll = new BScroll(this.$refs.wrapper, {
  39. //初始化better-scroll
  40. probeType: 1, //1 滚动的时候会派发scroll事件,会截流。2滚动的时候实时派发scroll事件,不会截流。 3除了实时派发scroll事件,在swipe的情况下仍然能实时派发scroll事件
  41. click: true, //是否派发click事件
  42. });
  43. // 滑动过程中事件
  44. this.scroll.on("scroll", (pos) => {
  45. if (pos.y > 30) {
  46. this.pulldownMsg = "释放立即刷新";
  47. }
  48. });
  49. //滑动结束松开事件
  50. this.scroll.on("touchEnd", (pos) => {
  51. //上拉刷新
  52. if (pos.y > 30) {
  53. setTimeout(() => {
  54. that.getData().then((res) => {
  55. //刷新数据
  56. that.data = res;
  57. //恢复刷新提示文本值
  58. that.pulldownMsg = "下拉刷新";
  59. //刷新成功后提示
  60. that.refreshalert();
  61. //刷新列表后,重新计算滚动区域高度
  62. that.scroll.refresh();
  63. });
  64. }, 1500);
  65. } else if (pos.y < this.scroll.maxScrollY - 30) {
  66. //下拉加载
  67. this.pullupMsg = "加载中。。。";
  68. setTimeout(() => {
  69. that.getData().then((res) => {
  70. //恢复文本值
  71. that.pullupMsg = "加载更多";
  72. that.data = this.data.concat(res);
  73. that.scroll.refresh();
  74. });
  75. }, 1500);
  76. }
  77. });
  78. });
  79. },
  80. mounted() {
  81. let wrapper = document.querySelector(".wrapper");
  82. let scroll = new BScroll(wrapper, {
  83. scrollY: true,
  84. click: true,
  85. probeType: 2, //实时获取滚动坐标位置
  86. });
  87. },
  88. methods: {
  89. getData() {
  90. return new Promise((resolve) => {
  91. //模拟数据请求
  92. setTimeout(() => {
  93. const arr = [];
  94. for (let i = 0; i < 20; i++) {
  95. arr.push(this.count++);
  96. }
  97. resolve(arr);
  98. }, 1000);
  99. });
  100. },
  101. refreshalert() {
  102. //刷新成功提示
  103. this.alertHook = "block";
  104. setTimeout(() => {
  105. this.alertHook = "none";
  106. }, 1000);
  107. },
  108. },
  109. };
  110. </script>
  111. <style lang="" scoped>
  112. .wrapper {
  113. width: 100%;
  114. height: 300px;
  115. background: #ccc;
  116. overflow: hidden;
  117. position: relative;
  118. }
  119. li {
  120. line-height: 50px;
  121. border-bottom: 1px solid #ccc;
  122. text-align: center;
  123. }
  124. /* 下拉、上拉提示信息 */
  125. .top-tip {
  126. position: absolute;
  127. top: -40px;
  128. left: 0;
  129. z-index: 1;
  130. width: 100%;
  131. height: 40px;
  132. line-height: 40px;
  133. text-align: center;
  134. color: #555;
  135. }
  136. .bottom-tip {
  137. width: 100%;
  138. height: 35px;
  139. line-height: 35px;
  140. text-align: center;
  141. color: #777;
  142. background: #f2f2f2;
  143. position: absolute;
  144. bottom: -35px;
  145. left: 0;
  146. }
  147. /* 全局提示信息 */
  148. .alert-hook {
  149. display: none;
  150. position: fixed;
  151. top: 62px;
  152. left: 0;
  153. z-index: 2;
  154. width: 100%;
  155. height: 35px;
  156. line-height: 35px;
  157. text-align: center;
  158. color: #fff;
  159. font-size: 12px;
  160. background: rgba(7, 17, 27, 0.5);
  161. }
  162. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/517192
推荐阅读
相关标签
  

闽ICP备14008679号