Mouse Right click event code (asp.net background) _javascript tips

Source: Internet
Author: User
The JS code is as follows:
Copy Code code as follows:

<script type= "Text/javascript" >
Disable Right-click menu
Document.oncontextmenu=contextmenu;
right mouse click event
function ContextMenu ()
{
Create two variables as the coordinates that appear in the menu
var x;
var y;
Get menu outside Div (hereinafter referred to as panel)
var Framepanel=document.getelementbyid ("Framediv");
Get panel width and height
var panelwidth=parseint (FramePanel.style.width.replace ("px", ""));
var panelheight=parseint (FramePanel.style.height.replace ("px", ""));
Get mouse coordinates
var Mousex=event.clientx;
var Mousey=event.clienty;
Get page window width and height
var windowwidth=document.body.offsetwidth;
var windowheight=document.documentelement.offsetheight;
If the mouse x coordinates + panel width > Page window width, the left side of the panel appears
if ((mousex+panelwidth) >=windowwidth)
{
x=mousex-panelwidth-20;
}
Else
{
X=mousex;
}
If the mouse y coordinate + panel height > page window height, the panel appears above
if ((mousey+panelheight) >=windowheight)
{
y=mousey-panelheight-20;
}
Else
{
Y=mousey;
}
Display panel
Framepanel.style.left=x;
Framepanel.style.top=y;
framepanel.style.display= "Block";
Hide Level Two menu
document.getElementById ("Childdiv"). style.display= "None";
Return false;//This code is equal to Event.returnvalue=false;
}
Mouse Select the menu, parameter description: Arg for the menu Div,imageurl refers to the image address to replace, haschildmenu refers to whether the menu has a submenu, childpanelid refers to the submenu ID (if there is a submenu)
function Menuonmouseover (arg,imageurl,haschildmenu,childpanelid)
{
Replace background picture (picture path is relative path)
arg.style.backgroundimage= "url (' +imageurl+ ')";
Change div background color
Arg.style.backgroundcolor= "#1665CB";
Change font Color
Arg.style.color= "#ffffff";
If there is a submenu under the menu
if (Haschildmenu)
{
Create two variables as the coordinates that appear in the menu
var x;
var y;
Get submenu div (sub Panel for short)
var Childpanel=document.getelementbyid (childpanelid);
Debugger
Get the child panel width and height
var childpanelwidth=parseint (ChildPanel.style.width.replace ("px", ""));
var childpanelheight=parseint (ChildPanel.style.width.replace ("px", ""));
Get the Father Menu Div (hereinafter referred to as the parent panel)
var framepanel=event.srcelement;
Get parent panel width and height
var panelwidth=framepanel.offsetwidth;
var panelheight=framepanel.offsetheight;
Get mouse coordinates
var Mousex=event.clientx;
var Mousey=event.clienty;
To get the mouse's offset from the parent panel
var mouseoffsetwidth=event.offsetx;
var mouseoffsetheight=event.offsety;
Get page window width and height
var windowwidth=document.body.offsetwidth;
var windowheight=document.documentelement.offsetheight;
If the mouse x coordinates + panel width + Sub panel width > page window width, the left side of the panel displays
if ((mousex-mouseoffsetwidth+panelwidth+childpanelwidth) >=windowwidth)
{
x=mousex-mouseoffsetwidth-childpanelwidth-3;
}
Else
{
x=mousex-mouseoffsetwidth+panelwidth-5;
}
If the mouse y coordinate + panel height > page window height, the panel appears above
if ((mousey-mouseoffsetheight+childpanelheight) >=windowheight-30)
{
y=mousey-mouseoffsetheight-childpanelheight-15;
}
Else
{
y=mousey-mouseoffsetheight+5;
}
Display panel
Childpanel.style.left=x;
Childpanel.style.top=y;
childpanel.style.display= "Block";
}
}
Mouse away from the menu
function Menuonmouseout (arg,imageurl,haschildmenu,childpanelid)
{
if (Haschildmenu)
{
Submenu Div (hereafter referred to as Subpanel)
var Childpanel=document.getelementbyid (childpanelid);
var toelement=event.toelement;
Notice here that the mouse may be moved to the descendants Div of the child panel, at this time event. Toelement DOM element!=childpanel (Subpanel), event. Toelement.parentnode=childpanel,
if (toelement==childpanel| | Toelement.parentnode==childpanel)
{
childpanel.style.display= "Block";
}
Else
{
Childpanel.style.display= "None";
}
}
Replace background picture (picture path is relative path)
arg.style.backgroundimage= "url (' +imageurl+ ')";
Change div background color
Arg.style.backgroundcolor= "#ffffff";
Change font Color
Arg.style.color= "#000000";
}
</script>

The page ASPX code is as follows:
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "ContextMenu.aspx.cs" inherits= "ContextMenu"%>
<%@ Register assembly= "AjaxControlToolkit" namespace= "AjaxControlToolkit" tagprefix= "CC1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<link href= "Css/stylesheet.css" rel= "StyleSheet" type= "Text/css"/>

<body>
<form id= "Form1" runat= "Server" >
<asp:scriptmanager id= "ScriptManager1" runat= "Server" >
</asp:ScriptManager>
<!--context Menu level menu-->
<div id= "Framediv" runat= "style=" width:170px; height:300px; border-style:solid;border-width:1px; Border-color: #A09F9F; Background-color:white; Display:none; position:relative; padding:1px 1px 1px 1px; " >
<!--files Common menu-->
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/menu1_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/menu1.jpg ') "style=" Background-image:url (' images/menu1.jpg ') "onclick=" Oncopyfoldermenuclcik () ">
Create a new Folder
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/menu2_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/menu2.jpg ') "style=" Background-image:url (' images/menu2.jpg ') ">
Search New Folder
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/menu3_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/menu3.jpg ') "style=" Background-image:url (' images/menu3.jpg ') ">
Delete this folder
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/menu4_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/menu4.jpg ') "style=" Background-image:url (' images/menu4.jpg ') ">
Delete all
</div>
<!--very menu-->
&LT;HR style= "color: #A09F9F; width:160px; height:1px; " />
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/menu5_click.jpg ', ' true ', ' ChildDiv ') Onmouseout= "Menuonmouseout" (This, ' images/menu5.jpg ', ' true ', ' Childdiv ') "style=" Background-image:url (' Images/ Menu5.jpg ') >
sorting files
</div>
</div>
<cc1:dropshadowextender id= "DropShadowExtender1" runat= "Server" targetcontrolid= "Framediv" width= "2" Trackposition= "true" opacity= "0.3" >
</cc1:DropShadowExtender>
<!--context Menu level two menu-->
<div id= "Childdiv" runat= "style=" width:170px; height:200px; Z-index:2; Background-color:white;border-style:solid; border-width:1px; Border-color: #A09F9F; Display:none; Position:absolute; padding:1px 1px 1px 1px; " >
<!--files Common menu-->
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/child_menu1_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/child_menu1.jpg ') "style=" Background-image:url (' images/child_menu1.jpg ') ">
Name
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/child_menu2_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/child_menu2.jpg ') "style=" Background-image:url (' images/child_menu2.jpg ') ">
Creation time
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/child_menu3_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/child_menu3.jpg ') "style=" Background-image:url (' images/child_menu3.jpg ') ">
Type
</div>
<div class= "Menudivdefault" onmouseover= "Menuonmouseover" (This, ' images/child_menu4_click.jpg ') "onmouseout=" Menuonmouseout (This, ' images/child_menu4.jpg ') "style=" Background-image:url (' images/child_menu4.jpg ') ">
Size
</div>
</div>
<cc1:dropshadowextender id= "DropShadowExtender2" runat= "Server" targetcontrolid= "Childdiv" width= "2" Trackposition= "true" opacity= "0.3" >
</cc1:DropShadowExtender>
</form>
</body>



Pay attention to this sentence:
<cc1:dropshadowextender id= "DropShadowExtender2" runat= "Server" targetcontrolid= "Childdiv" width= "2" Trackposition= "true" opacity= "0.3" >
</cc1:DropShadowExtender>
This control is a control that implements a shadow effect in asp.net AJAX1.0, so to add a control registration to the ASPX page:
<%@ Register assembly= "AjaxControlToolkit" namespace= "AjaxControlToolkit" tagprefix= "CC1"%>
Control specific property meaning please check Baidu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.