index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <!-- 商品详情轮播图 -->
  3. <view class="product-bg" :style="[swiperHeight]">
  4. <swiper
  5. :indicator-dots="indicatorDots"
  6. indicator-active-color="var(--view-theme)"
  7. :autoplay="autoplay"
  8. :circular="circular"
  9. :interval="interval"
  10. :duration="duration"
  11. @change="change"
  12. v-if="isPlay"
  13. >
  14. <!-- #ifndef APP-PLUS -->
  15. <swiper-item v-if="videoline">
  16. <view class="item">
  17. <view v-show="!controls" class="item-box">
  18. <video
  19. id="myVideo"
  20. :src="videoline"
  21. objectFit="cover"
  22. controls
  23. style="width: 100%; height: 100%"
  24. show-center-play-btn
  25. show-mute-btn="true"
  26. auto-pause-if-navigate
  27. :custom-cache="false"
  28. :enable-progress-gesture="false"
  29. :poster="imgUrls[0]"
  30. @pause="videoPause"
  31. ></video>
  32. </view>
  33. <view class="poster" v-show="controls">
  34. <image class="image" :src="imgUrls[0]"></image>
  35. </view>
  36. <view class="stop" v-show="controls" @tap="bindPause">
  37. <image class="image" src="../../static/images/stop.png"></image>
  38. </view>
  39. </view>
  40. </swiper-item>
  41. <!-- #endif -->
  42. <!-- #ifdef APP-PLUS -->
  43. <swiper-item v-if="videoline">
  44. <view class="item">
  45. <view class="poster" v-show="controls">
  46. <image class="image" :src="imgUrls[0]"></image>
  47. </view>
  48. <view class="stop" v-show="controls" @tap="bindPause">
  49. <image class="image" src="../../static/images/stop.png"></image>
  50. </view>
  51. </view>
  52. </swiper-item>
  53. <!-- #endif -->
  54. <block v-for="(item, index) in imgUrls" :key="index">
  55. <swiper-item v-if="videoline ? index >= 1 : index >= 0">
  56. <image :src="item" class="slide-image" />
  57. </swiper-item>
  58. </block>
  59. </swiper>
  60. <!-- #ifdef APP-PLUS -->
  61. <view v-if="!isPlay" style="width: 100%; height: 750rpx">
  62. <video
  63. id="myVideo"
  64. :src="videoline"
  65. objectFit="cover"
  66. controls
  67. style="width: 100%; height: 100%"
  68. show-center-play-btn
  69. show-mute-btn="true"
  70. autoplay="true"
  71. auto-pause-if-navigate
  72. :custom-cache="false"
  73. :enable-progress-gesture="false"
  74. :poster="imgUrls[0]"
  75. @pause="videoPause"
  76. ></video>
  77. </view>
  78. <!-- #endif -->
  79. <view class="w-full flex-center absolute left-0 bottom-54rpx z-4" v-if="imgUrls.length > 1 && showDot">
  80. <block v-for="(_, index) in imgUrls" :key="index">
  81. <view class="dot_item h-4 rd-2rpx" :style="{ 'background-color': currents == index ? '#fff' : 'rgba(255,255,255,0.4)', width: dotWidth + 'rpx' }"></view>
  82. </block>
  83. </view>
  84. <view class="w-full flex-center abs-lb h-200 white-jianbian"></view>
  85. </view>
  86. </template>
  87. <script>
  88. export default {
  89. props: {
  90. imgUrls: {
  91. type: Array,
  92. default: () => []
  93. },
  94. videoline: {
  95. type: String,
  96. default: ''
  97. },
  98. showDot: {
  99. type: Number,
  100. default: 1
  101. },
  102. autoHeight: {
  103. type: Number,
  104. default: 0
  105. }
  106. },
  107. data() {
  108. return {
  109. indicatorDots: false,
  110. circular: true,
  111. autoplay: true,
  112. interval: 3000,
  113. duration: 500,
  114. currents: 0,
  115. controls: true,
  116. isPlay: true,
  117. videoContext: '',
  118. imgHeight:0
  119. };
  120. },
  121. mounted() {
  122. if (this.videoline) {
  123. this.imgUrls.shift();
  124. }
  125. // #ifndef APP-PLUS
  126. this.videoContext = uni.createVideoContext('myVideo', this);
  127. // #endif
  128. },
  129. computed: {
  130. dotWidth() {
  131. let windowWidth = uni.getSystemInfoSync().windowWidth;
  132. return (windowWidth * 2 - (40 + (this.imgUrls.length - 1) * 12)) / this.imgUrls.length;
  133. },
  134. swiperHeight() {
  135. let windowWidth = uni.getSystemInfoSync().windowWidth;
  136. let that = this;
  137. uni.getImageInfo({
  138. src: this.imgUrls[0],
  139. success: (image) => {
  140. that.imgHeight = image.height * windowWidth / image.width;
  141. }
  142. });
  143. if (this.autoHeight) {
  144. return {
  145. height: `${this.imgHeight}px`
  146. };
  147. } else {
  148. return {
  149. height: '750rpx'
  150. };
  151. }
  152. }
  153. },
  154. methods: {
  155. videoPause(e) {
  156. // #ifdef APP-PLUS
  157. this.isPlay = true;
  158. this.autoplay = true;
  159. // #endif
  160. },
  161. bindPause: function () {
  162. // #ifndef APP-PLUS
  163. this.videoContext.play();
  164. this.$set(this, 'controls', false);
  165. this.autoplay = false;
  166. // #endif
  167. // #ifdef APP-PLUS
  168. this.isPlay = false;
  169. this.videoContext = uni.createVideoContext('myVideo', this);
  170. this.videoContext.play();
  171. // #endif
  172. },
  173. change: function (e) {
  174. this.$set(this, 'currents', e.detail.current);
  175. }
  176. }
  177. };
  178. </script>
  179. <style scoped lang="scss">
  180. .item-box {
  181. width: 100%;
  182. height: 100%;
  183. }
  184. .product-bg {
  185. width: 100%;
  186. position: relative;
  187. }
  188. .product-bg swiper {
  189. width: 100%;
  190. height: 100%;
  191. position: relative;
  192. }
  193. .product-bg .slide-image {
  194. width: 100%;
  195. height: 100%;
  196. }
  197. .product-bg .pages {
  198. position: absolute;
  199. background-color: #fff;
  200. height: 34rpx;
  201. padding: 0 10rpx;
  202. border-radius: 3rpx;
  203. right: 30rpx;
  204. bottom: 30rpx;
  205. line-height: 34rpx;
  206. font-size: 24rpx;
  207. color: #050505;
  208. }
  209. #myVideo {
  210. width: 100%;
  211. height: 100%;
  212. }
  213. .product-bg .item {
  214. position: relative;
  215. width: 100%;
  216. height: 100%;
  217. }
  218. .product-bg .item .poster {
  219. position: absolute;
  220. top: 0;
  221. left: 0;
  222. height: 750rpx;
  223. width: 100%;
  224. z-index: 9;
  225. }
  226. .product-bg .item .poster .image {
  227. width: 100%;
  228. height: 100%;
  229. }
  230. .product-bg .item .stop {
  231. position: absolute;
  232. top: 50%;
  233. left: 50%;
  234. width: 136rpx;
  235. height: 136rpx;
  236. margin-top: -68rpx;
  237. margin-left: -68rpx;
  238. z-index: 9;
  239. }
  240. .product-bg .item .stop .image {
  241. width: 100%;
  242. height: 100%;
  243. }
  244. .dot_item ~ .dot_item {
  245. margin-left: 12rpx;
  246. }
  247. .white-jianbian {
  248. background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, rgba(0, 0, 0, 0.2) 100%);
  249. }
  250. </style>