tm-swiperListItem.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <view class="tm-swiperListItem fulled overflow absolute"
  3. :style="{
  4. height: itemHeight + 'rpx',
  5. top:scroll_data.scroll.top+(scroll_data.index)*itemHeight+'rpx'
  6. }">
  7. <view class="fulled fulled-height"><slot></slot></view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'tm-swiperListItem',
  13. props: {
  14. scroll: {
  15. type: Object,
  16. default: () => ({
  17. scroll: {
  18. showNum: 1, //当前视图内可容纳的条数
  19. totalSum: 40, //总条数
  20. topIndex: 0 //顶部位置条目的索引
  21. },
  22. index: 0 //当前索引。
  23. })
  24. },
  25. itemHeight: {
  26. type: Number,
  27. default: 100
  28. }
  29. },
  30. data() {
  31. return {
  32. show: false //是否渲染本条目。
  33. };
  34. },
  35. watch: {
  36. scroll: {
  37. deep: true,
  38. handler(val) {
  39. this.setShow();
  40. }
  41. }
  42. },
  43. computed: {
  44. scroll_data() {
  45. return this.scroll
  46. }
  47. },
  48. created() {
  49. this.setShow();
  50. },
  51. methods: {
  52. setShow() {
  53. if (this.scroll.index == this.scroll.scroll.topIndex) {
  54. this.show = true;
  55. return;
  56. }
  57. let shown = 3; //前后三条可以显示+上视图内的。
  58. let min = this.scroll.scroll.topIndex - shown;
  59. min = min <= 0 ? 0 : min;
  60. let max = this.scroll.scroll.topIndex + shown + this.scroll.scroll.showNum;
  61. if (this.scroll.index >= min && this.scroll.index <= max) {
  62. this.show = true;
  63. } else {
  64. this.show = false;
  65. }
  66. }
  67. }
  68. };
  69. </script>
  70. <style lang="scss"></style>