Touchbox.vue 4.7 KB

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