|
|
@ -1,10 +1,17 @@ |
|
|
|
import { defineComponent, inject, nextTick } from 'vue'; |
|
|
|
import { |
|
|
|
|
|
|
|
defineComponent, |
|
|
|
|
|
|
|
ExtractPropTypes, |
|
|
|
|
|
|
|
inject, |
|
|
|
|
|
|
|
nextTick, |
|
|
|
|
|
|
|
onActivated, |
|
|
|
|
|
|
|
onBeforeUnmount, |
|
|
|
|
|
|
|
onMounted, |
|
|
|
|
|
|
|
reactive, |
|
|
|
|
|
|
|
} from 'vue'; |
|
|
|
import classNames from '../_util/classNames'; |
|
|
|
import classNames from '../_util/classNames'; |
|
|
|
import PropTypes from '../_util/vue-types'; |
|
|
|
import PropTypes from '../_util/vue-types'; |
|
|
|
import backTopTypes from './backTopTypes'; |
|
|
|
|
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener'; |
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener'; |
|
|
|
import getScroll from '../_util/getScroll'; |
|
|
|
import getScroll from '../_util/getScroll'; |
|
|
|
import BaseMixin from '../_util/BaseMixin'; |
|
|
|
|
|
|
|
import { getTransitionProps, Transition } from '../_util/transition'; |
|
|
|
import { getTransitionProps, Transition } from '../_util/transition'; |
|
|
|
import { defaultConfigProvider } from '../config-provider'; |
|
|
|
import { defaultConfigProvider } from '../config-provider'; |
|
|
|
import scrollTo from '../_util/scrollTo'; |
|
|
|
import scrollTo from '../_util/scrollTo'; |
|
|
@ -14,95 +21,91 @@ function getDefaultTarget() { |
|
|
|
return window; |
|
|
|
return window; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const props = backTopTypes(); |
|
|
|
export const backTopProps = { |
|
|
|
|
|
|
|
// 滚动高度达到此参数值才出现 BackTop |
|
|
|
|
|
|
|
visibilityHeight: PropTypes.number.def(400), |
|
|
|
|
|
|
|
// 回到顶部所需时间(ms) @4.4.0 |
|
|
|
|
|
|
|
duration: PropTypes.number.def(450), |
|
|
|
|
|
|
|
// 设置需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 |
|
|
|
|
|
|
|
target: PropTypes.func, |
|
|
|
|
|
|
|
prefixCls: PropTypes.string, |
|
|
|
|
|
|
|
onClick: PropTypes.func, |
|
|
|
|
|
|
|
// visible: PropTypes.looseBool, // Only for test. Don't use it. |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type BackTopProps = Partial<ExtractPropTypes<typeof backTopProps>>; |
|
|
|
|
|
|
|
|
|
|
|
const BackTop = defineComponent({ |
|
|
|
const BackTop = defineComponent({ |
|
|
|
name: 'ABackTop', |
|
|
|
name: 'ABackTop', |
|
|
|
mixins: [BaseMixin], |
|
|
|
|
|
|
|
inheritAttrs: false, |
|
|
|
inheritAttrs: false, |
|
|
|
props: { |
|
|
|
props: backTopProps, |
|
|
|
...props, |
|
|
|
|
|
|
|
visibilityHeight: PropTypes.number.def(400), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
emits: ['click'], |
|
|
|
emits: ['click'], |
|
|
|
setup() { |
|
|
|
setup(props, { slots, attrs, emit }) { |
|
|
|
return { |
|
|
|
const configProvider = inject('configProvider', defaultConfigProvider); |
|
|
|
configProvider: inject('configProvider', defaultConfigProvider), |
|
|
|
|
|
|
|
}; |
|
|
|
const state = reactive({ |
|
|
|
}, |
|
|
|
|
|
|
|
data() { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
visible: false, |
|
|
|
visible: false, |
|
|
|
scrollEvent: null, |
|
|
|
scrollEvent: null, |
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
mounted() { |
|
|
|
|
|
|
|
nextTick(() => { |
|
|
|
|
|
|
|
const getTarget = this.target || getDefaultTarget; |
|
|
|
|
|
|
|
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll); |
|
|
|
|
|
|
|
this.handleScroll(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
activated() { |
|
|
|
|
|
|
|
nextTick(() => { |
|
|
|
|
|
|
|
this.handleScroll(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
beforeUnmount() { |
|
|
|
|
|
|
|
if (this.scrollEvent) { |
|
|
|
|
|
|
|
this.scrollEvent.remove(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
methods: { |
|
|
|
|
|
|
|
getCurrentScrollTop() { |
|
|
|
|
|
|
|
const getTarget = this.target || getDefaultTarget; |
|
|
|
|
|
|
|
const targetNode = getTarget(); |
|
|
|
|
|
|
|
if (targetNode === window) { |
|
|
|
|
|
|
|
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return targetNode.scrollTop; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scrollToTop(e: Event) { |
|
|
|
const scrollToTop = (e: Event) => { |
|
|
|
const { target = getDefaultTarget } = this; |
|
|
|
const { target = getDefaultTarget, duration } = props; |
|
|
|
scrollTo(0, { |
|
|
|
scrollTo(0, { |
|
|
|
getContainer: target, |
|
|
|
getContainer: target, |
|
|
|
|
|
|
|
duration, |
|
|
|
}); |
|
|
|
}); |
|
|
|
this.$emit('click', e); |
|
|
|
emit('click', e); |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
handleScroll() { |
|
|
|
const handleScroll = () => { |
|
|
|
const { visibilityHeight, target = getDefaultTarget } = this; |
|
|
|
const { visibilityHeight, target = getDefaultTarget } = props; |
|
|
|
const scrollTop = getScroll(target(), true); |
|
|
|
const scrollTop = getScroll(target(), true); |
|
|
|
this.setState({ |
|
|
|
state.visible = scrollTop > visibilityHeight; |
|
|
|
visible: scrollTop > visibilityHeight, |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
|
|
|
|
nextTick(() => { |
|
|
|
|
|
|
|
const getTarget = props.target || getDefaultTarget; |
|
|
|
|
|
|
|
state.scrollEvent = addEventListener(getTarget(), 'scroll', handleScroll); |
|
|
|
|
|
|
|
handleScroll(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render() { |
|
|
|
onActivated(() => { |
|
|
|
const { prefixCls: customizePrefixCls, $slots } = this; |
|
|
|
nextTick(() => { |
|
|
|
|
|
|
|
handleScroll(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls; |
|
|
|
onBeforeUnmount(() => { |
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls); |
|
|
|
if (state.scrollEvent) { |
|
|
|
const classString = classNames(prefixCls, this.$attrs.class); |
|
|
|
state.scrollEvent.remove(); |
|
|
|
const defaultElement = ( |
|
|
|
} |
|
|
|
<div class={`${prefixCls}-content`}> |
|
|
|
}); |
|
|
|
<div class={`${prefixCls}-icon`} /> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const divProps = { |
|
|
|
|
|
|
|
...this.$attrs, |
|
|
|
|
|
|
|
onClick: this.scrollToTop, |
|
|
|
|
|
|
|
class: classString, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const backTopBtn = this.visible ? ( |
|
|
|
return () => { |
|
|
|
<div {...divProps}>{$slots.default?.() || defaultElement}</div> |
|
|
|
const { prefixCls: customizePrefixCls } = props; |
|
|
|
) : null; |
|
|
|
|
|
|
|
const transitionProps = getTransitionProps('fade'); |
|
|
|
const getPrefixCls = configProvider.getPrefixCls; |
|
|
|
return <Transition {...transitionProps}>{backTopBtn}</Transition>; |
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls); |
|
|
|
|
|
|
|
const classString = classNames(prefixCls, attrs.class); |
|
|
|
|
|
|
|
const defaultElement = ( |
|
|
|
|
|
|
|
<div class={`${prefixCls}-content`}> |
|
|
|
|
|
|
|
<div class={`${prefixCls}-icon`} /> |
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const divProps = { |
|
|
|
|
|
|
|
...attrs, |
|
|
|
|
|
|
|
onClick: scrollToTop, |
|
|
|
|
|
|
|
class: classString, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const backTopBtn = state.visible ? ( |
|
|
|
|
|
|
|
<div {...divProps}>{slots.default?.() || defaultElement}</div> |
|
|
|
|
|
|
|
) : null; |
|
|
|
|
|
|
|
const transitionProps = getTransitionProps('fade'); |
|
|
|
|
|
|
|
return <Transition {...transitionProps}>{backTopBtn}</Transition>; |
|
|
|
|
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|