非常漂亮网页右键菜单,JQuery实现右键菜单(3)
01.
//获取菜单的跟
02.
var
$root = $(
"#"
+ option.alias);
03.
var
root =
null
;
04.
if
($root.length == 0) {
//如果顶级不存在,这创建顶级菜单哦
05.
root = buildGroup.apply(gTemplet.clone()[0], [option]);
06.
root.applyrule = applyRule;
//把一个方法注册到dom上
07.
root.showMenu = showMenu;
//另外一个方法注册的该dom上
08.
addItems(option.alias, option.items);
//添加菜单项
09.
}
10.
else
{
11.
root = $root[0];
//否则就用这个了
12.
}
这个代码很玄乎,好像做了些什么好像有什么都没做,其实只有来看下buildGroup方法和addItems才知道到底干了什么
01.
var
buildGroup =
function
(obj) {
//创建菜单容器
02.
groups[obj.alias] =
this
;
//菜单项注册到临时变量中
03.
this
.gidx = obj.alias;
04.
this
.id = obj.alias;
05.
if
(obj.disable) {
//如果是禁用状态
06.
this
.disable = obj.disable;
07.
this
.className =
"b-m-idisable"
;
08.
}
09.
//设置菜单宽度,设置事件的阻止事件冒泡,并添加到body中
10.
$(
this
).width(obj.width).click(returnfalse).mousedown(returnfalse).appendTo($(
"body"
));
11.
obj =
null
;
12.
return
this
;
//返回菜单本身
13.
};
有了容器就可以往里面添加菜单项了,我在代码中加了详细的注释了,应该可以很好的理解了
01.
//添加菜单项
02.
var
addItems =
function
(gidx, items) {
03.
var
tmp =
null
;
04.
for
(
var
i = 0; i < items.length; i++) {
05.
if
(items[i].type ==
"splitLine"
) {
//如果是分割线
06.
//菜单分隔线
07.
tmp = sTemplet.clone()[0];
08.
}
else
{
09.
items[i].gidx = gidx;
//把group的标识赋给item上
10.
if
(items[i].type ==
"group"
) {
11.
//菜单组
12.
buildGroup.apply(gTemplet.clone()[0], [items[i]]);
//每个菜单组都是独立的div哦,所以顶级一样调用生产组的方法
13.
arguments.callee(items[i].alias, items[i].items);
//递归生成菜单项
14.
items[i].type =
"arrow"
;
//如果是group生成箭头
15.
tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
//生成菜单项的html
16.
}
else
{
17.
//菜单项
18.
items[i].type =
"ibody"
;
19.
tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
//生成菜单项的html
20.
$(tmp).click(
function
(e) {
//如果菜单项那么注册click事件
21.
if
(!
this
.disable) {
22.
if
($.isFunction(actions[
this
.idx])) {
23.
actions[
this
.idx].call(
this
, target);
24.
}
25.
hideMenuPane();
26.
}
27.
return
false
;
28.
});
29.
}
//Endif
30.
//把菜单项的右键事件屏蔽,同时注册hover的效果
31.
$(tmp).bind(
"contextmenu"
, returnfalse).hover(overItem, outItem);
32.
}
//Endif
33.
groups[gidx].appendChild(tmp);
//把菜单项添加到group的中
34.
tmp = items[i] = items[i].items =
null
;
35.
}
//Endfor
36.
gidx = items =
null
;
37.
};
builditem方法就比较简单,就不详细描述了,接着我们还是继续往下看主流程了哦
01.
02.
var
me = $(
this
).each(
function
() {
03.
//给元素添加右键事件了哦
04.
return
$(
this
).bind(
'contextmenu'
,
function
(e) {
05.
//如果(option.onContextMenu 存在则调用并判断返回值是否显示菜单,可以利用这个在特定情况下禁用菜单
06.
var
bShowContext = (option.onContextMenu && $.isFunction(option.onContextMenu)) ? option.onContextMenu.call(
this
, e) :
true
;
07.
if
(bShowContext) {
08.
//触发onShow事件,这个事件中可以执行修改rule,禁用某几项菜单项哦
09.
if
(option.onShow && $.isFunction(option.onShow)) {
10.
option.onShow.call(
this
, root);
11.
}
12.
root.showMenu(e,
this
);
//调用显示菜单
13.
}
14.
//阻止冒泡
15.
return
false
;
16.
});
17.
});
18.
//设置显示规则,第一次执行时的规则,同时也可以onshow中动态设置rule
19.
if
(option.rule) {
20.
applyRule(option.rule);
21.
}
基本就OK了,另外几个方法就比较简单了,还有亮点是边缘的处理,这个前面的datepicker中也有相应的说明逻辑差不多就不在描述了,同样还是来看下demo吧。关于打包下载,大家可以把demo的网页完整的另存为即可
http://jscs.cloudapp.net/ControlsSample/CM
编辑推荐Jquery文章,希望你喜欢:
JS优秀框架:jQuery最新版下载地址及jquery官方
http://www.xueit.com/html/2009-03/33_992_00.html
使用JQUERY仿GOOGLE自动完成插件
http://www.xueit.com/html/2009-03/33_874_00.html
实例JQuery+Ajax实现无刷新数据查询(仿Google/百度搜索框)
http://www.xueit.com/html/2009-05/33_1796_00.html
JQuery选项卡插件推荐:17个引人注目JQuery导航菜单
http://www.xueit.com/html/2009-09/2_4447_00.html
JQuery插件大全下载,共有240个JQuery插件哦
http://www.xueit.com/html/2009-09/33_4506_00.html
jquery仿百度图片幻灯浏览功能
http://www.xueit.com/html/2009-09/33_4573_00.html
jQuery投票插件:jQuery实现投票系统显示结果插件
http://www.xueit.com/html/2009-11-10/33-1250974127921.html
不可不看20个超级酷Jquery实用效果例子
http://www.xueit.com/html/2009-11-25/33-1484914632500.html
非常漂亮网页右键菜单,JQuery实现右键菜单
http://www.xueit.com/html/2009-10-30/33-19177319171.html
推荐10个jquery动画菜单 十分漂亮的Jquery菜单插件
http://www.xueit.com/html/2009-11-20/33-2102769523921.html