tm-swiper.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <template>
  2. <view class="tm-swiper " :class="[inline ? 'd-inline-block' : '']">
  3. <swiper :disable-touch="notouch" :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. notouch: false,
  127. circular: false,
  128. autoplay: false,
  129. interval: {
  130. type: Number,
  131. default: 3000
  132. },
  133. duration: {
  134. type: Number,
  135. default: 500
  136. },
  137. // 是否显示指示
  138. indicatorDots: false,
  139. //指示点的主题色。
  140. color: {
  141. type: String,
  142. default: 'primary'
  143. },
  144. // 当前位置。
  145. current: {
  146. type: Number,
  147. default: 0
  148. },
  149. ani3d: {
  150. type: Number,
  151. default: 0
  152. },
  153. // 指示点的类型。
  154. dotModel: {
  155. type: String,
  156. default: 'dot' //dot|number|rect
  157. },
  158. // 指示点的位置 。
  159. dotDirection: {
  160. type: String,
  161. default: 'center' //left|center|right
  162. },
  163. round: {
  164. type: Boolean | Number,
  165. default: 0
  166. },
  167. margin: {
  168. type: Number,
  169. default: 0
  170. },
  171. // 跟随主题色的改变而改变。
  172. fllowTheme: {
  173. type: Boolean | String,
  174. default: true
  175. }
  176. },
  177. watch: {
  178. current: function(newval) {
  179. if (newval >= this.dataList.length) {
  180. this.nowIndex = 0;
  181. return;
  182. }
  183. this.nowIndex = newval;
  184. },
  185. list: {
  186. deep: true,
  187. handler() {
  188. this.chulidata();
  189. }
  190. }
  191. },
  192. computed: {
  193. color_tmeme: function() {
  194. if (this.$tm.vx.state().tmVuetify.color !== null && this.$tm.vx.state().tmVuetify.color && this
  195. .fllowTheme) {
  196. return this.$tm.vx.state().tmVuetify.color;
  197. }
  198. return this.color;
  199. },
  200. round_num: function() {
  201. if (typeof this.round === 'boolean') return 0;
  202. return this.round;
  203. },
  204. nowIndex: {
  205. get: function() {
  206. return this.dotIndex;
  207. },
  208. set: function(val) {
  209. this.dotIndex = val;
  210. this.$emit('update:current', val);
  211. this.$emit('change', val);
  212. }
  213. },
  214. nowshowDot:function(){
  215. return this.showdot();
  216. },
  217. w_s: {
  218. get: function() {
  219. return this.w_w;
  220. },
  221. set: function(val) {
  222. this.w_w = val;
  223. }
  224. },
  225. h_s: {
  226. get: function() {
  227. return this.h_h;
  228. },
  229. set: function(val) {
  230. this.h_h = val;
  231. }
  232. },
  233. margin_px: function() {
  234. return this.margin * 2;
  235. },
  236. },
  237. data() {
  238. return {
  239. w_w: 0,
  240. h_h: 0,
  241. dataList: [],
  242. dotIndex: 0,
  243. isPlayVedio:false,
  244. };
  245. },
  246. async mounted() {
  247. this.nowIndex = this.current;
  248. let w = this.width;
  249. if (String(w).indexOf('px') > -1 && typeof w == 'string') {
  250. let p = parseInt(h);
  251. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  252. w = p * sy;
  253. }
  254. if (typeof w == 'string') {
  255. w = parseInt(w);
  256. }
  257. this.w_w = w;
  258. let h = this.height;
  259. if (String(h).indexOf('px') > -1 && typeof h == 'string') {
  260. let p = parseInt(h);
  261. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  262. h = p * sy;
  263. }
  264. if (typeof h == 'string') {
  265. h = parseInt(h);
  266. }
  267. this.h_h = h;
  268. this.$nextTick(async function() {
  269. await this.chulidata();
  270. });
  271. },
  272. methods: {
  273. showdot(){
  274. if(typeof this.dataList[this.nowIndex]=='string') return true;
  275. if(typeof this.dataList[this.nowIndex]=='object'){
  276. if(this.dataList[this.nowIndex].dtype=='video') return false;
  277. }
  278. return true;
  279. },
  280. async chulidata() {
  281. let t = this;
  282. let tb = await this.$Querey('.tm-swiper', this).catch(ev => {});
  283. //console.log(uni.getSystemInfoSync());
  284. let sy = 750 / uni.getSystemInfoSync().windowWidth;
  285. let upxw = tb[0].width * sy;
  286. let sw = this.w_s > 0 ? this.w_s : upxw > 0 ? upxw : 100;
  287. this.w_s = sw;
  288. let sh = this.h_s > 0 ? this.h_s : tb[0].height > 0 ? tb[0].height : 300;
  289. this.h_s = sh;
  290. let d = [];
  291. this.list.forEach(item => {
  292. if (typeof item === 'string') {
  293. d.push({
  294. width: sw,
  295. height: sh,
  296. url: item,
  297. title: '',
  298. dtype: '',
  299. data: item
  300. });
  301. } else if (typeof item === 'object') {
  302. d.push({
  303. width: sw,
  304. height: sh,
  305. url: item[t.rangKey],
  306. title: item['title'] || '',
  307. dtype: item['type'] || '',
  308. data: item
  309. });
  310. }
  311. });
  312. this.dataList = d;
  313. },
  314. imgload(e, index) {},
  315. change(event) {
  316. this.nowIndex = event.detail.current;
  317. },
  318. itemClick(e) {
  319. if (this.previmage) {
  320. uni.previewImage({
  321. current: e.url,
  322. urls: [e.url]
  323. });
  324. }
  325. this.$emit('click', e);
  326. }
  327. }
  328. };
  329. </script>
  330. <style></style>
  331. <style lang="scss" scoped>
  332. .tm-swiper {
  333. position: relative;
  334. .tm-swiper-ik {
  335. transition: all 1s;
  336. }
  337. .a3d {
  338. transform: scale(0.76);
  339. }
  340. .a3dL {
  341. transform: scale(0.76) translateX(-24rpx)
  342. }
  343. .a3dR {
  344. transform: scale(0.76) translateX(24rpx)
  345. }
  346. .actvieMarginDh {
  347. display: flex;
  348. justify-content: center;
  349. .actvieMarginDh-item {
  350. overflow: hidden;
  351. line-height: 0;
  352. }
  353. }
  354. .tm-swiper-dot {
  355. .tm-swiper-dot-item {
  356. width: 16upx;
  357. height: 16upx;
  358. border-radius: 50%;
  359. &.unactive {
  360. background-color: rgba(255, 255, 255, 0.6);
  361. }
  362. &.number {
  363. width: 34upx;
  364. height: 34upx;
  365. }
  366. &.rect {
  367. width: 24upx;
  368. height: 6upx;
  369. border-radius: 0;
  370. }
  371. &.round {
  372. background-color: rgba(0, 0, 0, 0.4);
  373. }
  374. }
  375. width: 100%;
  376. position: absolute;
  377. left: 0;
  378. display: flex;
  379. bottom: 20upx;
  380. &.center {
  381. display: flex;
  382. justify-content: center;
  383. }
  384. &.left {
  385. display: flex;
  386. justify-content: flex-start;
  387. padding-left: 20upx;
  388. width: calc(100% - 20upx);
  389. }
  390. &.right {
  391. display: flex;
  392. justify-content: flex-end;
  393. padding-right: 20upx;
  394. width: calc(100% - 20upx);
  395. }
  396. }
  397. }
  398. </style>