File tree 2 files changed +77
-0
lines changed
2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > 自定义网页右键菜单</ title >
6
+ < style >
7
+ ul {
8
+ list-style : none;
9
+ margin : 0 ;
10
+ padding : 0 ;
11
+ }
12
+ .container {
13
+ width : 1200px ;
14
+ height : 600px ;
15
+ border-radius : 4px ;
16
+ border : 2px solid # ccc ;
17
+ margin : 0 auto;
18
+ box-shadow : 2px 2px 4px rgba (0 , 0 , 0 , .2 ) inset;
19
+ font-size : 42px ;
20
+ font-weight : bold;
21
+ text-align : center;
22
+ }
23
+ .menu {
24
+ width : 240px ;
25
+ font-size : 24px ;
26
+ line-height : 40px ;
27
+ border : 2px solid # ccc ;
28
+ border-radius : 3px ;
29
+ box-shadow : 4px 4px 6px rgba (0 , 0 , 0 , .2 );
30
+ display : none;
31
+ position : absolute;
32
+ top : 0 ;
33
+ left : 0 ;
34
+ /*z-index: 2;*/
35
+ background : # fff ;
36
+ }
37
+ .menu li {
38
+ padding-left : 20px ;
39
+ }
40
+ .menu li : hover {
41
+ background : # 1E90FF ;
42
+ }
43
+ </ style >
44
+ </ head >
45
+ < body >
46
+ < div id ="container " class ="container "> right click to see the menu</ div >
47
+ < div class ="menu " id ="menu ">
48
+ < ul >
49
+ < li > HTML</ li >
50
+ < li > CSS</ li >
51
+ < li > JavaScript</ li >
52
+ < li > HTML5</ li >
53
+ < li > CSS3</ li >
54
+ </ ul >
55
+ </ div >
56
+ < script >
57
+ var menu = document . getElementById ( 'menu' ) ,
58
+ container = document . getElementById ( 'container' ) ;
59
+ container . oncontextmenu = function ( event ) {
60
+ var e = event || document . event ;
61
+ e . returnValue = false ; //阻止默认的鼠标右键行为
62
+ var x = e . clientX ;
63
+ var y = e . clientY ;
64
+ menu . style . top = y + "px" ;
65
+ menu . style . left = x + "px" ;
66
+ menu . style . display = "block" ;
67
+ var width_x = container . offsetHeight ;
68
+ var height_y = window . innerHeight ;
69
+ console . log ( x ) ;
70
+ console . log ( y ) ;
71
+ }
72
+ document . onclick = function ( ) {
73
+ menu . style . display = "none" ;
74
+ }
75
+ </ script >
76
+ </ body >
77
+ </ html >
You can’t perform that action at this time.
0 commit comments