tip.js
920 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
/**
* tip
* @author chenxuan <xuan.chen@yoho.cn>
*
* @example
* tip('lalalalal~')
*
* @example
* tip({
* txt: 'lalalalal',
* delay: 2000
* })
*/
const $ = require('jquery');
function tip(param) {
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();
}, viewData.delay));
}
module.exports = tip;