ui-swiper.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <style>
  2. .swiper-img .bg{ width: 100%;}
  3. .slide-indicator{position:absolute;bottom:90rpx;left:0px;width:100%}
  4. .slide-indicator-item{width:20rpx;height:2rpx;background:rgba(255,255,255,0.4);margin:0 10rpx}
  5. .slide-indicator-item.select{background:white}
  6. </style>
  7. <template>
  8. <view>
  9. <view id="swiper" class="swiper">
  10. <swiper class="swiper-img" :style="height > 0 ? ('height:' + height + 'px') : '' " :indicator-dots="indicator" autoplay="true" @change="tapChange">
  11. <swiper-item @tap="tapItem(index)" v-for="(item,index) in swData">
  12. <image mode="widthFix" class="bg" :src="item.img"/>
  13. </swiper-item>
  14. </swiper>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. var swH = 0;
  20. export default {
  21. name: 'ui-swiper',
  22. props:{
  23. swData : {
  24. type: Array,
  25. default: []
  26. },
  27. indicator:{
  28. type:Boolean,
  29. default : false
  30. }
  31. },
  32. data() {
  33. return{
  34. imgsAr : [],
  35. index : 0,
  36. width : 0,
  37. height:0
  38. }
  39. },
  40. watch: {
  41. swData() {
  42. this.initView();
  43. }
  44. },
  45. created() {
  46. this.initView();
  47. this.$nextTick(() => {
  48. const query = uni.createSelectorQuery().in(this);
  49. query.select("#swiper").boundingClientRect(res=>{
  50. console.log("xxxxx");
  51. this.width = res.width;
  52. if(this.swData.length > 0) this.initImg(this.index);
  53. }).exec();
  54. })
  55. },
  56. mounted() {
  57. //this.width = ;
  58. },
  59. methods: {
  60. tapItem:function(index){
  61. this.$emit('tapItem',index);
  62. },
  63. tapChange:function(ev){
  64. this.index = ev.detail.current;
  65. if(this.imgsAr[this.index] != null) {
  66. this.setHeight(this.imgsAr[this.index].height);
  67. }
  68. },
  69. /**
  70. * 初始化
  71. */
  72. initView:function(){
  73. this.imgsAr = [];
  74. for(var i in this.swData) {
  75. this.imgsAr[i] = null;
  76. this.initImg(i);
  77. }
  78. if(swH > 0) {
  79. this.height = swH;
  80. }
  81. },
  82. /**
  83. * 初始化
  84. * @param {Object} i
  85. */
  86. initImg:function(i) {
  87. uni.getImageInfo({
  88. src:this.swData[i].img,
  89. success:(img) => {
  90. var width = 0;
  91. var height = 0;
  92. if(img.width > this.width) {
  93. width = this.width;
  94. } else {
  95. width = img.width;
  96. }
  97. //"width": 1500,
  98. //"height": 972,
  99. height = (img.height / img.width) * width;
  100. this.imgsAr[i] = { width:width, height:height, img:this.swData[i]};
  101. console.log(this.imgsAr[i]);
  102. if(this.index == i) {
  103. this.setHeight(height);
  104. }
  105. }
  106. });
  107. },
  108. setHeight:function (height)
  109. {
  110. this.height = height;
  111. swH = height;
  112. this.$emit('onHeight',{height:height,index:this.index});
  113. }
  114. }
  115. }
  116. </script>