xt-swiperCell.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <view>
  3. <view
  4. @touchstart.stop="compTouchstart"
  5. @touchmove.stop="compTouchmove"
  6. @touchend.stop="compTouchend"
  7. :class="{ 'xt_swiper--container': true, 'xt_add-index': offsetWidth !== 0 }"
  8. :style="{ transform: `translate3d(${offsetWidth}px, 0, 0)` }"
  9. >
  10. <!-- 左边按钮 -->
  11. <view class="xt_swiper--btn-left-container">
  12. <slot name="right">
  13. <template v-if="left.length > 0">
  14. <view
  15. v-for="(leftBtn, index) in left"
  16. :key="index"
  17. class="xt_swiper--btn"
  18. >
  19. <view
  20. class="xt_swiper--btn-text"
  21. :style="{
  22. color: leftBtn.color || '#FFFFFF',
  23. 'background-color':
  24. leftBtn.backgroundColor || '#30a9de',
  25. ...leftBtn.style
  26. }"
  27. @click.stop="handleBtn(leftBtn.callback, index)"
  28. >
  29. {{ leftBtn.text }}
  30. </view>
  31. </view>
  32. </template>
  33. </slot>
  34. </view>
  35. <!-- 内容 -->
  36. <view class="xt_swiper--content">
  37. <slot name="default">
  38. <template v-if="content">
  39. <view class="xt_swiper--content-container">
  40. <view class="default_content">
  41. <view class="default_content-title" v-if="content.title">
  42. <text>{{ content.title }}</text>
  43. </view>
  44. <view class="default_content-desc" v-if="content.desc">
  45. <text>{{ content.desc }}</text>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. </slot>
  51. </view>
  52. <!-- 右边按钮 -->
  53. <view class="xt_swiper--btn-right-container">
  54. <slot name="right">
  55. <template v-if="right.length > 0">
  56. <view
  57. v-for="(rightBtn, index) in right"
  58. :key="index"
  59. class="xt_swiper--btn"
  60. >
  61. <view
  62. class="xt_swiper--btn-text"
  63. :style="{
  64. color: rightBtn.color || '#FFFFFF',
  65. 'background-color':
  66. rightBtn.backgroundColor || '#30a9de',
  67. ...rightBtn.style
  68. }"
  69. @click.stop="handleBtn(rightBtn.callback, index)"
  70. >
  71. {{ rightBtn.text }}
  72. </view>
  73. </view>
  74. </template>
  75. </slot>
  76. </view>
  77. </view>
  78. <view
  79. class="xt_swiper--mask"
  80. v-if="offsetWidth !== 0"
  81. @touchstart.stop="handleMask"
  82. ></view>
  83. </view>
  84. </template>
  85. <script>
  86. export default {
  87. name: 'xt-swiperCell',
  88. props: {
  89. /**
  90. * [{
  91. * text: String,
  92. * callback: (index: Number) => void|Promise
  93. * color: String
  94. * backgroundColor: String
  95. }]
  96. */
  97. content: {
  98. type: Object,
  99. default: () => {
  100. return null;
  101. }
  102. },
  103. right: {
  104. type: Array,
  105. default: () => []
  106. }, // 右边按钮
  107. left: {
  108. type: Array,
  109. default: () => []
  110. } // 右边按钮
  111. },
  112. data() {
  113. return {
  114. offsetWidth: 0, // 偏移量 -: 向左 +: 向右
  115. rightBtnWidth: 0, // 右边按钮的总宽度
  116. leftBtnWidth: 0, // 左边按钮的总宽度
  117. clickClientX: 0 // 开始触摸时X轴
  118. };
  119. },
  120. methods: {
  121. /**
  122. * 开始触摸
  123. */
  124. compTouchstart(event) {
  125. this.clickClientX = parseInt(event.changedTouches[0].clientX);
  126. },
  127. compTouchmove(event) {
  128. const move = parseInt(event.changedTouches[0].clientX - this.clickClientX);
  129. let res = 0;
  130. if (move < 0 && this.rightBtnWidth) {
  131. res = move > this.rightBtnWidth ? move : this.rightBtnWidth;
  132. } else if (move > 0 && this.leftBtnWidth) {
  133. res = move < this.leftBtnWidth ? move : this.leftBtnWidth;
  134. } else {
  135. res = 0;
  136. }
  137. this.offsetWidth = res;
  138. },
  139. /**
  140. * 结束触摸
  141. */
  142. compTouchend(event) {
  143. const endOffset = parseInt(
  144. event.changedTouches[0].clientX - this.clickClientX
  145. );
  146. let res = 0;
  147. if (endOffset < 0 && this.rightBtnWidth) {
  148. res = endOffset > this.rightBtnWidth / 2 ? 0 : this.rightBtnWidth;
  149. } else if (endOffset > 0 && this.leftBtnWidth) {
  150. res = endOffset < this.leftBtnWidth / 2 ? 0 : this.leftBtnWidth;
  151. } else {
  152. res = 0;
  153. }
  154. this.offsetWidth = res;
  155. },
  156. async handleBtn(callback, index) {
  157. let flag = true;
  158. if (!!callback) {
  159. if (
  160. (typeof callback === 'object' || typeof callback === 'function') &&
  161. typeof callback.then === 'function'
  162. ) {
  163. flag = await callback(index);
  164. } else {
  165. callback(index);
  166. flag = true;
  167. }
  168. } else {
  169. flag = true;
  170. }
  171. if (flag) {
  172. this.offsetWidth = 0;
  173. }
  174. },
  175. /**
  176. * 触摸蒙版回调
  177. */
  178. handleMask() {
  179. this.offsetWidth = 0;
  180. },
  181. /**
  182. * 获取按钮部分的总长度
  183. */
  184. getBtnWidth() {
  185. this.getElement('.xt_swiper--btn-right-container', elements => {
  186. this.rightBtnWidth = 0 - elements[0].width;
  187. });
  188. this.getElement('.xt_swiper--btn-left-container', elements => {
  189. this.leftBtnWidth = elements[0].width;
  190. });
  191. },
  192. /**
  193. * 获取元素
  194. */
  195. getElement(el, callback) {
  196. uni.createSelectorQuery()
  197. .in(this)
  198. .selectAll(el)
  199. .boundingClientRect()
  200. .exec(data => {
  201. callback(data[0]);
  202. });
  203. }
  204. },
  205. mounted() {
  206. this.getBtnWidth();
  207. }
  208. };
  209. </script>
  210. <style lang="scss">
  211. .xt_swiper--container {
  212. font-size: 28rpx;
  213. width: 100vw;
  214. min-height: 100rpx;
  215. white-space: nowrap;
  216. transition-duration: 0.6s;
  217. position: relative;
  218. background-color: #4CD964;
  219. border-bottom: solid 2upx #e70000;
  220. .xt_swiper--content {
  221. display: inline-block;
  222. width: 100vw;
  223. height: 100%;
  224. vertical-align: top;
  225. .xt_swiper--content-container {
  226. display: flex;
  227. padding: 0 10rpx;
  228. .default_content {
  229. padding-left: 20rpx;
  230. flex: 1;
  231. display: flex;
  232. flex-direction: column;
  233. justify-content: space-between;
  234. .default_content-title {
  235. font-size: 28rpx;
  236. height: 50rpx;
  237. line-height: 50rpx;
  238. color: #333333;
  239. }
  240. .default_content-desc {
  241. font-size: 24rpx;
  242. color: #999999;
  243. height: 50rpx;
  244. line-height: 50rpx;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. // 按钮
  251. .xt_swiper--btn-container {
  252. display: inline-block;
  253. height: 100%;
  254. // scroll-snap-align: end;
  255. }
  256. .xt_swiper--btn-left-container {
  257. @extend .xt_swiper--btn-container;
  258. position: absolute;
  259. left: 0;
  260. transform: translate3d(-100%, 0, 0);
  261. }
  262. .xt_swiper--btn-right-container {
  263. @extend .xt_swiper--btn-container;
  264. position: absolute;
  265. right: 0;
  266. transform: translate3d(100%, 0, 0);
  267. }
  268. // 按钮
  269. .xt_swiper--btn {
  270. display: inline-block;
  271. height: 100%;
  272. .xt_swiper--btn-text {
  273. font-size: 28rpx;
  274. display: flex;
  275. justify-content: center;
  276. align-items: center;
  277. padding: 0 30rpx;
  278. height: 100%;
  279. color: #ffffff;
  280. }
  281. }
  282. // 组件设置层级
  283. .xt_add-index {
  284. z-index: 100;
  285. }
  286. // 蒙版
  287. .xt_swiper--mask {
  288. position: fixed;
  289. left: 0;
  290. top: 0;
  291. width: 100vw;
  292. height: 100vh;
  293. z-index: 99;
  294. background-color: rgba(0, 0, 0, 0);
  295. }
  296. </style>