su-fixed.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="ui-fixed">
  3. <view
  4. class="ui-fixed-box"
  5. :id="`fixed-${uuid}`"
  6. :class="[{ fixed: state.fixed }]"
  7. :style="[
  8. {
  9. left: sticky ? 'auto' : '0px',
  10. top: state.fixed && !bottom ? (noNav ? val : val + sys_navBar) + 'px' : 'auto',
  11. bottom: insetHeight,
  12. zIndex: index + sheep.$zIndex.navbar,
  13. },
  14. !alway ? { opacity: state.opacityVal } : '',
  15. ]"
  16. >
  17. <view
  18. class="ui-fixed-content"
  19. @tap="toTop"
  20. :style="[{ zIndex: index + sheep.$zIndex.navbar }]"
  21. >
  22. <slot></slot>
  23. <view
  24. v-if="safeAreaInsets.bottom && bottom && isInset"
  25. class="inset-bottom"
  26. :style="[{ height: safeAreaInsets.bottom + 'px' }]"
  27. ></view>
  28. </view>
  29. <view class="ui-fixed-bottom" :class="[bg]" v-if="bottom"></view>
  30. <view
  31. class="ui-fixed-bg"
  32. :class="[ui, bg]"
  33. :style="[
  34. { zIndex: index + sheep.$zIndex.navbar - 1 },
  35. bgStyles,
  36. opacity ? { opacity: state.opacityVal } : '',
  37. ]"
  38. ></view>
  39. </view>
  40. <view
  41. class="skeleton"
  42. :style="[{ height: state.content.height + 'px', width: width + 'px' }]"
  43. v-if="sticky ? state.fixed : placeholder && state.fixed"
  44. ></view>
  45. </view>
  46. </template>
  47. <script setup>
  48. import { onPageScroll } from '@dcloudio/uni-app';
  49. import { getCurrentInstance, unref, onMounted, reactive, nextTick, computed } from 'vue';
  50. import sheep from '@/sheep';
  51. const { safeAreaInsets } = sheep.$platform.device;
  52. const vm = getCurrentInstance();
  53. const uuid = sheep.$helper.guid();
  54. const sys_navBar = sheep.$platform.navbar;
  55. const state = reactive({
  56. content: {},
  57. fixed: true,
  58. scrollTop: 0,
  59. opacityVal: 0,
  60. });
  61. const insetHeight = computed(() => {
  62. if (state.fixed && props.bottom) {
  63. if (props.isInset) {
  64. return props.val + 'px';
  65. } else {
  66. return props.val + safeAreaInsets.bottom + 'px';
  67. }
  68. } else {
  69. return 'auto';
  70. }
  71. });
  72. const props = defineProps({
  73. noNav: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. bottom: {
  78. type: Boolean,
  79. default: false,
  80. },
  81. bg: {
  82. type: String,
  83. default: '',
  84. },
  85. bgStyles: {
  86. type: Object,
  87. default() {},
  88. },
  89. val: {
  90. type: Number,
  91. default: 0,
  92. },
  93. width: {
  94. type: [String, Number],
  95. default: 0,
  96. },
  97. alway: {
  98. type: Boolean,
  99. default: true,
  100. },
  101. opacity: {
  102. type: Boolean,
  103. default: false,
  104. },
  105. index: {
  106. type: [Number, String],
  107. default: 0,
  108. },
  109. placeholder: {
  110. type: [Boolean],
  111. default: false,
  112. },
  113. sticky: {
  114. type: [Boolean],
  115. default: false,
  116. },
  117. noFixed: {
  118. type: Boolean,
  119. default: false,
  120. },
  121. ui: {
  122. type: String,
  123. default: '',
  124. },
  125. clickTo: {
  126. type: Boolean,
  127. default: false,
  128. },
  129. //是否需要安全区
  130. isInset: {
  131. type: Boolean,
  132. default: true,
  133. },
  134. });
  135. state.fixed = !unref(props.sticky);
  136. onPageScroll((e) => {
  137. let top = e.scrollTop;
  138. state.scrollTop = top;
  139. state.opacityVal = top > sheep.$platform.navbar ? 1 : top * 0.01;
  140. });
  141. onMounted(() => {
  142. nextTick(() => {
  143. computedQuery();
  144. });
  145. });
  146. const computedQuery = () => {
  147. uni.createSelectorQuery()
  148. .in(vm)
  149. .select(`#fixed-${uuid}`)
  150. .boundingClientRect((data) => {
  151. if (data != null) {
  152. state.content = data;
  153. if (unref(props.sticky)) {
  154. setFixed(state.scrollTop);
  155. }
  156. }
  157. })
  158. .exec();
  159. };
  160. const setFixed = (value) => {
  161. if (unref(props.bottom)) {
  162. state.fixed =
  163. value >=
  164. state.content.bottom -
  165. sheep.$platform.device.windowHeight +
  166. state.content.height +
  167. unref(props.val);
  168. } else {
  169. state.fixed =
  170. value >=
  171. state.content.top -
  172. (unref(props.noNav) ? unref(props.val) : unref(props.val) + sheep.$platform.navbar);
  173. }
  174. };
  175. const toTop = () => {
  176. if (props.hasToTop) {
  177. uni.pageScrollTo({
  178. scrollTop: state.content.top,
  179. duration: 100,
  180. });
  181. }
  182. };
  183. </script>
  184. <style lang="scss">
  185. .ui-fixed {
  186. .ui-fixed-box {
  187. position: relative;
  188. width: 100%;
  189. &.fixed {
  190. position: fixed;
  191. }
  192. .ui-fixed-content {
  193. position: relative;
  194. }
  195. .ui-fixed-bg {
  196. position: absolute;
  197. width: 100%;
  198. height: 100%;
  199. top: 0;
  200. z-index: 1;
  201. pointer-events: none;
  202. }
  203. }
  204. }
  205. .inset-bottom {
  206. background: #fff;
  207. }
  208. </style>