index.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <swiper class="swiper" @change="viewChange" :autoplay="true" circular :indicator-dots="true"
  3. indicator-color="#F5F5F5" indicator-active-color="#ffffff" v-if="imgsList.length"
  4. :style="[{ height: swiperHeight + 'px' }]">
  5. <swiper-item v-for="item in imgsList" :key="item.id">
  6. <view class="swiper-item uni-bg-red">
  7. <image :src="item.url" mode="widthFix" class="img" @load="loadImg"></image>
  8. </view>
  9. </swiper-item>
  10. </swiper>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'swiperImg',
  15. props: {
  16. imgsList: Array,
  17. },
  18. data() {
  19. return {
  20. swiperHeight: 0,
  21. current: 0,
  22. };
  23. },
  24. methods: {
  25. loadImg() {
  26. this.getCurrentSwiperHeight('.img');
  27. },
  28. // 轮播切换
  29. viewChange(e) {
  30. this.current = e.target.current;
  31. this.getCurrentSwiperHeight('.img');
  32. },
  33. // 动态获取内容高度
  34. getCurrentSwiperHeight(element) {
  35. let query = uni.createSelectorQuery().in(this);
  36. query.selectAll(element).boundingClientRect();
  37. query.exec((res) => {
  38. // 切换到其他页面swiper的change事件仍会触发,这时获取的高度会是0,会导致回到使用swiper组件的页面不显示了
  39. if (this.imgsList.length && res[0][this.current].height) {
  40. this.swiperHeight = res[0][this.current].height;
  41. }
  42. });
  43. },
  44. },
  45. };
  46. </script>
  47. <style lang="scss" scoped>
  48. .swiper {
  49. padding: 20rpx;
  50. border-radius: 4px;
  51. .swiper-item {
  52. width: 100%;
  53. image {
  54. width: 100%;
  55. }
  56. }
  57. }
  58. </style>