index.vue 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <transition :name="transitionName">
  3. <div class="back-to-ceiling" @click="backToTop" v-show="visible" :style="customStyle">
  4. <svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height: 16px; width: 16px;">
  5. <title>回到顶部</title>
  6. <g>
  7. <path d="M12.036 15.59c0 .55-.453.995-.997.995H5.032c-.55 0-.997-.445-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29c.39-.39 1.026-.385 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" fill-rule="evenodd"></path>
  8. </g>
  9. </svg>
  10. </div>
  11. </transition>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'BackToTop',
  16. props: {
  17. visibilityHeight: {
  18. type: Number,
  19. default: 400
  20. },
  21. backPosition: {
  22. type: Number,
  23. default: 0
  24. },
  25. customStyle: {
  26. type: Object,
  27. default: function() {
  28. return {
  29. right: '50px',
  30. bottom: '50px',
  31. width: '40px',
  32. height: '40px',
  33. 'border-radius': '4px',
  34. 'line-height': '45px',
  35. background: '#e7eaf1'
  36. }
  37. }
  38. },
  39. transitionName: {
  40. type: String,
  41. default: 'fade'
  42. }
  43. },
  44. data() {
  45. return {
  46. visible: false,
  47. interval: null
  48. }
  49. },
  50. mounted() {
  51. window.addEventListener('scroll', this.handleScroll)
  52. },
  53. beforeDestroy() {
  54. window.removeEventListener('scroll', this.handleScroll)
  55. if (this.interval) {
  56. clearInterval(this.interval)
  57. }
  58. },
  59. methods: {
  60. handleScroll() {
  61. this.visible = window.pageYOffset > this.visibilityHeight
  62. },
  63. backToTop() {
  64. const start = window.pageYOffset
  65. let i = 0
  66. this.interval = setInterval(() => {
  67. const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500))
  68. if (next <= this.backPosition) {
  69. window.scrollTo(0, this.backPosition)
  70. clearInterval(this.interval)
  71. } else {
  72. window.scrollTo(0, next)
  73. }
  74. i++
  75. }, 16.7)
  76. },
  77. easeInOutQuad(t, b, c, d) {
  78. if ((t /= d / 2) < 1) return c / 2 * t * t + b
  79. return -c / 2 * (--t * (t - 2) - 1) + b
  80. }
  81. }
  82. }
  83. </script>
  84. <style scoped>
  85. .back-to-ceiling {
  86. position: fixed;
  87. display: inline-block;
  88. text-align: center;
  89. cursor: pointer;
  90. }
  91. .back-to-ceiling:hover {
  92. background: #d5dbe7;
  93. }
  94. .fade-enter-active,
  95. .fade-leave-active {
  96. transition: opacity .5s;
  97. }
  98. .fade-enter,
  99. .fade-leave-to {
  100. opacity: 0
  101. }
  102. .back-to-ceiling .Icon {
  103. fill: #9aaabf;
  104. background: none;
  105. }
  106. </style>