mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat: add useScroller
This commit is contained in:
parent
7fd4e3eabb
commit
0a701e549c
14
src/composition/use-scroller.ts
Normal file
14
src/composition/use-scroller.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { Ref, ref, onMounted } from 'vue';
|
||||||
|
import { getScroller } from '../utils/dom/scroll';
|
||||||
|
|
||||||
|
export function useScroller(el: Ref<HTMLElement>) {
|
||||||
|
const scrollerRef = ref();
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (el.value) {
|
||||||
|
scrollerRef.value = getScroller(el.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return scrollerRef;
|
||||||
|
}
|
@ -10,10 +10,10 @@ import {
|
|||||||
// Utils
|
// Utils
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { isHidden } from '../utils/dom/style';
|
import { isHidden } from '../utils/dom/style';
|
||||||
import { getScroller } from '../utils/dom/scroll';
|
|
||||||
|
|
||||||
// Composition
|
// Composition
|
||||||
import { useRect } from '../composition/use-rect';
|
import { useRect } from '../composition/use-rect';
|
||||||
|
import { useScroller } from '../composition/use-scroller';
|
||||||
import { useGlobalEvent } from '../composition/use-global-event';
|
import { useGlobalEvent } from '../composition/use-global-event';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
@ -49,8 +49,8 @@ export default createComponent({
|
|||||||
// use sync innerLoading state to avoid repeated loading in some edge cases
|
// use sync innerLoading state to avoid repeated loading in some edge cases
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const rootRef = ref();
|
const rootRef = ref();
|
||||||
const scrollerRef = ref();
|
|
||||||
const placeholderRef = ref();
|
const placeholderRef = ref();
|
||||||
|
const scroller = useScroller(rootRef);
|
||||||
|
|
||||||
const check = () => {
|
const check = () => {
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
@ -58,16 +58,15 @@ export default createComponent({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const scroller = scrollerRef.value;
|
|
||||||
const { offset, direction } = props;
|
const { offset, direction } = props;
|
||||||
let scrollerRect;
|
let scrollerRect;
|
||||||
|
|
||||||
if (scroller.getBoundingClientRect) {
|
if (scroller.value.getBoundingClientRect) {
|
||||||
scrollerRect = scroller.getBoundingClientRect();
|
scrollerRect = scroller.value.getBoundingClientRect();
|
||||||
} else {
|
} else {
|
||||||
scrollerRect = {
|
scrollerRect = {
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: scroller.innerHeight,
|
bottom: scroller.value.innerHeight,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,14 +142,12 @@ export default createComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
scrollerRef.value = getScroller(rootRef.value);
|
|
||||||
|
|
||||||
if (props.immediateCheck) {
|
if (props.immediateCheck) {
|
||||||
check();
|
check();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
useGlobalEvent(scrollerRef, 'scroll', check);
|
useGlobalEvent(scroller, 'scroll', check);
|
||||||
|
|
||||||
// @exposed-api
|
// @exposed-api
|
||||||
const vm = getCurrentInstance().proxy;
|
const vm = getCurrentInstance().proxy;
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { ref, reactive, computed, onMounted } from 'vue';
|
import { ref, reactive, computed } from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { isHidden } from '../utils/dom/style';
|
import { isHidden } from '../utils/dom/style';
|
||||||
import { unitToPx } from '../utils/format/unit';
|
import { unitToPx } from '../utils/format/unit';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { getScrollTop, getElementTop, getScroller } from '../utils/dom/scroll';
|
import { getScrollTop, getElementTop } from '../utils/dom/scroll';
|
||||||
|
|
||||||
// Composition
|
// Composition
|
||||||
|
import { useScroller } from '../composition/use-scroller';
|
||||||
import { useGlobalEvent } from '../composition/use-global-event';
|
import { useGlobalEvent } from '../composition/use-global-event';
|
||||||
import { useVisibilityChange } from '../composition/use-visibility-change';
|
import { useVisibilityChange } from '../composition/use-visibility-change';
|
||||||
|
|
||||||
@ -26,7 +27,7 @@ export default createComponent({
|
|||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
const rootRef = ref();
|
const rootRef = ref();
|
||||||
const scrollerRef = ref();
|
const scroller = useScroller(rootRef);
|
||||||
|
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
fixed: false,
|
fixed: false,
|
||||||
@ -100,11 +101,7 @@ export default createComponent({
|
|||||||
emitScrollEvent(scrollTop);
|
emitScrollEvent(scrollTop);
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(() => {
|
useGlobalEvent(scroller, 'scroll', onScroll);
|
||||||
scrollerRef.value = getScroller(rootRef.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
useGlobalEvent(scrollerRef, 'scroll', onScroll);
|
|
||||||
useVisibilityChange(rootRef, onScroll);
|
useVisibilityChange(rootRef, onScroll);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user