|
| 1 | +<template> |
| 2 | + <Transition name="back-to-top"> |
| 3 | + <button |
| 4 | + v-show="isVisible" |
| 5 | + class="back-to-top-btn" |
| 6 | + @click="scrollToTop" |
| 7 | + :aria-label="$t('tooltip.back_to_top')" |
| 8 | + :title="$t('tooltip.back_to_top')" |
| 9 | + > |
| 10 | + <div class="i-ri-arrow-up-line"></div> |
| 11 | + </button> |
| 12 | + </Transition> |
| 13 | +</template> |
| 14 | + |
| 15 | +<script setup lang="ts"> |
| 16 | + import { ref, onMounted, onUnmounted } from 'vue' |
| 17 | + import { useI18n } from 'vue-i18n' |
| 18 | +
|
| 19 | + const { t } = useI18n() |
| 20 | +
|
| 21 | + const isVisible = ref(false) |
| 22 | + const scrollThreshold = 300 |
| 23 | +
|
| 24 | + const handleScroll = () => { |
| 25 | + if (typeof window !== 'undefined') { |
| 26 | + isVisible.value = window.scrollY > scrollThreshold |
| 27 | + } |
| 28 | + } |
| 29 | +
|
| 30 | + const scrollToTop = () => { |
| 31 | + if (typeof window !== 'undefined') { |
| 32 | + window.scrollTo({ |
| 33 | + top: 0, |
| 34 | + behavior: 'smooth', |
| 35 | + }) |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + onMounted(() => { |
| 40 | + if (typeof window !== 'undefined') { |
| 41 | + window.addEventListener('scroll', handleScroll, { passive: true }) |
| 42 | + handleScroll() |
| 43 | + } |
| 44 | + }) |
| 45 | +
|
| 46 | + onUnmounted(() => { |
| 47 | + if (typeof window !== 'undefined') { |
| 48 | + window.removeEventListener('scroll', handleScroll) |
| 49 | + } |
| 50 | + }) |
| 51 | +</script> |
| 52 | + |
| 53 | +<style scoped> |
| 54 | + .back-to-top-btn { |
| 55 | + position: fixed; |
| 56 | + bottom: 2rem; |
| 57 | + right: 2rem; |
| 58 | + z-index: var(--pr-z-nav); |
| 59 | + width: 3rem; |
| 60 | + height: 3rem; |
| 61 | + border-radius: 50%; |
| 62 | + border: 1px solid var(--pr-c-divider-light); |
| 63 | + background: var(--pr-c-bg-soft); |
| 64 | + backdrop-filter: blur(8px); |
| 65 | + cursor: pointer; |
| 66 | + display: flex; |
| 67 | + align-items: center; |
| 68 | + justify-content: center; |
| 69 | + transition: all 0.3s ease; |
| 70 | + color: var(--pr-c-text-2); |
| 71 | + } |
| 72 | +
|
| 73 | + .back-to-top-btn:hover { |
| 74 | + background: var(--pr-c-bg); |
| 75 | + transform: translateY(-2px); |
| 76 | + color: var(--pr-c-text-1); |
| 77 | + border-color: var(--pr-c-brand); |
| 78 | + } |
| 79 | +
|
| 80 | + .back-to-top-btn:active { |
| 81 | + transform: translateY(0); |
| 82 | + } |
| 83 | +
|
| 84 | + .back-to-top-btn .i-ri-arrow-up-line { |
| 85 | + width: 1.25rem; |
| 86 | + height: 1.25rem; |
| 87 | + font-size: 1.25rem; |
| 88 | + } |
| 89 | +
|
| 90 | + .back-to-top-enter-active, |
| 91 | + .back-to-top-leave-active { |
| 92 | + transition: all 0.3s ease; |
| 93 | + } |
| 94 | +
|
| 95 | + .back-to-top-enter-from, |
| 96 | + .back-to-top-leave-to { |
| 97 | + opacity: 0; |
| 98 | + transform: translateY(20px) scale(0.8); |
| 99 | + } |
| 100 | +
|
| 101 | + .back-to-top-enter-to, |
| 102 | + .back-to-top-leave-from { |
| 103 | + opacity: 1; |
| 104 | + transform: translateY(0) scale(1); |
| 105 | + } |
| 106 | +
|
| 107 | + @media (max-width: 768px) { |
| 108 | + .back-to-top-btn { |
| 109 | + bottom: 1.5rem; |
| 110 | + right: 1.5rem; |
| 111 | + width: 2.75rem; |
| 112 | + height: 2.75rem; |
| 113 | + } |
| 114 | +
|
| 115 | + .back-to-top-btn .i-ri-arrow-up-line { |
| 116 | + width: 1.125rem; |
| 117 | + height: 1.125rem; |
| 118 | + font-size: 1.125rem; |
| 119 | + } |
| 120 | + } |
| 121 | +
|
| 122 | + @media (min-width: 1200px) { |
| 123 | + .back-to-top-btn { |
| 124 | + right: 2rem; |
| 125 | + } |
| 126 | + } |
| 127 | +</style> |
0 commit comments