|
|
@@ -12,7 +12,7 @@
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
-import { getTabBarHeight, hasCustomTabBar } from "@/cool";
|
|
|
+import { getTabBarHeight, hasCustomTabBar, scroller } from "@/cool";
|
|
|
import { computed, onMounted, ref, watch, type PropType } from "vue";
|
|
|
import { usePage } from "../../hooks";
|
|
|
|
|
|
@@ -27,10 +27,12 @@ const props = defineProps({
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+const emit = defineEmits(["backTop"]);
|
|
|
+
|
|
|
const { screenHeight } = uni.getWindowInfo();
|
|
|
|
|
|
// cl-page 上下文
|
|
|
-const page = usePage();
|
|
|
+const { scrollToTop, onScroll } = usePage();
|
|
|
|
|
|
// 是否显示回到顶部按钮
|
|
|
const visible = ref(false);
|
|
|
@@ -46,6 +48,9 @@ const bottom = computed(() => {
|
|
|
return h + "px";
|
|
|
});
|
|
|
|
|
|
+// 是否页面滚动
|
|
|
+const isPage = computed(() => props.top == null);
|
|
|
+
|
|
|
// 控制是否显示
|
|
|
function onVisible(top: number) {
|
|
|
visible.value = top > screenHeight - 100;
|
|
|
@@ -53,11 +58,20 @@ function onVisible(top: number) {
|
|
|
|
|
|
// 回到顶部
|
|
|
function toTop() {
|
|
|
- page.scrollToTop();
|
|
|
+ if (isPage.value) {
|
|
|
+ scrollToTop();
|
|
|
+ }
|
|
|
+
|
|
|
+ emit("backTop");
|
|
|
}
|
|
|
|
|
|
onMounted(() => {
|
|
|
- if (props.top != null) {
|
|
|
+ if (isPage.value) {
|
|
|
+ // 监听页面滚动
|
|
|
+ onScroll((top) => {
|
|
|
+ onVisible(top);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
// 监听参数变化
|
|
|
watch(
|
|
|
computed(() => props.top!),
|
|
|
@@ -68,22 +82,16 @@ onMounted(() => {
|
|
|
immediate: true
|
|
|
}
|
|
|
);
|
|
|
- } else {
|
|
|
- // 监听页面滚动
|
|
|
- page.onPageScroll((top) => {
|
|
|
- onVisible(top);
|
|
|
- });
|
|
|
}
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
.cl-back-top {
|
|
|
- @apply flex flex-row items-center justify-center bg-primary-500 rounded-full;
|
|
|
+ @apply flex flex-row items-center justify-center bg-primary-500 rounded-full duration-300;
|
|
|
width: 40px;
|
|
|
height: 40px;
|
|
|
transition-property: transform;
|
|
|
- transition-duration: 0.3s;
|
|
|
transform: translateX(160rpx);
|
|
|
|
|
|
&.is-show {
|
|
|
@@ -91,8 +99,7 @@ onMounted(() => {
|
|
|
}
|
|
|
|
|
|
&-wrapper {
|
|
|
- @apply fixed z-50 overflow-visible;
|
|
|
- right: 0;
|
|
|
+ @apply fixed right-0 z-50 overflow-visible;
|
|
|
}
|
|
|
}
|
|
|
</style>
|