1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import _extends from 'babel-runtime/helpers/extends';
import raf from 'raf';
import PropTypes from '../_util/vue-types';
import addEventListener from '../_util/Dom/addEventListener';
import getScroll from '../_util/getScroll';
import BaseMixin from '../_util/BaseMixin';
import getTransitionProps from '../_util/getTransitionProps';
var easeInOutCubic = function easeInOutCubic(t, b, c, d) {
var cc = c - b;
t /= d / 2;
if (t < 1) {
return cc / 2 * t * t * t + b;
} else {
return cc / 2 * ((t -= 2) * t * t + 2) + b;
}
};
function getDefaultTarget() {
return window;
}
var BackTopProps = {
visibilityHeight: PropTypes.number,
// onClick?: React.MouseEventHandler<any>;
target: PropTypes.func,
prefixCls: PropTypes.string
};
var BackTop = {
name: 'ABackTop',
mixins: [BaseMixin],
props: _extends({}, BackTopProps, {
visibilityHeight: PropTypes.number.def(400)
}),
data: function data() {
this.scrollEvent = null;
return {
visible: false
};
},
mounted: function mounted() {
var _this = this;
this.$nextTick(function () {
var getTarget = _this.target || getDefaultTarget;
_this.scrollEvent = addEventListener(getTarget(), 'scroll', _this.handleScroll);
_this.handleScroll();
});
},
beforeDestroy: function beforeDestroy() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
},
methods: {
getCurrentScrollTop: function getCurrentScrollTop() {
var getTarget = this.target || getDefaultTarget;
var targetNode = getTarget();
if (targetNode === window) {
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
}
return targetNode.scrollTop;
},
scrollToTop: function scrollToTop(e) {
var _this2 = this;
var scrollTop = this.getCurrentScrollTop();
var startTime = Date.now();
var frameFunc = function frameFunc() {
var timestamp = Date.now();
var time = timestamp - startTime;
_this2.setScrollTop(easeInOutCubic(time, scrollTop, 0, 450));
if (time < 450) {
raf(frameFunc);
} else {
_this2.setScrollTop(0);
}
};
raf(frameFunc);
this.$emit('click', e);
},
setScrollTop: function setScrollTop(value) {
var getTarget = this.target || getDefaultTarget;
var targetNode = getTarget();
if (targetNode === window) {
document.body.scrollTop = value;
document.documentElement.scrollTop = value;
} else {
targetNode.scrollTop = value;
}
},
handleScroll: function handleScroll() {
var visibilityHeight = this.visibilityHeight,
_target = this.target,
target = _target === undefined ? getDefaultTarget : _target;
var scrollTop = getScroll(target(), true);
this.setState({
visible: scrollTop > visibilityHeight
});
}
},
render: function render() {
var h = arguments[0];
var _prefixCls = this.prefixCls,
prefixCls = _prefixCls === undefined ? 'ant-back-top' : _prefixCls,
$slots = this.$slots,
$listeners = this.$listeners;
var defaultElement = h(
'div',
{ 'class': prefixCls + '-content' },
[h('div', { 'class': prefixCls + '-icon' })]
);
var divProps = {
on: _extends({}, $listeners, {
click: this.scrollToTop
}),
'class': prefixCls
};
var backTopBtn = this.visible ? h(
'div',
divProps,
[$slots['default'] || defaultElement]
) : null;
var transitionProps = getTransitionProps('fade');
return h(
'transition',
transitionProps,
[backTopBtn]
);
}
};
/* istanbul ignore next */
BackTop.install = function (Vue) {
Vue.component(BackTop.name, BackTop);
};
export default BackTop;