jquery-smartMenu.js
4.62 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
147
148
149
150
151
152
153
154
155
156
157
/*
* smartMenu.js 智能上下文菜单插件
* http://www.zhangxinxu.com/
*
* Copyright 2011, zhangxinxu
*
* 2011-05-26 v1.0 编写
* 2011-06-03 v1.1 修复func中this失准问题
* 2011-10-10 v1.2 修复脚本放在<head>标签中层无法隐藏的问题
* 2011-10-30 v1.3 修复IE6~7下二级菜单移到第二项隐藏的问题
*/
(function($) {
var D = $(document).data("func", {});
$.smartMenu = $.noop;
$.fn.smartMenu = function(data, options) {
var B = $("body"), defaults = {
name: "",
offsetX: 2,
offsetY: 2,
textLimit: 6,
beforeShow: $.noop,
afterShow: $.noop
};
var params = $.extend(defaults, options || {});
var htmlCreateMenu = function(datum) {
var dataMenu = datum || data, nameMenu = datum? Math.random().toString(): params.name, htmlMenu = "", htmlCorner = "", clKey = "smart_menu_";
if ($.isArray(dataMenu) && dataMenu.length) {
htmlMenu = '<div id="smartMenu_'+ nameMenu +'" class="'+ clKey +'box">' +
'<div class="'+ clKey +'body">' +
'<ul class="'+ clKey +'ul">';
$.each(dataMenu, function(i, arr) {
if (i) {
htmlMenu = htmlMenu + '<li class="'+ clKey +'li_separate"> </li>';
}
if ($.isArray(arr)) {
$.each(arr, function(j, obj) {
var text = obj.text, htmlMenuLi = "", strTitle = "", rand = Math.random().toString().replace(".", "");
if (text) {
if (text.length > params.textLimit) {
text = text.slice(0, params.textLimit) + "…";
strTitle = ' title="'+ obj.text +'"';
}
if ($.isArray(obj.data) && obj.data.length) {
htmlMenuLi = '<li class="'+ clKey +'li" data-hover="true">' + htmlCreateMenu(obj.data) +
'<a href="javascript:" class="'+ clKey +'a"'+ strTitle +' data-key="'+ rand +'"><i class="'+ clKey +'triangle"></i>'+ text +'</a>' +
'</li>';
} else {
htmlMenuLi = '<li class="'+ clKey +'li">' +
'<a href="javascript:" class="'+ clKey +'a"'+ strTitle +' data-key="'+ rand +'">'+ text +'</a>' +
'</li>';
}
htmlMenu += htmlMenuLi;
var objFunc = D.data("func");
objFunc[rand] = obj.func;
D.data("func", objFunc);
}
});
}
});
htmlMenu = htmlMenu + '</ul>' +
'</div>' +
'</div>';
}
return htmlMenu;
}, funSmartMenu = function() {
var idKey = "#smartMenu_", clKey = "smart_menu_", jqueryMenu = $(idKey + params.name);
if (!jqueryMenu.size()) {
$("body").append(htmlCreateMenu());
//事件
$(idKey + params.name +" a").bind("click", function() {
var key = $(this).attr("data-key"),
callback = D.data("func")[key];
if ($.isFunction(callback)) {
callback.call(D.data("trigger"));
}
$.smartMenu.hide();
return false;
});
$(idKey + params.name +" li").each(function() {
var isHover = $(this).attr("data-hover"), clHover = clKey + "li_hover";
$(this).hover(function() {
var jqueryHover = $(this).siblings("." + clHover);
jqueryHover.removeClass(clHover).children("."+ clKey +"box").hide();
jqueryHover.children("."+ clKey +"a").removeClass(clKey +"a_hover");
if (isHover) {
$(this).addClass(clHover).children("."+ clKey +"box").show();
$(this).children("."+ clKey +"a").addClass(clKey +"a_hover");
}
});
});
return $(idKey + params.name);
}
return jqueryMenu;
};
$(this).each(function() {
this.oncontextmenu = function(e) {
//回调
if ($.isFunction(params.beforeShow)) {
params.beforeShow.call(this);
}
e = e || window.event;
//阻止冒泡
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
//隐藏当前上下文菜单,确保页面上一次只有一个上下文菜单
$.smartMenu.hide();
var st = D.scrollTop();
var jqueryMenu = funSmartMenu();
if (jqueryMenu) {
jqueryMenu.css({
display: "block",
left: e.clientX + params.offsetX,
top: e.clientY + st + params.offsetY
});
D.data("target", jqueryMenu);
D.data("trigger", this);
//回调
if ($.isFunction(params.afterShow)) {
params.afterShow.call(this);
}
return false;
}
};
});
if (!B.data("bind")) {
B.bind("click", $.smartMenu.hide).data("bind", true);
}
};
$.extend($.smartMenu, {
hide: function() {
var target = D.data("target");
if (target && target.css("display") === "block") {
target.hide();
}
},
remove: function() {
var target = D.data("target");
if (target) {
target.remove();
}
}
});
})(jQuery);