Touchbox.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="site-box" ref="fixbox" :style="{ 'z-index':zIndex , 'height':popupHeight + 'px'}">
  3. <view class="tap-touch-box" :style="{height:touchHeight + 'px'}" @touchmove="getstart" @touchend="getend" >
  4. <view class="tap-touch-line"/>
  5. <slot name='touchSlot' />
  6. </view>
  7. <slot name='contentSlot' />
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'Touchbox',
  13. data() {
  14. return {
  15. windowHeight: 0,
  16. touchHeight: uni.upx2px(125),
  17. popupHeight: 0,
  18. initialHeight: 0,
  19. oldHeight: 0,
  20. scrollHeight: 0,
  21. minTop: 0,
  22. maxTop: 0,
  23. };
  24. },
  25. props: {
  26. zIndex: {
  27. type: Number,
  28. default: 99,
  29. },
  30. smallHeight: {
  31. type: Number,
  32. default: 0.35,
  33. },
  34. maxHeight: {
  35. type: Number,
  36. default: 0.65,
  37. },
  38. },
  39. created() {
  40. const { windowHeight } = uni.getSystemInfoSync();
  41. this.windowHeight = windowHeight;
  42. // this.windowWidth = uni.getSystemInfoSync().windowWidth;
  43. this.popupHeight = this.initialHeight = this.oldHeight = windowHeight * this.smallHeight;
  44. this.maxTop = windowHeight * (1 - this.smallHeight);
  45. this.minTop = windowHeight * (1 - this.maxHeight);
  46. // minTop:0,
  47. // maxTop:0,
  48. },
  49. mounted() {
  50. this.$nextTick(function() {
  51. // this.windowWidth = uni.getSystemInfoSync().windowWidth;
  52. // this.windowHeight = uni.getSystemInfoSync().windowHeight;
  53. // var defaultHeight = this.windowHeight * (1 - this.minHeight);
  54. // this.firsttop = defaultHeight;
  55. // this.phonetop =
  56. // (this.windowHeight * this.maxHeight - this.windowHeight * this.minHeight) / 2;
  57. // this.phoneMinTop =
  58. // (this.windowHeight * this.minHeight - this.windowHeight * this.smallHeight) / 2;
  59. // this.fixboxtop = defaultHeight;
  60. // this.$emit('currentHeight', this.windowHeight - this.fixboxtop);
  61. // this.$emit('maxtHeight', this.windowHeight * this.maxHeight);
  62. });
  63. },
  64. onReady() {},
  65. computed: {},
  66. watch: {
  67. popupHeight(newH, oldH) {
  68. this.scrollHeight = newH - this.touchHeight;
  69. },
  70. scrollHeight: {
  71. handler(newH) {
  72. this.$emit('currentHeight', newH);
  73. },
  74. immediate: true
  75. }
  76. },
  77. methods: {
  78. // 隐藏
  79. concealList() {
  80. // this.popupHeight = this.touchHeight
  81. this.popupHeight = this.initialHeight
  82. },
  83. getstart(e) {
  84. e.preventDefault();
  85. const { clientY, screenY } = e.touches[0];
  86. const Y = clientY || screenY;
  87. // console.log(Y, this.maxTop)
  88. // // 这里特殊处理 解决:在滑动框内如果存在滚动元素,则会出现滑动时滑动框和内部滚动同时滑的问题
  89. if (Y < this.minTop || Y > (this.windowHeight - this.touchHeight)) {
  90. return
  91. }
  92. this.popupHeight = this.windowHeight - Y;
  93. },
  94. getend() {
  95. if (this.popupHeight >= this.oldHeight && (this.popupHeight - this.oldHeight) > 50) {
  96. // 上拉
  97. this.popupHeight = this.windowHeight * this.maxHeight
  98. } else if ((this.oldHeight - this.popupHeight) > 50) {
  99. // console.log("this.oldHeight = ", this.oldHeight, "this.popupHeight = ", this.popupHeight)
  100. const s_h = this.windowHeight * this.smallHeight;
  101. // 下拉
  102. if (this.popupHeight >= s_h) {
  103. // 在中等偏上区域,还原成中等高度
  104. this.popupHeight = s_h
  105. } else {
  106. // 在中等偏下区域,还原成最小高度
  107. this.popupHeight = this.initialHeight
  108. // this.popupHeight = this.touchHeight
  109. }
  110. } else {
  111. // 拉取幅度不够,还原,防止误触发
  112. this.popupHeight = this.oldHeight
  113. };
  114. // 记录当前高度
  115. this.oldHeight = this.popupHeight;
  116. },
  117. },
  118. };
  119. </script>
  120. <style lang="scss" scoped>
  121. .site-box {
  122. position: fixed;
  123. left: 0;
  124. right: 0;
  125. bottom: 0;
  126. background-color: #ffffff;
  127. // padding: 0 12px;
  128. transition-property: top;
  129. transition-duration: 0.7s;
  130. border-radius: 40rpx 40rpx 0 0;
  131. .tap-touch-box {
  132. padding: 20rpx 0;
  133. .tap-touch-line {
  134. width: 100%;
  135. height: 8rpx;
  136. overflow: hidden;
  137. position: relative;
  138. &:before {
  139. content: '';
  140. position: absolute;
  141. left: 50%;
  142. top: 0;
  143. transform: translateX(-50%);
  144. width: 50rpx;
  145. height: 16rpx;
  146. border-radius: 8rpx;
  147. background-color: rgb(214, 215, 217);
  148. }
  149. }
  150. }
  151. // .tap-touch-line {
  152. // width: 100%;
  153. // height: 100rpx;
  154. // position: relative;
  155. // &:before {
  156. // content: '';
  157. // position: absolute;
  158. // left: 50%;
  159. // top: 50%;
  160. // transform: translate(-50%, -50%);
  161. // width: 30%;
  162. // height: 8rpx;
  163. // border-radius: 4rpx;
  164. // background-color: rgb(214, 215, 217);
  165. // }
  166. // }
  167. }
  168. // .tapBoxTouchLine {
  169. // display: flex;
  170. // align-items: center;
  171. // justify-content: center;
  172. // padding: 20rpx 0 30rpx;
  173. // }
  174. // .line {
  175. // margin: 0px;
  176. // vertical-align: middle;
  177. // // border-bottom: 8rpx solid rgb(214, 215, 217);
  178. // height: 8rpx;
  179. // background-color: rgb(214, 215, 217);
  180. // border-radius: 4rpx;
  181. // // border: 4rpx;
  182. // // border-top-color: rgb(214, 215, 217);
  183. // // border-right-color: rgb(214, 215, 217);
  184. // // border-left-color: rgb(214, 215, 217);
  185. // }
  186. // .fixedbox {
  187. // position: fixed;
  188. // left: 0;
  189. // background-color: #ffffff;
  190. // padding: 0 12px;
  191. // }
  192. // .fixedbox2 {
  193. // position: fixed;
  194. // left: 0;
  195. // background-color: #ffffff;
  196. // padding: 0 12px;
  197. // transition-property: top;
  198. // transition-duration: 0.4s;
  199. // }
  200. </style>