swiperBg.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="swiperBg" :style="{marginTop:mt +'rpx'}">
  3. <view class="bag" v-if="isIframe || (imgUrls.length && isShow)">
  4. </view>
  5. <block v-if="isShow && imgUrls.length">
  6. <view class="swiper square" v-if="imgUrls.length">
  7. <swiper class="skeleton-rect" :style="'height:'+ (imageH) +'rpx;'" indicator-dots="true"
  8. :autoplay="true" :circular="circular" :interval="interval" :duration="duration"
  9. indicator-color="rgba(255,255,255,0.6)" indicator-active-color="#fff" :current="swiperCur"
  10. @change="swiperChange">
  11. <block v-for="(item,index) in imgUrls" :key="index">
  12. <swiper-item class="" :class="{active:index == swiperCur}">
  13. <view @click="goDetail(item)" class='slide-navigator acea-row row-between-wrapper'>
  14. <image :src="item.img" class="slide-image" mode="widthFix"
  15. :style="'height:'+ (imageH) +'rpx;'">
  16. </image>
  17. </view>
  18. </swiper-item>
  19. </block>
  20. </swiper>
  21. </view>
  22. </block>
  23. <block v-if="!isShow && isIframe && imgUrls.length && imageH">
  24. <view class="swiper square" v-if="imgUrls.length && imageH" :style="'height:'+ (imageH) +'rpx;'">
  25. <swiper :style="'height:'+ (imageH) +'rpx;'" indicator-dots="true" :autoplay="true" :circular="circular"
  26. :interval="interval" :duration="duration" indicator-color="rgba(255,255,255,0.6)"
  27. indicator-active-color="#fff">
  28. <block v-for="(item,index) in imgUrls" :key="index">
  29. <swiper-item>
  30. <view @click="goDetail(item)" class='slide-navigator acea-row row-between-wrapper'>
  31. <image :src="item.img" class="slide-image" mode="widthFix"
  32. :style="'height:'+ (imageH) +'rpx;'">
  33. </image>
  34. </view>
  35. </swiper-item>
  36. </block>
  37. </swiper>
  38. </view>
  39. </block>
  40. <block v-if="isIframe && (!imgUrls.length || !imageH)">
  41. <view class="empty-img">{{$t(`暂无图片,请上传图片`)}}</view>
  42. </block>
  43. </view>
  44. </template>
  45. <script>
  46. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  47. let app = getApp();
  48. import {
  49. goPage
  50. } from '@/libs/order.js'
  51. export default {
  52. name: 'swiperBg',
  53. props: {
  54. dataConfig: {
  55. type: Object,
  56. default: () => {}
  57. },
  58. },
  59. watch: {
  60. dataConfig: {
  61. immediate: true,
  62. handler(nVal, oVal) {
  63. if (nVal) {
  64. this.imgUrls = nVal.imgList ? nVal.imgList.list : [];
  65. this.isShow = nVal.isShow ? nVal.isShow.val : true
  66. uni.getImageInfo({
  67. src: this.imgUrls.length ? this.imgUrls[0].img : '',
  68. success: (res) => {
  69. if (res && res.height > 0) {
  70. this.$set(this, 'imageH',
  71. res.height / res
  72. .width * 690)
  73. } else {
  74. this.$set(this, 'imageH', 320);
  75. }
  76. },
  77. fail: (error) => {
  78. this.$set(this, 'imageH', 320);
  79. }
  80. })
  81. }
  82. }
  83. },
  84. imageH(nVal, oVal) {
  85. let self = this
  86. },
  87. },
  88. data() {
  89. return {
  90. indicatorDots: false,
  91. circular: true,
  92. autoplay: true,
  93. interval: 4000,
  94. duration: 500,
  95. imgUrls: [], //图片轮播数据
  96. name: this.$options.name,
  97. isIframe: false,
  98. mt: -55,
  99. isShow: true,
  100. imageH: 320,
  101. swiperCur: 0,
  102. };
  103. },
  104. created() {
  105. // #ifdef MP || APP-PLUS
  106. const res = uni.getSystemInfoSync()
  107. const system = res.platform
  108. this.statusBarHeight = res.statusBarHeight
  109. if (system === 'android') {
  110. this.mt = parseFloat(statusBarHeight) * 2 + 170
  111. } else {
  112. this.mt = parseFloat(statusBarHeight) * 2 + 168
  113. }
  114. // #endif
  115. this.isIframe = app.globalData.isIframe;
  116. },
  117. mounted() {},
  118. methods: {
  119. goDetail(url) {
  120. goPage().then(res => {
  121. let urls = url.info[1].value
  122. if (urls) {
  123. if (urls.indexOf("http") != -1) {
  124. // #ifdef H5
  125. location.href = urls
  126. // #endif
  127. } else {
  128. if (['/pages/goods_cate/goods_cate', '/pages/order_addcart/order_addcart',
  129. '/pages/user/index'
  130. ].indexOf(urls) ==
  131. -1) {
  132. uni.navigateTo({
  133. url: urls
  134. })
  135. } else {
  136. uni.switchTab({
  137. url: urls,
  138. fail() {
  139. uni.reLaunch({
  140. url: urls
  141. })
  142. }
  143. })
  144. }
  145. }
  146. }
  147. })
  148. },
  149. //替换安全域名
  150. setDomain: function(url) {
  151. url = url ? url.toString() : '';
  152. //本地调试打开,生产请注销
  153. if (url.indexOf("https://") > -1) return url;
  154. else return url.replace('http://', 'https://');
  155. },
  156. swiperChange(e) {
  157. // this.swiperCur = e.detail.current
  158. let {
  159. current,
  160. source
  161. } = e.detail
  162. if (source === 'autoplay' || source === 'touch') {
  163. //根据官方 source 来进行判断swiper的change事件是通过什么来触发的,autoplay是自动轮播。touch是用户手动滑动。其他的就是未知问题。抖动问题主要由于未知问题引起的,所以做了限制,只有在自动轮播和用户主动触发才去改变current值,达到规避了抖动bug
  164. this.swiperCur = e.detail.current
  165. }
  166. },
  167. }
  168. }
  169. </script>
  170. <style lang="scss">
  171. .swiperBg /deep/ .uni-swiper-slides {
  172. overflow: hidden;
  173. border-radius: 10rpx;
  174. }
  175. .swiperBg {
  176. background-color: #fff;
  177. position: relative;
  178. margin-top: -20rpx;
  179. padding-top: 4rpx;
  180. .bag {
  181. position: absolute;
  182. top: 0;
  183. width: 100%;
  184. height: 140rpx;
  185. background: linear-gradient(90deg, var(--view-main-start) 0%, var(--view-main-over) 100%);
  186. border-bottom-left-radius: 40rpx;
  187. border-bottom-right-radius: 40rpx;
  188. }
  189. /* #ifdef APP-PLUS */
  190. /* #endif */
  191. .colorBg {
  192. position: absolute;
  193. left: 0;
  194. top: 0;
  195. height: 130rpx;
  196. width: 100%;
  197. }
  198. .swiper {
  199. z-index: 100;
  200. position: relative;
  201. min-height: 200rpx;
  202. padding: 0 $uni-index-margin-col;
  203. /* #ifdef APP-PLUS */
  204. // margin: 0rpx auto 0 auto;
  205. /* #endif */
  206. overflow: hidden;
  207. /* #ifdef MP */
  208. /* #endif */
  209. /* 设置圆角 */
  210. &.fillet {
  211. border-radius: 10rpx;
  212. image {
  213. border-radius: 10rpx;
  214. }
  215. }
  216. swiper,
  217. .swiper-item,
  218. image {
  219. width: 100%;
  220. overflow: hidden;
  221. border-radius: 10rpx;
  222. }
  223. .slide-navigator {}
  224. image {
  225. transform: scale(1);
  226. // transition: all .3s ease;
  227. }
  228. swiper-item.active {
  229. image {
  230. transform: scale(1);
  231. }
  232. }
  233. // 圆形指示点
  234. &.circular {
  235. /deep/.uni-swiper-dot {
  236. width: 10rpx !important;
  237. height: 10rpx !important;
  238. background: rgba(0, 0, 0, .4) !important
  239. }
  240. /deep/.uni-swiper-dot-active {
  241. background: #fff !important
  242. }
  243. }
  244. // 方形指示点
  245. &.square {
  246. /deep/.uni-swiper-dot {
  247. width: 20rpx !important;
  248. height: 5rpx !important;
  249. border-radius: 3rpx;
  250. background: rgba(0, 0, 0, .4) !important
  251. }
  252. /deep/.uni-swiper-dot-active {
  253. background: #fff !important
  254. }
  255. }
  256. }
  257. }
  258. .item-img image {
  259. display: block;
  260. width: 100%;
  261. border-radius: 10rpx;
  262. }
  263. .empty-img {
  264. width: 690rpx;
  265. height: 300rpx;
  266. border-radius: 14rpx;
  267. margin: 26rpx auto 0 auto;
  268. background-color: #ccc;
  269. text-align: center;
  270. line-height: 300rpx;
  271. position: relative;
  272. .iconfont {
  273. font-size: 50rpx;
  274. }
  275. }
  276. </style>