2020-11-01 07:03:33 +00:00
|
|
|
import { defineComponent, inject, nextTick } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-06-12 14:05:56 +00:00
|
|
|
import backTopTypes from './backTopTypes';
|
2020-03-07 11:45:13 +00:00
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
2019-01-12 03:33:27 +00:00
|
|
|
import getScroll from '../_util/getScroll';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-10-22 14:45:21 +00:00
|
|
|
import { getTransitionProps, Transition } from '../_util/transition';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
2020-11-01 07:03:33 +00:00
|
|
|
import { withInstall } from '../_util/type';
|
2018-03-05 11:05:23 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getDefaultTarget() {
|
|
|
|
return window;
|
2018-03-05 11:05:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 14:05:56 +00:00
|
|
|
const props = backTopTypes();
|
2018-03-05 11:05:23 +00:00
|
|
|
|
2020-10-13 07:02:35 +00:00
|
|
|
const BackTop = defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ABackTop',
|
2018-03-05 11:05:23 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-11-07 07:37:17 +00:00
|
|
|
inheritAttrs: false,
|
2018-03-05 11:05:23 +00:00
|
|
|
props: {
|
2020-06-12 14:05:56 +00:00
|
|
|
...props,
|
2018-03-05 11:05:23 +00:00
|
|
|
visibilityHeight: PropTypes.number.def(400),
|
|
|
|
},
|
2020-11-07 13:19:50 +00:00
|
|
|
emits: ['click'],
|
2020-06-12 14:05:56 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-06-12 14:05:56 +00:00
|
|
|
};
|
2019-03-12 04:04:40 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-03-05 11:05:23 +00:00
|
|
|
return {
|
|
|
|
visible: false,
|
2020-10-13 07:02:35 +00:00
|
|
|
scrollEvent: null,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2020-10-28 07:31:45 +00:00
|
|
|
nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const getTarget = this.target || getDefaultTarget;
|
|
|
|
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll);
|
|
|
|
this.handleScroll();
|
|
|
|
});
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2018-03-05 11:05:23 +00:00
|
|
|
if (this.scrollEvent) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.scrollEvent.remove();
|
2018-03-05 11:05:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
getCurrentScrollTop() {
|
|
|
|
const getTarget = this.target || getDefaultTarget;
|
|
|
|
const targetNode = getTarget();
|
2018-03-05 11:05:23 +00:00
|
|
|
if (targetNode === window) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
|
2018-03-05 11:05:23 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return targetNode.scrollTop;
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
|
|
|
|
2020-10-13 07:39:08 +00:00
|
|
|
scrollToTop(e: Event) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { target = getDefaultTarget } = this;
|
|
|
|
scrollTo(0, {
|
|
|
|
getContainer: target,
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$emit('click', e);
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
handleScroll() {
|
|
|
|
const { visibilityHeight, target = getDefaultTarget } = this;
|
|
|
|
const scrollTop = getScroll(target(), true);
|
2018-03-05 11:05:23 +00:00
|
|
|
this.setState({
|
|
|
|
visible: scrollTop > visibilityHeight,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-01-18 13:34:23 +00:00
|
|
|
const { prefixCls: customizePrefixCls, $slots } = this;
|
2019-03-12 04:04:40 +00:00
|
|
|
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-12 04:04:40 +00:00
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
2020-07-25 13:57:06 +00:00
|
|
|
const classString = classNames(prefixCls, this.$attrs.class);
|
2018-03-05 11:05:23 +00:00
|
|
|
const defaultElement = (
|
|
|
|
<div class={`${prefixCls}-content`}>
|
|
|
|
<div class={`${prefixCls}-icon`} />
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-05 11:05:23 +00:00
|
|
|
const divProps = {
|
2020-07-25 13:57:06 +00:00
|
|
|
...this.$attrs,
|
2020-06-12 14:05:56 +00:00
|
|
|
onClick: this.scrollToTop,
|
2020-07-25 13:57:06 +00:00
|
|
|
class: classString,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-05 11:05:23 +00:00
|
|
|
|
|
|
|
const backTopBtn = this.visible ? (
|
2020-10-24 12:36:57 +00:00
|
|
|
<div {...divProps}>{$slots.default?.() || defaultElement}</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
) : null;
|
|
|
|
const transitionProps = getTransitionProps('fade');
|
2020-07-25 13:46:25 +00:00
|
|
|
return <Transition {...transitionProps}>{backTopBtn}</Transition>;
|
2018-03-05 11:05:23 +00:00
|
|
|
},
|
2020-10-13 07:02:35 +00:00
|
|
|
});
|
2018-03-05 11:05:23 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default withInstall(BackTop);
|