tm-swiper.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <view class="tm-swiper " :class="[inline ? 'd-inline-block' : '']">
  3. <swiper :previous-margin="`${ani3d}rpx`" :next-margin="`${ani3d}rpx`" :style="{
  4. width: w_s + 'rpx',
  5. height: h_s + 'rpx'
  6. }" :vertical="vertical" :autoplay="autoplay&&!isPlayVedio" :circular="circular" :interval="interval" :duration="duration"
  7. :indicator-active-color="color_tmeme" :current="nowIndex" @change="change">
  8. <block v-for="(item, index) in dataList" :key="index">
  9. <swiper-item :style="{
  10. width: w_s + 'rpx',
  11. height: h_s + 'rpx'
  12. }" class="shadow-24" :class="[round_num > 0 ? ` round-${round_num} overflow` : '']">
  13. <view
  14. :class="[nowIndex !== index&&ani3d>0?'a3d':'',round_num > 0 ? ` round-${round_num} overflow` : '',nowIndex<index&&ani3d>0?'a3dL':'',nowIndex>index&&ani3d>0?'a3dR':'']"
  15. class="tm-swiper-ik fulled fulled-height" :key="index">
  16. <view class="actvieMarginDh">
  17. <view class="actvieMarginDh-item"
  18. :class="[round_num > 0 ? ` round-${round_num} overflow` : '']">
  19. <image v-if="item.dtype!='video'" @click="itemClick(item)" :style="{
  20. height: h_s + 'rpx',
  21. width: (w_s - margin_px) + 'rpx'
  22. }" @load="imgload($event, index)" :src="item.url"
  23. :class="[round_num > 0 ? ` round-${round_num} overflow` : '']"></image>
  24. <video @play="isPlayVedio = true" @pause="isPlayVedio=false" @ended="isPlayVedio=false" :autoplay="nowIndex == index?true:false" :style="{
  25. height: h_s + 'rpx',
  26. width: (w_s - margin_px) + 'rpx'
  27. }" v-if="item.dtype=='video'" :src="item.url"></video>
  28. <view v-if="item.title" :style="{
  29. width: (w_s - margin_px) + 'rpx'
  30. }" class="flex-start relative ">
  31. <view
  32. class="text-size-s px-16 absolute b-0 l-0 zIndex-14 flex-start text-overflow text-white"
  33. :style="{
  34. height: '66rpx',
  35. width: (w_s - margin_px-32) + 'rpx',
  36. lineHeight:'66rpx',
  37. background:'rgba(0,0,0,0.4)'
  38. }">
  39. {{item.title}}
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </swiper-item>
  46. </block>
  47. </swiper>
  48. <view class="tm-swiper-dot" v-if="indicatorDots&&nowshowDot" :class="[dotDirection]">
  49. <block v-if="dotModel != 'round'">
  50. <view @click="nowIndex = index" v-for="(item, index) in dataList" :key="index"
  51. class="tm-swiper-dot-item flex-center mx-8"
  52. :class="[nowIndex == index ? color_tmeme : 'unactive', dotModel]">
  53. <text v-if="dotModel === 'number'" class="text-size-xs">{{ index + 1 }}</text>
  54. </view>
  55. </block>
  56. <block v-if="dotModel == 'round'">
  57. <view :class="[dotModel]"
  58. class="text-size-xs balck text-white round-24 px-24 tm-swiper-dot-item flex-center flex-shrink"
  59. :style="{ height: '40rpx', minWidth: '60rpx' }">
  60. {{ nowIndex + 1 + '/' + dataList.length }}
  61. </view>
  62. </block>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. /**
  68. * 图片轮播
  69. * @description 很参数与原官方相同。
  70. * @property {Number} width = [] 默认:0,轮播宽度,0为自己计算。
  71. * @property {Number} height = [] 默认:0,轮播高度,0为自己计算。
  72. * @property {Array} list = [] 默认:[],图片列表,可以是string数组或者object数组。
  73. * @property {String} rang-key = [] 默认:[],图片列表object数组时,需要提供图片地址 的键值。
  74. * @property {Boolean} inline = [] 默认:false,是否内联,方便一排放多个轮播组件。
  75. * @property {Boolean} previmage = [] 默认:true,是否预览图片,点击图片时,放大预览。
  76. * @property {Boolean} vertical = [] 默认:false,是否竖向滚动。
  77. * @property {Boolean} circular = [] 默认:false,是否采用衔接滑动,即播放到末尾后重新回到开头
  78. * @property {Boolean} autoplay = [] 默认:false,是否自动切换
  79. * @property {Number} interval = [] 默认:3000,自动切换时间间隔
  80. * @property {Number} duration = [] 默认:500,滑动动画时长
  81. * @property {Number} ani3d = [] 默认:0,开启3d缩放动画
  82. * @property {Boolean} indicator-dots = [] 默认:true,是否显示指示
  83. * @property {String} color = [] 默认:primary,指示点的主题色。
  84. * @property {Number} current = [] 默认:0,如果需要双向绑定请使用current.sync.
  85. * @property {String} dot-model = [dot|number|rect|round] 默认:dot,指示点的类型。
  86. * @property {String} dot-direction = [left|center|right] 默认:center,指示点的位置。
  87. * @property {Number} round = [] 默认:0,圆角,单位rpx
  88. * @property {Number} margin = [] 默认:0,单位rpx
  89. * @property {Function} change 轮播切换时触发。参数返回当前播放位置。
  90. * @property {Function} click 轮播项目被点击时触发发,返回项目数据
  91. * @example <tm-swiper :list="['https://picsum.photos/300?jv=3','https://picsum.photos/300?jv=3']" ></tm-swiper>
  92. */
  93. export default {
  94. name: 'tm-swiper',
  95. props: {
  96. width: {
  97. type: Number | String,
  98. default: 0
  99. },
  100. // 自定高度。
  101. height: {
  102. type: Number | String,
  103. default: 0
  104. },
  105. list: {
  106. type: Array,
  107. default: () => {
  108. return [];
  109. }
  110. },
  111. rangKey: {
  112. type: String,
  113. default: 'src'
  114. },
  115. // 是否内联。
  116. inline: {
  117. type: Boolean,
  118. default: false
  119. },
  120. // 是否开启预览模式,即点击图片可以预览。
  121. previmage: {
  122. type: Boolean | String,
  123. default: true
  124. },
  125. vertical: false,
  126. circular: false,
  127. autoplay: false,
  128. interval: {
  129. type: Number,
  130. default: 3000
  131. },
  132. duration: {
  133. type: Number,
  134. default: 500
  135. },
  136. // 是否显示指示
  137. indicatorDots: false,
  138. //指示点的主题色。
  139. color: {
  140. type: String,
  141. default: 'primary'
  142. },
  143. // 当前位置。
  144. current: {
  145. type: Number,
  146. default: 0
  147. },
  148. ani3d: {
  149. type: Number,
  150. default: 0
  151. },
  152. // 指示点的类型。
  153. dotModel: {
  154. type: String,
  155. default: 'dot' //dot|number|rect
  156. },
  157. // 指示点的位置 。
  158. dotDirection: {
  159. type: String,
  160. default: 'center' //left|center|right
  161. },
  162. round: {
  163. type: Boolean | Number,
  164. default: 0
  165. },
  166. margin: {
  167. type: Number,
  168. default: 0
  169. },
  170. // 跟随主题色的改变而改变。
  171. fllowTheme: {
  172. type: Boolean | String,
  173. default: true
  174. }
  175. },
  176. watch: {
  177. current: function(newval) {
  178. if (newval >= this.dataList.length) {
  179. this.nowIndex = 0;
  180. return;
  181. }
  182. this.nowIndex = newval;
  183. },
  184. list: {
  185. deep: true,
  186. handler() {
  187. this.chulidata();
  188. }
  189. }
  190. },
  191. computed: {
  192. color_tmeme: function() {
  193. if (this.$tm.vx.state().tmVuetify.color !== null && this.$tm.vx.state().tmVuetify.color && this
  194. .fllowTheme) {
  195. return this.$tm.vx.state().tmVuetify.color;
  196. }
  197. return this.color;
  198. },
  199. round_num: function() {
  200. if (typeof this.round === 'boolean') return 0;
  201. return this.round;
  202. },
  203. nowIndex: {
  204. get: function() {
  205. return this.dotIndex;
  206. },
  207. set: function(val) {
  208. this.dotIndex = val;
  209. this.$emit('update:current', val);
  210. this.$emit('change', val);
  211. }
  212. },
  213. nowshowDot:function(){
  214. return this.showdot();
  215. },
  216. w_s: {
  217. get: function() {
  218. return this.w_w;
  219. },
  220. set: function(val) {
  221. this.w_w = val;
  222. }
  223. },
  224. h_s: {
  225. get: function() {
  226. return this.h_h;
  227. },
  228. set: function(val) {
  229. this.h_h = val;
  230. }
  231. },
  232. margin_px: function() {
  233. return this.margin * 2;
  234. },
  235. },
  236. data() {
  237. return {
  238. w_w: 0,
  239. h_h: 0,
  240. dataList: [],
  241. dotIndex: 0,
  242. isPlayVedio:false,
  243. };
  244. },
  245. async mounted() {
  246. this.nowIndex = this.current;
  247. let w = this.width;
  248. if (String(w).indexOf('px') > -1 && typeof w == 'string') {
  249. let p = parseInt(h);
  250. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  251. w = p * sy;
  252. }
  253. if (typeof w == 'string') {
  254. w = parseInt(w);
  255. }
  256. this.w_w = w;
  257. let h = this.height;
  258. if (String(h).indexOf('px') > -1 && typeof h == 'string') {
  259. let p = parseInt(h);
  260. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  261. h = p * sy;
  262. }
  263. if (typeof h == 'string') {
  264. h = parseInt(h);
  265. }
  266. this.h_h = h;
  267. this.$nextTick(async function() {
  268. await this.chulidata();
  269. });
  270. },
  271. methods: {
  272. showdot(){
  273. if(typeof this.dataList[this.nowIndex]=='string') return true;
  274. if(typeof this.dataList[this.nowIndex]=='object'){
  275. if(this.dataList[this.nowIndex].dtype=='video') return false;
  276. }
  277. return true;
  278. },
  279. async chulidata() {
  280. let t = this;
  281. let tb = await this.$Querey('.tm-swiper', this).catch(ev => {});
  282. //console.log(uni.getSystemInfoSync());
  283. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  284. let upxw = tb[0].width * sy;
  285. let sw = this.w_s > 0 ? this.w_s : upxw > 0 ? upxw : 100;
  286. this.w_s = sw;
  287. let sh = this.h_s > 0 ? this.h_s : tb[0].height > 0 ? tb[0].height : 300;
  288. this.h_s = sh;
  289. let d = [];
  290. this.list.forEach(item => {
  291. if (typeof item === 'string') {
  292. d.push({
  293. width: sw,
  294. height: sh,
  295. url: item,
  296. title: '',
  297. dtype: '',
  298. data: item
  299. });
  300. } else if (typeof item === 'object') {
  301. d.push({
  302. width: sw,
  303. height: sh,
  304. url: item[t.rangKey],
  305. title: item['title'] || '',
  306. dtype: item['type'] || '',
  307. data: item
  308. });
  309. }
  310. });
  311. this.dataList = d;
  312. },
  313. imgload(e, index) {},
  314. change(event) {
  315. this.nowIndex = event.detail.current;
  316. },
  317. itemClick(e) {
  318. if (this.previmage) {
  319. uni.previewImage({
  320. current: e.url,
  321. urls: [e.url]
  322. });
  323. }
  324. this.$emit('click', e);
  325. }
  326. }
  327. };
  328. </script>
  329. <style></style>
  330. <style lang="scss" scoped>
  331. .tm-swiper {
  332. position: relative;
  333. .tm-swiper-ik {
  334. transition: all 1s;
  335. }
  336. .a3d {
  337. transform: scale(0.76);
  338. }
  339. .a3dL {
  340. transform: scale(0.76) translateX(-24rpx)
  341. }
  342. .a3dR {
  343. transform: scale(0.76) translateX(24rpx)
  344. }
  345. .actvieMarginDh {
  346. display: flex;
  347. justify-content: center;
  348. .actvieMarginDh-item {
  349. overflow: hidden;
  350. line-height: 0;
  351. }
  352. }
  353. .tm-swiper-dot {
  354. .tm-swiper-dot-item {
  355. width: 16upx;
  356. height: 16upx;
  357. border-radius: 50%;
  358. &.unactive {
  359. background-color: rgba(255, 255, 255, 0.6);
  360. }
  361. &.number {
  362. width: 34upx;
  363. height: 34upx;
  364. }
  365. &.rect {
  366. width: 24upx;
  367. height: 6upx;
  368. border-radius: 0;
  369. }
  370. &.round {
  371. background-color: rgba(0, 0, 0, 0.4);
  372. }
  373. }
  374. width: 100%;
  375. position: absolute;
  376. left: 0;
  377. display: flex;
  378. bottom: 20upx;
  379. &.center {
  380. display: flex;
  381. justify-content: center;
  382. }
  383. &.left {
  384. display: flex;
  385. justify-content: flex-start;
  386. padding-left: 20upx;
  387. width: calc(100% - 20upx);
  388. }
  389. &.right {
  390. display: flex;
  391. justify-content: flex-end;
  392. padding-right: 20upx;
  393. width: calc(100% - 20upx);
  394. }
  395. }
  396. }
  397. </style>