-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename directives & affix use setup
- Loading branch information
Showing
5 changed files
with
81 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,62 @@ | ||
<template> | ||
<div class="hidden-print"> | ||
<div ref="el" class="hidden-print"> | ||
<div v-scroll="onScroll" :class="classes" :style="styles"> | ||
<slot></slot> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import scroll from './../../directives/scroll'; | ||
<script setup> | ||
import vScroll from './../../directives/scroll'; | ||
import { computed, defineProps, ref, nextTick } from 'vue'; | ||
export default { | ||
directives: { | ||
scroll, | ||
const props = defineProps({ | ||
offset: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
props: { | ||
offset: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
affixed: false, | ||
}; | ||
}, | ||
computed: { | ||
classes() { | ||
return { | ||
affix: this.affixed, | ||
}; | ||
}, | ||
styles() { | ||
return { | ||
top: this.affixed ? this.offset + 'px' : null, | ||
}; | ||
}, | ||
}, | ||
methods: { | ||
// from https://github.com/ant-design/ant-design/blob/master/components/affix/index.jsx#L20 | ||
onScroll() { | ||
// if is hidden don't calculate anything | ||
if ( | ||
!( | ||
this.$el.offsetWidth || | ||
this.$el.offsetHeight || | ||
this.$el.getClientRects().length | ||
) | ||
) { | ||
return; | ||
} | ||
// get window scroll and element position to detect if have to be normal or affixed | ||
const scroll = {}; | ||
const element = {}; | ||
const rect = this.$el.getBoundingClientRect(); | ||
const body = document.body; | ||
const types = ['Top', 'Left']; | ||
types.forEach((type) => { | ||
const t = type.toLowerCase(); | ||
scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']; | ||
element[t] = | ||
scroll[t] + | ||
rect[t] - | ||
(this.$el['client' + type] || body['client' + type] || 0); | ||
}); | ||
const fix = scroll.top > element.top - this.offset; | ||
if (this.affixed !== fix) { | ||
this.affixed = fix; | ||
this.$emit(this.affixed ? 'affix' : 'unfix'); | ||
this.$nextTick(() => { | ||
this.$emit(this.affixed ? 'affixed' : 'unfixed'); | ||
}); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); | ||
const emit = defineEmits(['affix', 'affixed', 'unfix', 'unfixed']); | ||
const el = ref(null); | ||
const affixed = ref(false); | ||
const classes = computed(() => ({ affix: affixed.value })); | ||
const styles = computed(() => ({ | ||
top: affixed.value ? props.offset + 'px' : null, | ||
})); | ||
function onScroll() { | ||
if ( | ||
!( | ||
el.value?.offsetWidth || | ||
el.value?.offsetHeight || | ||
el.value?.getClientRects().length | ||
) | ||
) { | ||
return; | ||
} | ||
// get window scroll and element position to detect if have to be normal or affixed | ||
const scroll = {}; | ||
const element = {}; | ||
const rect = el.value.getBoundingClientRect(); | ||
const body = document.body; | ||
const types = ['Top', 'Left']; | ||
types.forEach((type) => { | ||
const t = type.toLowerCase(); | ||
scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']; | ||
element[t] = | ||
scroll[t] + | ||
rect[t] - | ||
(el.value['client' + type] || body['client' + type] || 0); | ||
}); | ||
const fix = scroll.top > element.top - props.offset; | ||
if (affixed.value !== fix) { | ||
affixed.value = fix; | ||
emit(affixed.value ? 'affix' : 'unfix'); | ||
nextTick(() => { | ||
emit(affixed.value ? 'affixed' : 'unfixed'); | ||
}); | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters