imgsBanner-tag.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template name='imgsBanner'>
  2. <view class="imgsBannerBox">
  3. <swiper class="imgsBanner_swiper" :autoplay='autoplay' :interval='Number(interval)' :duration='Number(duration)' :current='comCurrentImg' @change='changCurrent'>
  4. <swiper-item v-for="(item,index) in imgList" :key='index'>
  5. <image :src="getItemUrl(item)" mode="aspectFill"></image>
  6. </swiper-item>
  7. </swiper>
  8. <!-- 图片位置 -->
  9. <view class="imgLength">{{(comCurrentImg+1)+'/'+(imgList.length)}}</view>
  10. <scroll-view scroll-x="true" class="scrollImgBox" :scroll-left='scrollImgList' scroll-with-animation v-if="isShowSmallImgs">
  11. <view class="scrollImgList">
  12. <image :src="getItemUrl(item)" mode="aspectFill" v-for="(item,index) in imgList" :key='index' :class="comCurrentImg==index?'activeImageItem':''" @click="onClickImg(index)" :id="'item'+index"></image>
  13. </view>
  14. </scroll-view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name:'imgsBanner-tag',
  20. props:{
  21. imgList:{
  22. type:Array,
  23. default:()=>[]
  24. },
  25. currentImg:{
  26. type:Number,
  27. default:0
  28. },
  29. isShowSmallImgs:{ //是否需要展示小图
  30. type:Boolean,
  31. default:true
  32. },
  33. customizeDisplayField:{ //自定义展示字段
  34. type:String,
  35. default:''
  36. },
  37. // 是否循环
  38. autoplay:{
  39. type:Boolean,
  40. default:false
  41. },
  42. // 自动切换时间间隔
  43. interval:{
  44. type:[Number,String],
  45. default:5000
  46. },
  47. // 滑动动画时长
  48. duration:{
  49. type:[Number,String],
  50. default:500
  51. },
  52. },
  53. data() {
  54. return {
  55. comCurrentImg:0,
  56. scrollImgList:0,
  57. imgLeftList:[]
  58. };
  59. },
  60. created() {
  61. this.comCurrentImg = this.currentImg;
  62. },
  63. mounted() {
  64. uni.getSystemInfo({
  65. success: (res)=> {
  66. this.imgList.forEach((i,v)=>{
  67. let info = uni.createSelectorQuery().in(this);
  68. info.select("#item"+v).boundingClientRect((res)=>{
  69. this.imgLeftList.push(res.left)
  70. }).exec()
  71. })
  72. this.imgListScroll(this.comCurrentImg)
  73. }
  74. });
  75. },
  76. methods:{
  77. getItemUrl(item){
  78. let {customizeDisplayField} = this
  79. if(customizeDisplayField){
  80. return customizeDisplayField.split('.').reduce((v, k) => v[k], item)
  81. }else{
  82. return item
  83. }
  84. },
  85. onClickImg(index){
  86. this.comCurrentImg = index
  87. this.imgListScroll(index)
  88. },
  89. changCurrent(e){
  90. this.comCurrentImg = e.target.current
  91. this.imgListScroll(e.target.current)
  92. this.$emit('change',this.imgList[e.target.current],e.target.current)
  93. },
  94. // 图片滑动
  95. imgListScroll(index){
  96. if(index>=2){
  97. this.scrollImgList = this.imgLeftList[index-2]
  98. }else{
  99. this.scrollImgList = 0
  100. }
  101. }
  102. }
  103. }
  104. </script>
  105. <style lang="less" scoped>
  106. .imgsBannerBox{
  107. position: relative;
  108. }
  109. .imgLength{
  110. position: absolute;
  111. top: 694rpx;
  112. right: 24rpx;
  113. background:rgba(0,0,0,0.34);
  114. padding: 0 12rpx;
  115. line-height: 32rpx;
  116. font-size: 22rpx;
  117. border-radius: 16rpx;
  118. color: #fff;
  119. }
  120. .imgsBanner_swiper{
  121. width: 750rpx;
  122. height: 750rpx;
  123. margin-bottom: 24rpx;
  124. swiper-item{
  125. width: 750rpx;
  126. height: 100%;
  127. image{
  128. width: 750rpx;
  129. height: 750rpx;
  130. }
  131. }
  132. }
  133. .scrollImgBox{
  134. .scrollImgList{
  135. white-space: nowrap;
  136. image{
  137. width: 132rpx;
  138. height: 132rpx;
  139. margin-right: 16rpx;
  140. display: inline-block;
  141. border: 6rpx solid #fff;
  142. }
  143. image:last-child{
  144. margin-right: 0;
  145. }
  146. .activeImageItem{
  147. border: 6rpx solid #F57341;
  148. }
  149. }
  150. }
  151. </style>