scrollBar.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div id="scrollBar">
  3. <div class="scroll-container" ref="scrollContainer" @wheel.prevent="handleScroll">
  4. <div class="scroll-wrapper" ref="scrollWrapper" :style="{ top: top + 'px' }">
  5. <slot></slot>
  6. </div>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. const delta = 15;
  12. export default {
  13. name: 'ScrollBar',
  14. data() {
  15. return {
  16. top: 0,
  17. };
  18. },
  19. methods: {
  20. handleScroll(e) {
  21. const eventDelta = e.wheelDelta || -e.deltaY * 3;
  22. const $container = this.$refs.scrollContainer;
  23. const $containerHeight = $container.offsetHeight;
  24. const $wrapper = this.$refs.scrollWrapper;
  25. const $wrapperHeight = $wrapper.offsetHeight;
  26. if (eventDelta > 0) {
  27. this.top = Math.min(0, this.top + eventDelta);
  28. } else if ($containerHeight - delta < $wrapperHeight) {
  29. if (this.top < -($wrapperHeight - $containerHeight + delta)) {
  30. // this.top = this.top;
  31. } else {
  32. this.top = Math.max(this.top + eventDelta, $containerHeight - $wrapperHeight - delta);
  33. }
  34. } else {
  35. this.top = 0;
  36. }
  37. },
  38. },
  39. };
  40. </script>
  41. <style lang="less" scoped>
  42. .scroll-container {
  43. width: 100%;
  44. height: 100%;
  45. background-color: #00142a;
  46. .scroll-wrapper {
  47. position: absolute;
  48. width: 100%;
  49. }
  50. }
  51. </style>