JQuery右键菜单插件 - 浏览器兼容:IE,Firefox,Chrome,

新闻动态/经验分享/SEO营销/前端技术/视觉设计/交互设计/建站知识
JQuery右键菜单插件 - 浏览器兼容:IE,Firefox,Chrome,

分类:前端技术 | 网络转载 |  时间:2013-10-11

一:浏览器兼容:IE,Firefox,Chrome,Safari,TT

二:js文件

// create by tony on 2009-04-22
// eg: $("#id").jsRightMenu({
//         menuList:[{ menuName: "右键菜单", clickEvent: "divClick('click1')"}]
//     });


jQuery.fn.extend({
    jsRightMenu: function(options) {
        options = $.extend({
            menuList: []
        }, options);
        return this.each(function() {
            if ($("#div_RightMenu", $(this)).size() == 0) {
                var menuCount = options.menuList.length;
                if (menuCount > 0) {
                    var divMenuList = "<div id=\"div_RightMenu\" class=\"div_RightMenu\">";
                    for (var i = 0; i < menuCount; i++) {
                        divMenuList += "<div class=\"divMenuItem\"  onclick=\"" + options.menuList[i].clickEvent + "\"  onmouseover=\"" + options.menuList[i].mouseoverEvent + "\" onmouseout=\"" + options.menuList[i].mouseoutEvent + "\">" + options.menuList[i].menuName + "</div>";
                    }
                    divMenuList += "</div>";
                    $(this).append(divMenuList);
                    var objM = $(".divMenuItem");
                    $("#div_RightMenu").hide();
                    objM.bind("mouseover", function() {
                        this.style.backgroundColor = "#316ac5";
                        this.style.paddingLeft = "30px";
                    });
                    objM.bind("mouseout", function() {
                        this.style.backgroundColor = '#EAEAEA';
                    });
                }
            }
            this.oncontextmenu = function() {
                var objMenu = $("#div_RightMenu");
                if (objMenu.size() > 0) {
                    objMenu.hide();
                    var event = arguments[0] || window.event;
                    var clientX = event.clientX;
                    var clientY = event.clientY;
                    var redge = document.body.clientWidth - clientX;
                    var bedge = document.body.clientHeight - clientY;
                    var menu = objMenu.get(0);
                    var menuLeft = 0;
                    var menuTop = 0;
                    if (redge < menu.offsetWidth)
                        menuLeft = document.body.scrollLeft + clientX - menu.offsetWidth;
                    else
                        menuLeft = document.body.scrollLeft + clientX;
                    if (bedge < menu.offsetHeight)
                        menuTop = document.body.scrollTop + clientY - menu.offsetHeight;
                    else
                        menuTop = document.body.scrollTop + clientY;
                    objMenu.css({ top: menuTop + "px", left: menuLeft + "px" });
                    objMenu.show();
                    return false;
                }
            }
            document.onclick = function() {
                var objMenu = $("#div_RightMenu");
                if (objMenu.size() > 0) objMenu.hide();
            }
        });
    }
});

 

三:html

<div id="divTest">右键菜单显示区域</div>

四:使用

首先去JQuery的官方网站下载最新版本:http://jquery.com/,然后在页面的<head></head>之间加入下面代码:

<script src="../js/jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function() {
$("#divTest" ).jsRightMenu({
    menuList: [{ menuName: "右键菜单1", clickEvent: "divClick('1')"},
               { menuName: "右键菜单2", clickEvent: "divClick('2')"},
               { menuName: "右键菜单3", clickEvent: "divClick('3')"},
               { menuName: "右键菜单4", clickEvent: "divClick('4')"},
               { menuName: "右键菜单5", clickEvent: "divClick('5')"}
              ]
    });

});

function divClick(id){
    alert("你点了--右键菜单"+id);
}

 </script>


Copyright © 2013 ZHIZ.NET, INC. ALL RIGHTS RESERVED. TERMS OF SERVICE & PRIVACY POLICY. 1.3.5 蜀ICP备11006209号-2 TEL/18200120015 QQ:297055170