Files
2026-10-09 11:59:15 +10:00

73 lines
1.4 KiB
Vue

<script setup lang="ts">
/**
* Кнопка возврата наверх, появляется после прокрутки.
* Использует IntersectionObserver (как useReveal).
*/
const show = ref(false);
let observer: IntersectionObserver | null = null;
onMounted(() => {
observer = new IntersectionObserver(
(entries) => {
show.value = !entries[0].isIntersecting;
},
{ rootMargin: "-80px 0px 0px 0px" },
);
const el = document.getElementById("top-anchor");
if (el) observer.observe(el);
});
onBeforeUnmount(() => observer?.disconnect());
function scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" });
}
</script>
<template>
<Transition name="fade">
<button
v-if="show"
class="back-to-top"
aria-label="Наверх"
@click="scrollToTop"
>
▲
</button>
</Transition>
</template>
<style scoped>
.back-to-top {
position: fixed;
bottom: 40px;
right: 20px;
z-index: 999;
width: 44px;
height: 44px;
border-radius: 50%;
border: none;
background-color: #28a745;
color: #fff;
font-size: 20px;
cursor: pointer;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
display: flex;
align-items: center;
justify-content: center;
}
.back-to-top:hover {
background-color: #218838;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>