项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

97 lines
2.1 KiB

  1. // Directives
  2. import { Scroll } from '../../directives'; // Utilities
  3. import { consoleWarn } from '../../util/console'; // Types
  4. import Vue from 'vue';
  5. /**
  6. * Scrollable
  7. *
  8. * Used for monitoring scrolling and
  9. * invoking functions based upon
  10. * scrolling thresholds being
  11. * met.
  12. */
  13. /* @vue/component */
  14. export default Vue.extend({
  15. name: 'scrollable',
  16. directives: {
  17. Scroll
  18. },
  19. props: {
  20. scrollTarget: String,
  21. scrollThreshold: [String, Number]
  22. },
  23. data: () => ({
  24. currentScroll: 0,
  25. currentThreshold: 0,
  26. isActive: false,
  27. isScrollingUp: false,
  28. previousScroll: 0,
  29. savedScroll: 0,
  30. target: null
  31. }),
  32. computed: {
  33. /**
  34. * A computed property that returns
  35. * whether scrolling features are
  36. * enabled or disabled
  37. */
  38. canScroll() {
  39. return typeof window !== 'undefined';
  40. },
  41. /**
  42. * The threshold that must be met before
  43. * thresholdMet function is invoked
  44. */
  45. computedScrollThreshold() {
  46. return this.scrollThreshold ? Number(this.scrollThreshold) : 300;
  47. }
  48. },
  49. watch: {
  50. isScrollingUp() {
  51. this.savedScroll = this.savedScroll || this.currentScroll;
  52. },
  53. isActive() {
  54. this.savedScroll = 0;
  55. }
  56. },
  57. mounted() {
  58. if (this.scrollTarget) {
  59. this.target = document.querySelector(this.scrollTarget);
  60. if (!this.target) {
  61. consoleWarn(`Unable to locate element with identifier ${this.scrollTarget}`, this);
  62. }
  63. }
  64. },
  65. methods: {
  66. onScroll() {
  67. if (!this.canScroll) return;
  68. this.previousScroll = this.currentScroll;
  69. this.currentScroll = this.target ? this.target.scrollTop : window.pageYOffset;
  70. this.isScrollingUp = this.currentScroll < this.previousScroll;
  71. this.currentThreshold = Math.abs(this.currentScroll - this.computedScrollThreshold);
  72. this.$nextTick(() => {
  73. if (Math.abs(this.currentScroll - this.savedScroll) > this.computedScrollThreshold) this.thresholdMet();
  74. });
  75. },
  76. /**
  77. * The method invoked when
  78. * scrolling in any direction
  79. * has exceeded the threshold
  80. */
  81. thresholdMet() {}
  82. }
  83. });
  84. //# sourceMappingURL=index.js.map