鸿宇研学生前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.5 KiB

  1. <template>
  2. <view class="product" @touchstart="onTouchstart" @touchmove="onTouchmove" @touchend="onTouchend">
  3. <image class="product-img" :src="data.image" mode="aspectFill"></image>
  4. <view class="flex flex-column product-info">
  5. <view class="product-info-top">
  6. <view class="product-name text-ellipsis-2">{{ data.name }}</view>
  7. <view class="product-desc text-ellipsis">{{ data.desc }}</view>
  8. </view>
  9. <view class="flex product-info-bottom">
  10. <view class="product-detail">
  11. <view class="flex product-price">
  12. <view class="product-price-val">
  13. <text>¥</text>
  14. <text class="highlight">{{ priceInt }}</text>
  15. <text>{{ `${priceFrac}` }}</text>
  16. </view>
  17. <view class="product-price-bef" v-if="data.originalPrice">
  18. {{ `¥${data.originalPrice}` }}
  19. </view>
  20. </view>
  21. <view class="product-registered">
  22. {{ `${data.registered}人已报名` }}
  23. </view>
  24. </view>
  25. <button class="btn">报名</button>
  26. </view>
  27. </view>
  28. <button class="flex btn-collect"
  29. :style="collectBtnStyle"
  30. @click.stop="onCollect"
  31. @touchstart.stop="() => {}"
  32. >
  33. <view>收藏</view>
  34. </button>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. props: {
  40. data: {
  41. type: Object,
  42. default() {
  43. return {}
  44. }
  45. }
  46. },
  47. data() {
  48. return {
  49. isMove: false,
  50. startClientX: null,
  51. displayX: 0,
  52. }
  53. },
  54. computed: {
  55. priceInt() {
  56. return parseInt(this.data.currentPrice)
  57. },
  58. priceFrac() {
  59. return (this.data.currentPrice % this.priceInt).toFixed(2).slice(1)
  60. },
  61. collectBtnStyle() {
  62. let display = Math.ceil(this.displayX / 56 * 100)
  63. display > 100 && (display = 100)
  64. const translateX = 100 - display
  65. return `transform: translateX(${translateX}%);`
  66. }
  67. },
  68. methods: {
  69. onTouchstart(e) {
  70. const clientX = e.changedTouches[0].clientX
  71. this.isMove = false
  72. this.startClientX = clientX
  73. this.displayX = 0
  74. },
  75. onTouchmove(e) {
  76. const clientX = e.changedTouches[0].clientX
  77. if (clientX < this.startClientX) {
  78. this.displayX = this.startClientX - clientX
  79. } else {
  80. this.displayX = 0
  81. }
  82. this.isMove = true
  83. },
  84. onTouchend() {
  85. if (this.displayX < 100) {
  86. this.displayX = 0
  87. }
  88. this.isMove = false
  89. },
  90. showCollectBtn() {
  91. this.displayX = 100
  92. },
  93. hiddenCollectBtn() {
  94. this.displayX = 0
  95. },
  96. onCollect() {
  97. console.log('onCollect')
  98. // todo: fetch collect
  99. uni.showToast({
  100. icon: 'success',
  101. title: '已收藏',
  102. });
  103. this.hiddenCollectBtn()
  104. }
  105. },
  106. }
  107. </script>
  108. <style scoped lang="scss">
  109. .product {
  110. position: relative;
  111. height: 464rpx;
  112. background: #FFFFFF;
  113. border: 2rpx solid #FFFFFF;
  114. border-radius: 32rpx;
  115. overflow: hidden;
  116. font-size: 0;
  117. &-img {
  118. width: 100%;
  119. height: 220rpx;
  120. }
  121. &-info {
  122. height: 244rpx;
  123. padding: 16rpx 16rpx 24rpx 16rpx;
  124. box-sizing: border-box;
  125. justify-content: space-between;
  126. &-top {
  127. }
  128. &-bottom {
  129. width: 100%;
  130. justify-content: space-between;
  131. }
  132. }
  133. &-name {
  134. font-size: 28rpx;
  135. font-weight: 500;
  136. color: #000000;
  137. }
  138. &-desc {
  139. margin-top: 8rpx;
  140. font-size: 24rpx;
  141. color: #8B8B8B;
  142. }
  143. &-detail {
  144. }
  145. &-price {
  146. justify-content: flex-start;
  147. align-items: baseline;
  148. column-gap: 6rpx;
  149. &-val {
  150. font-size: 24rpx;
  151. font-weight: 500;
  152. color: #FF4800;
  153. .highlight {
  154. font-size: 32rpx;
  155. }
  156. }
  157. &-bef {
  158. text-decoration: line-through;
  159. font-size: 24rpx;
  160. color: #8B8B8B;
  161. }
  162. }
  163. &-registered {
  164. font-size: 24rpx;
  165. color: #8B8B8B;
  166. }
  167. .btn {
  168. padding: 11rpx 16rpx;
  169. font-size: 26rpx;
  170. font-weight: 500;
  171. color: #FFFFFF;
  172. background: #00A9FF;
  173. border-radius: 24rpx;
  174. }
  175. }
  176. .btn-collect {
  177. position: absolute;
  178. top: 0;
  179. right: 0;
  180. row-gap: 8rpx;
  181. width: 112rpx;
  182. height: 100%;
  183. font-size: 24rpx;
  184. line-height: 1;
  185. color: #FFFFFF;
  186. background: #FF9035;
  187. }
  188. </style>