龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

非常漂亮网页右键菜单,JQuery实现右键菜单(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
完整JS Code [http://www.xueit.com] ;( function ($) ... { function returnfalse() ... { return false ; } ; $.fn.contextmenu = function (option) ... { option = $.extend( ... { alias: " cmroot " , width

 

完整JS Code [http://www.xueit.com]
;(function($) { function returnfalse() { return false; }; $.fn.contextmenu = function(option) { option = $.extend({ alias: "cmroot", width: 150 }, option); var ruleName = null, target = null, groups = {}, mitems = {}, actions = {}, showGroups = [], itemTpl = "<div class='b-m-$[type]' unselectable=on><nobr unselectable=on><img src='$[icon]' align='absmiddle'/><span unselectable=on>$[text]</span></nobr></div>"; var gTemplet = $("<div/>").addClass("b-m-mpanel").attr("unselectable", "on").css("display", "none"); var iTemplet = $("<div/>").addClass("b-m-item").attr("unselectable", "on"); var sTemplet = $("<div/>").addClass("b-m-split"); //创建菜单组 var buildGroup = function(obj) { groups[obj.alias] = this; this.gidx = obj.alias; this.id = obj.alias; if (obj.disable) { this.disable = obj.disable; this.className = "b-m-idisable"; } $(this).width(obj.width).click(returnfalse).mousedown(returnfalse).appendTo($("body")); obj = null; return this; }; var buildItem = function(obj) { var T = this; T.title = obj.text; T.idx = obj.alias; T.gidx = obj.gidx; T.data = obj; T.innerHTML = itemTpl.replace(/\$\[([^\]] )\]/g, function() { return obj[arguments[1]]; }); if (obj.disable) { T.disable = obj.disable; T.className = "b-m-idisable"; } obj.items && (T.group = true); obj.action && (actions[obj.alias] = obj.action); mitems[obj.alias] = T; T = obj = null; return this; }; //添加菜单项 var addItems = function(gidx, items) { var tmp = null; for (var i = 0; i < items.length; i ) { if (items[i].type == "splitLine") { //菜单分隔线 tmp = sTemplet.clone()[0]; } else { items[i].gidx = gidx; if (items[i].type == "group") { //菜单组 buildGroup.apply(gTemplet.clone()[0], [items[i]]); arguments.callee(items[i].alias, items[i].items); items[i].type = "arrow"; tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]); } else { //菜单项 items[i].type = "ibody"; tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]); $(tmp).click(function(e) { if (!this.disable) { if ($.isFunction(actions[this.idx])) { actions[this.idx].call(this, target); } hideMenuPane(); } return false; }); } //Endif $(tmp).bind("contextmenu", returnfalse).hover(overItem, outItem); } //Endif groups[gidx].appendChild(tmp); tmp = items[i] = items[i].items = null; } //Endfor gidx = items = null; }; var overItem = function(e) { //如果菜单项不可用 if (this.disable) return false; hideMenuPane.call(groups[this.gidx]); //如果是菜单组 if (this.group) { var pos = $(this).offset(); var width = $(this).outerWidth(); showMenuGroup.apply(groups[this.idx], [pos, width]); } this.className = "b-m-ifocus"; return false; }; //菜单项失去焦点 var outItem = function(e) { //如果菜单项不可用 if (this.disable ) return false; if (!this.group) { //菜单项 this.className = "b-m-item"; } //Endif return false; }; //在指定位置显示指定的菜单组 var showMenuGroup = function(pos, width) { var bwidth = $("body").width(); var bheight = document.documentElement.clientHeight; var mwidth = $(this).outerWidth(); var mheight = $(this).outerHeight(); pos.left = (pos.left width mwidth > bwidth) ? (pos.left - mwidth < 0 ? 0 : pos.left - mwidth) : pos.left width; pos.top = (pos.top mheight > bheight) ? (pos.top - mheight (width > 0 ? 25 : 0) < 0 ? 0 : pos.top - mheight (width > 0 ? 25 : 0)) : pos.top; $(this).css(pos).show(); showGroups.push(this.gidx); }; //隐藏菜单组 var hideMenuPane = function() { var alias = null; for (var i = showGroups.length - 1; i >= 0; i--) { if (showGroups[i] == this.gidx) break; alias = showGroups.pop(); groups[alias].style.display = "none"; mitems[alias] && (mitems[alias].className = "b-m-item");
精彩图集

赞助商链接