tn-swiper.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <view class="tn-swiper__wrap-class tn-swiper__wrap" :style="{borderRadius: `${radius}rpx`}">
  3. <!-- 轮播图 -->
  4. <swiper
  5. class="tn-swiper"
  6. :class="[backgroundColorClass]"
  7. :style="[swiperStyle]"
  8. :current="current"
  9. :interval="interval"
  10. :circular="circular"
  11. :autoplay="autoplay"
  12. :duration="duration"
  13. :previous-margin="effect3d ? effect3dPreviousSpacing + 'rpx' : '0'"
  14. :next-margin="effect3d ? effect3dPreviousSpacing + 'rpx' : '0'"
  15. @change="change"
  16. >
  17. <swiper-item
  18. v-for="(item, index) in list"
  19. :key="index"
  20. class="tn-swiper__item"
  21. >
  22. <view
  23. class="tn-swiper__item__image__wrap"
  24. :class="[swiperIndex !== index ? 'tn-swiper__item__image--scale' : '']"
  25. :style="{
  26. borderRadius: `${radius}rpx`,
  27. transform: effect3d && swiperIndex !== index ? 'scaleY(0.9)' : 'scaleY(1)',
  28. margin: effect3d && swiperIndex !== index ? '0 20rpx' : 0
  29. }"
  30. @click="click(index)"
  31. >
  32. <image class="tn-swiper__item__image" :src="item[name] || item" :mode="imageMode"></image>
  33. <view
  34. v-if="title && item[titleName]"
  35. class="tn-swiper__item__title tn-text-ellipsis"
  36. :style="[titleStyle]">
  37. {{ item[titleName] }}
  38. </view>
  39. </view>
  40. </swiper-item>
  41. </swiper>
  42. <!-- 指示点 -->
  43. <view class="tn-swiper__indicator" :style="[indicatorStyle]">
  44. <block v-if="mode === 'rect'">
  45. <view
  46. v-for="(item, index) in list"
  47. :key="index"
  48. class="tn-swiper__indicator__rect"
  49. :class="{'tn-swiper__indicator__rect--active': swiperIndex === index}"
  50. ></view>
  51. </block>
  52. <block v-if="mode === 'dot'">
  53. <view
  54. v-for="(item, index) in list"
  55. :key="index"
  56. class="tn-swiper__indicator__dot"
  57. :class="{'tn-swiper__indicator__dot--active': swiperIndex === index}"
  58. ></view>
  59. </block>
  60. <block v-if="mode === 'round'">
  61. <view
  62. v-for="(item, index) in list"
  63. :key="index"
  64. class="tn-swiper__indicator__round"
  65. :class="{'tn-swiper__indicator__round--active': swiperIndex === index}"
  66. ></view>
  67. </block>
  68. <block v-if="mode === 'number'">
  69. <view class="tn-swiper__indicator__number">{{ swiperIndex + 1 }}/{{ list.length }}</view>
  70. </block>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. export default {
  76. name: 'tn-swiper',
  77. props: {
  78. // 轮播图列表数据
  79. // [{image: xxx.jpg, title: 'xxxx'}]
  80. list: {
  81. type: Array,
  82. default() {
  83. return []
  84. }
  85. },
  86. // 初始化时,默认显示第几项
  87. current: {
  88. type: Number,
  89. default: 0
  90. },
  91. // 高度
  92. height: {
  93. type: Number,
  94. default: 250
  95. },
  96. // 背景颜色
  97. backgroundColor: {
  98. type: String,
  99. default: 'transparent'
  100. },
  101. // 图片的属性名
  102. name: {
  103. type: String,
  104. default: 'image'
  105. },
  106. // 是否显示标题
  107. title: {
  108. type: Boolean,
  109. default: false
  110. },
  111. // 标题的属性名
  112. titleName: {
  113. type: String,
  114. default: 'title'
  115. },
  116. // 用户自定义标题样式
  117. titleStyle: {
  118. type: Object,
  119. default() {
  120. return {}
  121. }
  122. },
  123. // 圆角的值
  124. radius: {
  125. type: Number,
  126. default: 8
  127. },
  128. // 指示器模式
  129. // rect -> 方形 round -> 圆角方形 dot -> 点 number -> 轮播图下标
  130. mode: {
  131. type: String,
  132. default: 'round'
  133. },
  134. // 指示器位置
  135. // topLeft \ topCenter \ topRight \ bottomLeft \ bottomCenter \ bottomRight
  136. indicatorPosition: {
  137. type: String,
  138. default: 'bottomCenter'
  139. },
  140. // 开启3D缩放效果
  141. effect3d: {
  142. type: Boolean,
  143. default: false
  144. },
  145. // 在3D缩放模式下,item之间的间隔
  146. effect3dPreviousSpacing: {
  147. type: Number,
  148. default: 50
  149. },
  150. // 自定播放
  151. autoplay: {
  152. type: Boolean,
  153. default: true
  154. },
  155. // 图片之间播放间隔多久
  156. interval: {
  157. type: Number,
  158. default: 3000
  159. },
  160. // 轮播间隔时间
  161. duration: {
  162. type: Number,
  163. default: 500
  164. },
  165. // 是否衔接滑动
  166. circular: {
  167. type: Boolean,
  168. default: true
  169. },
  170. // 图片裁剪模式
  171. imageMode: {
  172. type: String,
  173. default: 'aspectFill'
  174. }
  175. },
  176. computed: {
  177. backgroundColorStyle() {
  178. return this.$t.color.getBackgroundColorStyle(this.backgroundColor)
  179. },
  180. backgroundColorClass() {
  181. return this.$t.color.getBackgroundColorInternalClass(this.backgroundColor)
  182. },
  183. swiperStyle() {
  184. let style = {}
  185. if (this.backgroundColorStyle) {
  186. style.backgroundColor = this.backgroundColorStyle
  187. }
  188. if (this.height) {
  189. style.height = this.height + 'rpx'
  190. }
  191. return style
  192. },
  193. indicatorStyle() {
  194. let style = {}
  195. if (this.indicatorPosition === 'topLeft' || this.indicatorPosition === 'bottomLeft') style.justifyContent = 'flex-start'
  196. if (this.indicatorPosition === 'topCenter' || this.indicatorPosition === 'bottomCenter') style.justifyContent = 'center'
  197. if (this.indicatorPosition === 'topRight' || this.indicatorPosition === 'bottomRight') style.justifyContent = 'flex-end'
  198. if (['topLeft','topCenter','topRight'].indexOf(this.indicatorPosition) >= 0) {
  199. style.top = '12rpx'
  200. style.bottom = 'auto'
  201. } else {
  202. style.top = 'auto'
  203. style.bottom = '12rpx'
  204. }
  205. style.padding = `0 ${this.effect3d ? '74rpx' : '24rpx'}`
  206. return style
  207. },
  208. swiperTitleStyle() {
  209. let style = {}
  210. if (this.mode === 'none' || this.mode === '') style.paddingBottom = '12rpx'
  211. if (['bottomLeft','bottomCenter','bottomRight'].indexOf(this.indicatorPosition) >= 0 && this.mode === 'number') {
  212. style.paddingBottom = '60rpx'
  213. } else if (['bottomLeft','bottomCenter','bottomRight'].indexOf(this.indicatorPosition) >= 0 && this.mode !== 'number') {
  214. style.paddingBottom = '40rpx'
  215. } else {
  216. style.paddingBottom = '12rpx'
  217. }
  218. style = Object.assign(style, this.titleStyle)
  219. return style
  220. }
  221. },
  222. data() {
  223. return {
  224. // 当前显示的item的index
  225. swiperIndex: this.current
  226. }
  227. },
  228. watch: {
  229. list(newVal, oldVal) {
  230. // 如果修改了list的数据,重置current的值
  231. if (newVal.length !== oldVal.length) this.swiperIndex = 0
  232. },
  233. current(value) {
  234. // 监听外部current的变化,实时修改内部依赖于此测swiperIndex值,如果更新了current,而不是更新swiperIndex,就会错乱,因为指示器是依赖于swiperIndex的
  235. this.swiperIndex = value
  236. }
  237. },
  238. methods: {
  239. click(index) {
  240. this.$emit('click', index)
  241. },
  242. // 图片自动切换时触发
  243. change(event) {
  244. const current = event.detail.current
  245. this.swiperIndex = current
  246. this.$emit('change', current)
  247. }
  248. }
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .tn-swiper {
  253. &__wrap {
  254. position: relative;
  255. overflow: hidden;
  256. transform: translateY(0);
  257. }
  258. &__item {
  259. display: flex;
  260. flex-direction: row;
  261. align-items: center;
  262. overflow: hidden;
  263. &__image {
  264. width: 100%;
  265. height: 100%;
  266. will-change: transform;
  267. display: block;
  268. /* #ifdef H5 */
  269. pointer-events: none;
  270. /* #endif */
  271. &__wrap {
  272. width: 100%;
  273. height: 100%;
  274. flex: 1;
  275. transition: all 0.5s;
  276. overflow: hidden;
  277. box-sizing: content-box;
  278. position: relative;
  279. }
  280. &--scale {
  281. transform-origin: center center;
  282. }
  283. }
  284. &__title {
  285. width: 100%;
  286. position: absolute;
  287. background-color: rgba(0, 0, 0, 0.3);
  288. bottom: 0;
  289. left: 0;
  290. font-size: 28rpx;
  291. padding: 12rpx 24rpx;
  292. color: rgba(255, 255, 255, 0.8);
  293. }
  294. }
  295. &__indicator {
  296. padding: 0 24rpx;
  297. position: absolute;
  298. display: flex;
  299. flex-direction: row;
  300. width: 100%;
  301. z-index: 1;
  302. &__rect {
  303. width: 26rpx;
  304. height: 8rpx;
  305. background-color: rgba(0, 0, 0, 0.3);
  306. transition: all 0.5s;
  307. &--active {
  308. background-color: rgba(255, 255, 255, 0.8);
  309. }
  310. }
  311. &__dot {
  312. width: 14rpx;
  313. height: 14rpx;
  314. margin: 0 6rpx;
  315. border-radius: 20rpx;
  316. background-color: rgba(0, 0, 0, 0.3);
  317. transition: all 0.5s;
  318. &--active {
  319. background-color: rgba(255, 255, 255, 0.8);
  320. }
  321. }
  322. &__round {
  323. width: 14rpx;
  324. height: 14rpx;
  325. margin: 0 6rpx;
  326. border-radius: 20rpx;
  327. background-color: rgba(0, 0, 0, 0.3);
  328. transition: all 0.5s;
  329. &--active {
  330. width: 34rpx;
  331. background-color: rgba(255, 255, 255, 0.8);
  332. }
  333. }
  334. &__number {
  335. padding: 6rpx 16rpx;
  336. line-height: 1;
  337. background-color: rgba(0, 0, 0, 0.3);
  338. color: rgba(255, 255, 255, 0.8);
  339. border-radius: 100rpx;
  340. font-size: 26rpx;
  341. }
  342. }
  343. }
  344. </style>