index.vue 2.5 KB

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