当前位置:   article > 正文

uniapp swiper 组件大数据优化_uniapp小程序端 swiper性能优化

uniapp小程序端 swiper性能优化

这是我目前使用的方式,首页视频流无限滚动时用到的,不影响预加载,用起来很流畅
swiper-item过多时导致界面卡顿,通过每次只渲染3个的方式实现无限滚动

  1. <template>
  2. <view class="content">
  3. <view class="title">{{originIndex +1 }}/{{ originList.length }}</view>
  4. <swiper class="swiper" circular @change="swiperChange" swiperDuration="250">
  5. <swiper-item v-for="(item, index) in displaySwiperList" :key="index">
  6. <view class="wrap_content">{{ item }} </view>
  7. </swiper-item>
  8. </swiper>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. originList: [], // 源数据
  16. displaySwiperList: [], // swiper需要的数据
  17. displayIndex: 0, // 用于显示swiper的真正的下标数值只有:0,1,2。
  18. originIndex: 0, // 记录源数据的下标
  19. };
  20. },
  21. methods: {
  22. /**
  23. * 初始一个显示的swiper数据
  24. * @originIndex 从源数据的哪个开始显示默认0,如从其他页面跳转进来,要显示第n个,这个参数就是他的下标
  25. */
  26. initSwiperData(originIndex = this.originIndex) {
  27. const originListLength = this.originList.length; // 源数据长度
  28. const displayList = [];
  29. displayList[this.displayIndex] = this.originList[originIndex];
  30. displayList[this.displayIndex - 1 == -1 ? 2 : this.displayIndex - 1] =
  31. this.originList[
  32. originIndex - 1 == -1 ? originListLength - 1 : originIndex - 1
  33. ];
  34. displayList[this.displayIndex + 1 == 3 ? 0 : this.displayIndex + 1] =
  35. this.originList[
  36. originIndex + 1 == originListLength ? 0 : originIndex + 1
  37. ];
  38. this.displaySwiperList = displayList;
  39. },
  40. /**
  41. * swiper滑动时候
  42. */
  43. swiperChange(event) {
  44. const { current } = event.detail;
  45. const originListLength = this.originList.length; // 源数据长度
  46. // =============向后==========
  47. if (this.displayIndex - current == 2 || this.displayIndex - current == -1) {
  48. this.originIndex =
  49. this.originIndex + 1 == originListLength ? 0 : this.originIndex + 1;
  50. this.displayIndex = this.displayIndex + 1 == 3 ? 0 : this.displayIndex + 1;
  51. this.initSwiperData(this.originIndex);
  52. }
  53. // ======如果两者的差为-2或者1则是向前滑动============
  54. else if (this.displayIndex - current == -2 || this.displayIndex - current == 1) {
  55. this.originIndex = this.originIndex - 1 == -1 ? originListLength - 1 : this.originIndex - 1;
  56. this.displayIndex = this.displayIndex - 1 == -1 ? 2 : this.displayIndex - 1;
  57. this.initSwiperData(this.originIndex);
  58. }
  59. },
  60. },
  61. created() {
  62. for (let i = 1; i <= 1300; i++) {
  63. this.originList.push(i);
  64. }
  65. this.initSwiperData();
  66. },
  67. };
  68. </script>
  69. <style>
  70. .title {
  71. width: 100%;
  72. display: flex;
  73. justify-content: center;
  74. align-items: center;
  75. height: 60rpx;
  76. }
  77. .swiper {
  78. height: calc(100vh - 120rpx);
  79. }
  80. .wrap_content {
  81. border-radius: 20rpx;
  82. display: flex;
  83. justify-content: center;
  84. align-items: center;
  85. background: gray;
  86. height: 100vh;
  87. color: aqua;
  88. font-size: 80px;
  89. margin: 0rpx 40rpx;
  90. }
  91. </style>

注意:

1、swiper-item的key一定要设置,并且用index。

  1. <swiper-item v-for="(item, index) in displaySwiperList" :key="index">
  2. <view class="wrap_content">{{ item }} </view>
  3. </swiper-item>

2、如果只有一张情况,不想让它来回滚动。可以设置circular,但是circular无法直接动态设置,我们可以使用computed来设置

  1. <template>
  2. <swiper :circular="!canCircular" > </swiper>
  3. </template>
  4. export default {
  5. data() {
  6. return {
  7. originList:[]
  8. }
  9. },
  10. computed: {
  11. canCircular() {
  12. return this.originList.length > 0; // 看这里重点
  13. }
  14. }
  15. }

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

闽ICP备14008679号