beehive/app/components/ScrollToTop.vue

60 lines
1.4 KiB
Vue
Raw Normal View History

2025-10-23 15:30:21 +08:00
<template>
2025-11-19 08:51:25 +08:00
<!-- 悬浮置顶按钮 -->
<Transition name="fade-slide">
<button
v-show="showScrollToTop"
aria-label="回到顶部"
class="fixed bottom-32 right-8 z-50 rounded-full p-3 shadow-lg transition-all duration-300 focus:outline-none
bg-primary/90 backdrop-blur-md border border-white/20
hover:bg-primary/95 hover:-translate-y-0.5 hover:scale-110 hover:shadow-xl
active:scale-95 focus:ring-4 focus:ring-primary/30
"
title="回到顶部"
@click="scrollToTop"
>
<UIcon class="w-6 h-6 text-white -rotate-45" name="i-lucide-rocket"/>
</button>
</Transition>
2025-10-23 15:30:21 +08:00
</template>
<script lang="ts" setup>
2025-11-19 08:51:25 +08:00
// 响应式状态
const showScrollToTop = ref(false)
2025-10-23 15:30:21 +08:00
2025-11-19 08:51:25 +08:00
// 滚动监听函数
2025-10-23 16:01:35 +08:00
const handleScroll = () => {
2025-11-19 08:51:25 +08:00
showScrollToTop.value = window.scrollY > 300
2025-10-23 16:01:35 +08:00
}
2025-10-23 15:30:21 +08:00
2025-11-19 08:51:25 +08:00
// 滚动到顶部函数
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
})
2025-10-23 15:30:21 +08:00
}
2025-11-19 08:51:25 +08:00
// 组件挂载时添加滚动监听
2025-10-23 15:30:21 +08:00
onMounted(() => {
2025-11-19 08:51:25 +08:00
window.addEventListener('scroll', handleScroll)
2025-10-23 15:30:21 +08:00
})
2025-11-19 08:51:25 +08:00
// 组件卸载时移除滚动监听
2025-10-23 15:30:21 +08:00
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll)
})
2025-11-19 08:51:25 +08:00
</script>
<style scoped>
/* 保留过渡动画 */
.fade-slide-enter-active,
.fade-slide-leave-active {
transition: all 0.3s ease;
}
.fade-slide-enter-from,
.fade-slide-leave-to {
opacity: 0;
transform: translateY(10px);
}
</style>