使用jQuery实现右键菜单

var arr = ["新建", "复制", "剪切", "粘贴", "删除"];
var cloneDiv,selectDiv;
var ul=$("<ul></ul>").appendTo("body").css({
	listStyle: "none",
     margin: 0,
     padding: 0,
     position: "absolute",
     display: "none",
     zIndex: 999,
}).mouseleave(function(){
	$(this).css("display","none");
});
$.each(arr,function(index,item){
	$("<li></li>").appendTo(ul).css({
     	padding: "5px 15px",
        backgroundColor: "skyblue",
        userSelect: "none",
	}).text(item).hover(
		function(){
			$(this).css("backgroundColor", "blue");
        },function(){
        	$(this).css("backgroundColor", "skyblue");
        }
    ).click(function(e){
    	switch(arr.indexOf($(this).text())){
        	case 0:
                var col="#";
                for(var i=0;i<6;i++){
					col+=Math.floor(Math.random()*16).toString(16);
                }
            	$("<div></div>").appendTo("body").css({
                 	width: 50,
                    height: 50,
                    backgroundColor: "red",
                    position: "absolute",
                    left: e.clientX - 25,
                    top: e.clientY - 25,
                 }).click(function(){
                 	selectDiv=$(this);
                  	selectDiv.css("border", "1px solid #000000").mousedown(function(e){
                  		var div=$(this);
                    	e.preventDefault();
                        $(document).mousemove(function(e1){
                        	div.css({
                            	left: e1.clientX - e.offsetX,
                                top: e1.clientY - e.offsetY,
                         	});
                     	}).mouseup(function(){
                            $(this).off("mousemove mouseup");
                     	})
                 	}).siblings("div").css("border","none").off("mousedown");
             	});
                break;
            case 1:
                cloneDiv=selectDiv.clone(true);
                break;
            case 2:
                cloneDiv=selectDiv.clone(true);
                selectDiv.remove();
                selectDiv=null;
                break;
           	case 3:
                if(!cloneDiv) return;
                selectDiv=cloneDiv;
                selectDiv.appendTo("body").css({
                	left: e.clientX - 25,
                    top: e.clientY - 25,
                }).siblings("div").css("border","none");
                cloneDiv=null;
                break;
            case 4:
                selectDiv.remove();
                selectDiv=null;
                break;
                }
       ul.css("display","none");
	})
});

$(document).contextmenu(function(e){
	e.preventDefault();
    ul.css({
    	display: "block",
        left: e.clientX - 10,
        top: e.clientY - 10, 
     });
});  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值