z-swiper-item.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <!-- #ifndef MP-WEIXIN || MP-QQ -->
  3. <view :class="['swiper-slide',slideClass]" :style="[itemStyle,customStyle]" @click.stop="onClickSlide">
  4. <!-- #endif -->
  5. <!-- #ifdef MP-WEIXIN || MP-QQ -->
  6. <view :class="['swiper-slide',slideClass]" :style="[itemStyle,customStyle]" @click.stop="onClickSlide"
  7. :swiperItemTransform="wxsItemTransform" :change:swiperItemTransform="zSwiperWxs.wxsItemTransformObserver">
  8. <!-- #endif -->
  9. <slot></slot>
  10. </view>
  11. </template>
  12. <!-- #ifdef MP-WEIXIN || MP-QQ -->
  13. <script src="../../wxs/z-swiper-wxs.wxs" module="zSwiperWxs" lang="wxs"></script>
  14. <!-- #endif -->
  15. <script>
  16. import {
  17. ChildrenMixin
  18. } from '../../libs/mixins/relation.js';
  19. import {
  20. getRect
  21. } from '../../libs/utils/utils.js';
  22. export default {
  23. name: "z-swipe-item",
  24. // #ifdef MP-WEIXIN
  25. options: {
  26. virtualHost: true
  27. },
  28. // #endif
  29. mixins: [ChildrenMixin('zSwipe')],
  30. props: {
  31. customStyle: {
  32. type: Object,
  33. default: () => {
  34. return {};
  35. }
  36. },
  37. swiperItemWidth: {
  38. type: [String, Number],
  39. default: 0
  40. },
  41. swiperItemHeight: {
  42. type: [String, Number],
  43. default: 0
  44. },
  45. },
  46. data() {
  47. return {
  48. wxsItemTransform: "",
  49. itemStyle: {},
  50. offsetLeft: 0,
  51. offsetTop: 0,
  52. itemClass: [],
  53. width: 0,
  54. height: 0,
  55. };
  56. },
  57. mounted() {
  58. this.getSize();
  59. },
  60. computed: {
  61. slideClass() {
  62. return this.itemClass.join(" ");
  63. }
  64. },
  65. watch: {
  66. swiperItemWidth: {
  67. handler(val) {
  68. if (val) {
  69. this.$set(this.itemStyle, 'width', this.unitFormat(val, "rpx"))
  70. }
  71. },
  72. immediate: true
  73. },
  74. swiperItemHeight: {
  75. handler(val) {
  76. if (val) {
  77. this.$set(this.itemStyle, 'height', this.unitFormat(val, "rpx"))
  78. }
  79. },
  80. immediate: true
  81. }
  82. },
  83. methods: {
  84. unitFormat(val, type) {
  85. if (type == "rpx") {
  86. if (val.includes("rpx") || val.includes("px")) {
  87. return val;
  88. } else {
  89. return val + 'px';
  90. }
  91. }
  92. if (type == "number") {
  93. if (val.includes("rpx")) {
  94. return uni.upx2px(parseInt(val.replace("rpx", "")))
  95. } else if (!val.includes("rpx") && val.includes("px")) {
  96. return parseInt(val.replace("px", ""))
  97. } else {
  98. return val;
  99. }
  100. }
  101. },
  102. onClickSlide(event) {
  103. this.$emit("click", {
  104. event,
  105. index: this.index
  106. });
  107. this.parent.swiper.updateClickedSlide(this.index);
  108. this.parent.swiper.emit("slideClick", this.index);
  109. },
  110. transform(value) {
  111. // #ifndef MP-WEIXIN || MP-QQ
  112. this.$set(this.itemStyle, 'transform', value)
  113. // #endif
  114. // #ifdef MP-WEIXIN || MP-QQ
  115. this.wxsItemTransform = value
  116. // #endif
  117. },
  118. transition(value) {
  119. // #ifdef MP-BAIDU
  120. this.$set(this.itemStyle, 'transitionDuration', `${value}ms`)
  121. // #endif
  122. // #ifndef MP-BAIDU
  123. this.$set(this.itemStyle, 'transition-duration', `${value}ms`)
  124. // #endif
  125. },
  126. willChange(value) {
  127. this.$set(this.itemStyle, 'will-change', value)
  128. },
  129. css(value) {
  130. Object.keys(value).forEach((item) => {
  131. this.$set(this.itemStyle, item, value[item])
  132. })
  133. },
  134. transitionEnd(callback, duration) {
  135. setTimeout(callback, duration)
  136. },
  137. getSize() {
  138. const query = uni.createSelectorQuery().in(this);
  139. return new Promise((resolve, reject) => {
  140. query.select('.swiper-slide').boundingClientRect(data => {
  141. if (this.swiperItemWidth) {
  142. this.width = this.unitFormat(this.swiperItemWidth, "number");
  143. this.height = data.height;
  144. }
  145. if (this.swiperItemHeight) {
  146. this.width = data.width;
  147. this.height = this.unitFormat(this.swiperItemHeight, "number");
  148. }
  149. if (!this.swiperItemWidth && !this.swiperItemHeight) {
  150. this.width = data.width;
  151. this.height = data.height;
  152. }
  153. resolve({
  154. width: this.width,
  155. height: this.height
  156. })
  157. }).exec();
  158. })
  159. },
  160. addClass(value) {
  161. this.itemClass = Array.from(new Set([...this.itemClass, ...value.split(" ")]));
  162. },
  163. removeClass(value) {
  164. this.itemClass = this.itemClass.filter(item => !value.split(" ").includes(item));
  165. },
  166. hasClass(value) {
  167. return this.itemClass.includes(value);
  168. },
  169. nextAll() {
  170. return this.parent.children.filter((item) => {
  171. return item.index > this.index
  172. })
  173. },
  174. prevAll() {
  175. return this.parent.children.filter((item) => {
  176. return item.index < this.index
  177. }).reverse()
  178. },
  179. }
  180. }
  181. </script>
  182. <style scoped lang="scss">
  183. @import '../../libs/core.scss';
  184. </style>