Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 1 | <template> |
| 2 | <b-button |
| 3 | id="scrollToTopBtn" |
| 4 | class="btn-top btn-icon-only" |
Derick Montague | 86e11ad | 2021-01-04 11:19:08 -0600 | [diff] [blame] | 5 | :class="{ 'show-btn': showButton }" |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 6 | variant="secondary" |
| 7 | :title="$t('global.ariaLabel.scrollToTop')" |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 8 | @click="scrollToTop" |
| 9 | > |
| 10 | <icon-up-to-top /> |
SurenNeware | 6e2cb97 | 2020-12-24 20:58:16 +0530 | [diff] [blame] | 11 | <span class="sr-only">{{ $t('global.ariaLabel.scrollToTop') }}</span> |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 12 | </b-button> |
| 13 | </template> |
| 14 | |
| 15 | <script> |
| 16 | import UpToTop24 from '@carbon/icons-vue/es/up-to-top/24'; |
| 17 | |
| 18 | import { debounce } from 'lodash'; |
| 19 | |
| 20 | export default { |
| 21 | name: 'BackToTop', |
| 22 | components: { IconUpToTop: UpToTop24 }, |
| 23 | data() { |
| 24 | return { |
| 25 | showButton: false, |
| 26 | }; |
| 27 | }, |
| 28 | created() { |
| 29 | window.addEventListener('scroll', debounce(this.handleScroll, 200)); |
| 30 | }, |
| 31 | methods: { |
| 32 | handleScroll() { |
| 33 | document.documentElement.scrollTop > 500 |
| 34 | ? (this.showButton = true) |
| 35 | : (this.showButton = false); |
| 36 | }, |
| 37 | scrollToTop() { |
| 38 | document.documentElement.scrollTo({ |
| 39 | top: 0, |
| 40 | behavior: 'smooth', |
| 41 | }); |
| 42 | }, |
| 43 | }, |
| 44 | }; |
| 45 | </script> |
| 46 | |
| 47 | <style lang="scss" scoped> |
| 48 | .btn-top { |
| 49 | position: fixed; |
| 50 | bottom: 24px; |
| 51 | right: 24px; |
Derick Montague | 86e11ad | 2021-01-04 11:19:08 -0600 | [diff] [blame] | 52 | |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 53 | box-shadow: $box-shadow; |
Derick Montague | 86e11ad | 2021-01-04 11:19:08 -0600 | [diff] [blame] | 54 | visibility: hidden; |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 55 | opacity: 0; |
| 56 | transition: $transition-base; |
Derick Montague | 86e11ad | 2021-01-04 11:19:08 -0600 | [diff] [blame] | 57 | z-index: $zindex-fixed; |
| 58 | |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 59 | @media (min-width: 1600px) { |
| 60 | left: 1485px; |
| 61 | right: auto; |
| 62 | } |
| 63 | } |
Derick Montague | 86e11ad | 2021-01-04 11:19:08 -0600 | [diff] [blame] | 64 | .show-btn { |
| 65 | visibility: visible; |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 66 | opacity: 1; |
Dixsie Wolmers | 970ea7d | 2020-11-16 16:46:01 -0600 | [diff] [blame] | 67 | } |
| 68 | </style> |