tips.js
1.4 KB
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
/**
* Created by TaoHuang on 2016/7/1.
*/
var jQuery = require('yoho-jquery'),
Handlebars = require('yoho-handlebars');
((function($) {
var tpl = '<div class="tips hide">' +
'<div class="triangle"></div>' +
'<div class="rectangle"> {{content}}</div>' +
'</div>';
var tplFn = Handlebars.compile(tpl);
var methods = {
show: function() {
var $this = this,
$tips = $this.next('.tips');
if ($tips.length !== 0) {
$tips.find('.rectangle').text(arguments[0]).removeClass('hide');
} else {
$this.after(tplFn({content: arguments[0]}));
$tips = $this.next('.tips').removeClass('hide');
$tips.css({
top: $this.height() - 2,
left: 0,
width: $this.width() + 2,
height: $this.height
});
}
return $tips;
},
hide: function() {
var $tips = this.next('.tips');
return $tips.addClass('hide');
}
};
$.fn.tips = function(method) {
// 方法调用
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
$.error('Method' + method + 'does not exist on jQuery.tooltip');
}
};
})(jQuery));