|
1 | 1 | <template> |
2 | | - <div class="hidden-print"> |
| 2 | + <div ref="el" class="hidden-print"> |
3 | 3 | <div v-scroll="onScroll" :class="classes" :style="styles"> |
4 | 4 | <slot></slot> |
5 | 5 | </div> |
6 | 6 | </div> |
7 | 7 | </template> |
8 | 8 |
|
9 | | -<script> |
10 | | -import scroll from './../../directives/scroll'; |
| 9 | +<script setup> |
| 10 | +import vScroll from './../../directives/scroll'; |
| 11 | +import { computed, defineProps, ref, nextTick } from 'vue'; |
11 | 12 |
|
12 | | -export default { |
13 | | - directives: { |
14 | | - scroll, |
| 13 | +const props = defineProps({ |
| 14 | + offset: { |
| 15 | + type: Number, |
| 16 | + default: 0, |
15 | 17 | }, |
16 | | - props: { |
17 | | - offset: { |
18 | | - type: Number, |
19 | | - default: 0, |
20 | | - }, |
21 | | - }, |
22 | | - data() { |
23 | | - return { |
24 | | - affixed: false, |
25 | | - }; |
26 | | - }, |
27 | | - computed: { |
28 | | - classes() { |
29 | | - return { |
30 | | - affix: this.affixed, |
31 | | - }; |
32 | | - }, |
33 | | - styles() { |
34 | | - return { |
35 | | - top: this.affixed ? this.offset + 'px' : null, |
36 | | - }; |
37 | | - }, |
38 | | - }, |
39 | | - methods: { |
40 | | - // from https://github.com/ant-design/ant-design/blob/master/components/affix/index.jsx#L20 |
41 | | - onScroll() { |
42 | | - // if is hidden don't calculate anything |
43 | | - if ( |
44 | | - !( |
45 | | - this.$el.offsetWidth || |
46 | | - this.$el.offsetHeight || |
47 | | - this.$el.getClientRects().length |
48 | | - ) |
49 | | - ) { |
50 | | - return; |
51 | | - } |
52 | | - // get window scroll and element position to detect if have to be normal or affixed |
53 | | - const scroll = {}; |
54 | | - const element = {}; |
55 | | - const rect = this.$el.getBoundingClientRect(); |
56 | | - const body = document.body; |
57 | | - const types = ['Top', 'Left']; |
58 | | - types.forEach((type) => { |
59 | | - const t = type.toLowerCase(); |
60 | | - scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']; |
61 | | - element[t] = |
62 | | - scroll[t] + |
63 | | - rect[t] - |
64 | | - (this.$el['client' + type] || body['client' + type] || 0); |
65 | | - }); |
66 | | - const fix = scroll.top > element.top - this.offset; |
67 | | - if (this.affixed !== fix) { |
68 | | - this.affixed = fix; |
69 | | - this.$emit(this.affixed ? 'affix' : 'unfix'); |
70 | | - this.$nextTick(() => { |
71 | | - this.$emit(this.affixed ? 'affixed' : 'unfixed'); |
72 | | - }); |
73 | | - } |
74 | | - }, |
75 | | - }, |
76 | | -}; |
| 18 | +}); |
| 19 | +
|
| 20 | +const emit = defineEmits(['affix', 'affixed', 'unfix', 'unfixed']); |
| 21 | +
|
| 22 | +const el = ref(null); |
| 23 | +const affixed = ref(false); |
| 24 | +const classes = computed(() => ({ affix: affixed.value })); |
| 25 | +const styles = computed(() => ({ |
| 26 | + top: affixed.value ? props.offset + 'px' : null, |
| 27 | +})); |
| 28 | +
|
| 29 | +function onScroll() { |
| 30 | + if ( |
| 31 | + !( |
| 32 | + el.value?.offsetWidth || |
| 33 | + el.value?.offsetHeight || |
| 34 | + el.value?.getClientRects().length |
| 35 | + ) |
| 36 | + ) { |
| 37 | + return; |
| 38 | + } |
| 39 | + // get window scroll and element position to detect if have to be normal or affixed |
| 40 | + const scroll = {}; |
| 41 | + const element = {}; |
| 42 | + const rect = el.value.getBoundingClientRect(); |
| 43 | + const body = document.body; |
| 44 | + const types = ['Top', 'Left']; |
| 45 | + types.forEach((type) => { |
| 46 | + const t = type.toLowerCase(); |
| 47 | + scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']; |
| 48 | + element[t] = |
| 49 | + scroll[t] + |
| 50 | + rect[t] - |
| 51 | + (el.value['client' + type] || body['client' + type] || 0); |
| 52 | + }); |
| 53 | + const fix = scroll.top > element.top - props.offset; |
| 54 | + if (affixed.value !== fix) { |
| 55 | + affixed.value = fix; |
| 56 | + emit(affixed.value ? 'affix' : 'unfix'); |
| 57 | + nextTick(() => { |
| 58 | + emit(affixed.value ? 'affixed' : 'unfixed'); |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
77 | 62 | </script> |
0 commit comments