tip.js
1006 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
43
44
45
46
47
48
49
50
51
52
/**
* tip
* @author chenxuan <xuan.chen@yoho.cn>
*
* @example
* tip('lalalalal~')
*
* @example
* tip({
* txt: 'lalalalal',
* delay: 2000
* })
*/
import $ from 'jquery';
function tip(param, cb) {
let timer;
const viewData = {
mask: false,
delay: 2000,
txt: ''
};
if (typeof param === 'string') {
Object.assign(viewData, {
txt: param
});
} else {
Object.assign(viewData, param);
}
const tipView = `<div class="tip-box"><div class="tip">${viewData.txt}</div></div>`;
const $oldTip = $('.tip-box');
if ($oldTip.length) {
timer = $oldTip.data('timer');
clearTimeout(timer);
$oldTip.remove();
}
const $tip = $(tipView).appendTo(document.body);
$tip.data('timer', setTimeout(function() {
$tip.remove();
if (typeof cb === 'function') {
cb();//eslint-disable-line
}
}, viewData.delay));
}
export default tip;