back-top.js
823 Bytes
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
/**
* back to top
* @author qi.li <qi.li@yoho.cn>
*
*/
const $ = require('jquery');
(function backTop() {
const $doc = $(document);
const $win = $(window);
let dom;
let $btn;
let show;
dom = `<div class="back-top">
<i class="icon icon-back-top1"></i>
</div>`;
$doc.find('body').append(dom);
$btn = $('.back-top');
$btn.click(function() {
$('html,body').animate({scrollTop: 0}, 500);
});
$doc.scroll(function() {
let top = $(this).scrollTop();
if (top > $win.height()) {
if (!show) {
$btn.fadeIn('slow');
show = true;
}
} else {
if (show) {
$btn.fadeOut('slow');
show = false;
}
}
});
}());