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. oldHeight: 0,
  16. scrollHeight: 0,
  17. minTop: 0,
  18. maxTop: 0,
  19. };
  20. },
  21. props: {
  22. zIndex: {
  23. type: Number,
  24. default: 99,
  25. },
  26. smallHeight: {
  27. type: Number,
  28. default: 0.35,
  29. },
  30. maxHeight: {
  31. type: Number,
  32. default: 0.65,
  33. },
  34. // minHeight: {
  35. // type: Number,
  36. // default: 0.2,
  37. // },
  38. },
  39. created() {
  40. const { windowHeight } = uni.getSystemInfoSync();
  41. this.windowHeight = windowHeight;
  42. // this.windowWidth = uni.getSystemInfoSync().windowWidth;
  43. this.popupHeight = 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. },
  82. getstart(e) {
  83. e.preventDefault();
  84. const { clientY, screenY } = e.touches[0];
  85. const Y = clientY || screenY;
  86. // console.log(Y, this.maxTop)
  87. // // 这里特殊处理 解决:在滑动框内如果存在滚动元素,则会出现滑动时滑动框和内部滚动同时滑的问题
  88. if (Y < this.minTop || Y > (this.windowHeight - this.touchHeight)) {
  89. return
  90. }
  91. this.popupHeight = this.windowHeight - Y;
  92. },
  93. getend() {
  94. if (this.popupHeight >= this.oldHeight && (this.popupHeight - this.oldHeight) > 50) {
  95. // 上拉
  96. this.popupHeight = this.windowHeight * this.maxHeight
  97. } else if ((this.oldHeight - this.popupHeight) > 50) {
  98. // console.log("this.oldHeight = ", this.oldHeight, "this.popupHeight = ", this.popupHeight)
  99. const s_h = this.windowHeight * this.smallHeight;
  100. // 下拉
  101. if (this.popupHeight >= s_h) {
  102. // 在中等偏上区域,还原成中等高度
  103. this.popupHeight = s_h
  104. } else {
  105. // 在中等偏下区域,还原成最小高度
  106. this.popupHeight = this.touchHeight
  107. }
  108. } else {
  109. // 拉取幅度不够,还原,防止误触发
  110. this.popupHeight = this.oldHeight
  111. };
  112. // 记录当前高度
  113. this.oldHeight = this.popupHeight;
  114. },
  115. },
  116. };
  117. </script>
  118. <style lang="scss" scoped>
  119. .site-box {
  120. position: fixed;
  121. left: 0;
  122. right: 0;
  123. bottom: 0;
  124. background-color: #ffffff;
  125. // padding: 0 12px;
  126. transition-property: top;
  127. transition-duration: 0.7s;
  128. border-radius: 40rpx 40rpx 0 0;
  129. .tap-touch-line {
  130. width: 100%;
  131. height: 100rpx;
  132. position: relative;
  133. &:before {
  134. content: '';
  135. position: absolute;
  136. left: 50%;
  137. top: 50%;
  138. transform: translate(-50%, -50%);
  139. width: 30%;
  140. height: 8rpx;
  141. border-radius: 4rpx;
  142. background-color: rgb(214, 215, 217);
  143. }
  144. }
  145. }
  146. // .tapBoxTouchLine {
  147. // display: flex;
  148. // align-items: center;
  149. // justify-content: center;
  150. // padding: 20rpx 0 30rpx;
  151. // }
  152. // .line {
  153. // margin: 0px;
  154. // vertical-align: middle;
  155. // // border-bottom: 8rpx solid rgb(214, 215, 217);
  156. // height: 8rpx;
  157. // background-color: rgb(214, 215, 217);
  158. // border-radius: 4rpx;
  159. // // border: 4rpx;
  160. // // border-top-color: rgb(214, 215, 217);
  161. // // border-right-color: rgb(214, 215, 217);
  162. // // border-left-color: rgb(214, 215, 217);
  163. // }
  164. // .fixedbox {
  165. // position: fixed;
  166. // left: 0;
  167. // background-color: #ffffff;
  168. // padding: 0 12px;
  169. // }
  170. // .fixedbox2 {
  171. // position: fixed;
  172. // left: 0;
  173. // background-color: #ffffff;
  174. // padding: 0 12px;
  175. // transition-property: top;
  176. // transition-duration: 0.4s;
  177. // }
  178. </style>