ntab.js
4.75 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
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
146
/**
* TAB 功能,同样适用于导航结构
*
*/
define('ntab',function(require, exports, module){
// $.fn.ntab = function(a) {
// this.each(function() {
// new ntab(this, a)
// })
// };
var ntab=function(a, b) {
this.target = a;
this.opt = {
handler: null,
eventType: "hover",
hoverActive: true,
currentClass: "pdo-ntab_current",
hideDelay: 120,
index: 0,
isAnimate: true,
onShow: function() {},
onHide: function() {}
};
$.extend(this.opt, b);
this.init()
}
ntab.prototype = {
init: function() {
var b = this;
this.H = $(this.target).find(this.opt.handler);
this.H.each(function(i) {
var a = $(this).attr("data-href") ? $(this).attr("data-href") : $(this).attr("href"),
cont = $(a);
this.C = a && cont.length ? cont: $(this).next();
if (this.C && this.C.length) {
this.C[0].H = this;
if (b.opt.index !== i) {
$(this.C).hide()
} else {
b.show(this)
}
}
});
this.bind()
},
bind: function() {
var b = this,
stimer = htimer = null;
if (/click/.test(this.opt.eventType)) {
this.H[this.opt.eventType](function(a) {
delayShow(this);
a.preventDefault()
})
} else {
this.H[this.opt.eventType](function() {
delayShow(this)
},
function() {
//delayHide(this)
});
$(this.target)[this.opt.eventType](function() {
clearTimeout(htimer)
},
function() {
//delayShow(null)
})
}
if (/hover/.test(this.opt.eventType) && this.opt.hoverActive) {
this.H.each(function() {
if (!this.C) return;
$(this.C)[b.opt.eventType](function() {
clearTimeout(htimer)
},
function() {
// delayHide(this.H)
})
})
}
$(".ntab-handler").each(function() {
if (!this._bind) {
$(this).click(function() {
var a = this,
relato = $(this).attr("relato"),
rela_elems = $(relato),
orgi_target = null;
rela_elems.each(function() {
if ($(this).attr("href") == $(a).attr("href")) {
orgi_target = this
}
});
$(orgi_target).click();
return false
});
this._bind = true
}
});
function delayShow(a) {
clearTimeout(stimer);
clearTimeout(htimer);
stimer = setTimeout(function() {
b.show(a)
},
b.opt.hideDelay)
}
function delayHide(a) {
clearTimeout(stimer);
clearTimeout(htimer);
htimer = setTimeout(function() {
b.hide(a)
},
b.opt.hideDelay)
}
},
show: function(a) {
var b = this;
this.H.each(function() {
$(this)[this === a && a.C && a.C.length ? "addClass": "removeClass"](b.opt.currentClass);
if (this === a) {
if (this.C) {
$(this.C)[b.opt.isAnimate ? "fadeIn": "show"](b.opt.isAnimate ? 200 : 0)
}
b.opt.onShow.call(this, this.C)
} else {
if (!/click/.test(b.opt.eventType)) {
$(this.C)[b.opt.isAnimate ? "fadeOut": "hide"](b.opt.isAnimate ? 200 : 0)
} else {
$(this.C).hide()
}
}
})
},
hide: function(a) {
$(a).removeClass(this.opt.currentClass);
if (/click/.test(this.opt.eventType)) {
$(a.C).hide()
} else {
$(a.C).fadeOut(200)
}
if (!a) {
_this.show()
}
}
};
return ntab;
});