This script allows you to build a pie menu. It uses unordered lists (<ul>) to define a menu, which means that not only is it easy to configure, but it will ‘gracefully degrade’ in older browsers!
Source code for index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>My project</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="/webtoolkit.piemenu.js"></script>
<script type="text/javascript">
PieContextMenu.attach('container', 'menu1');
</script>
</head>
<body>
<ul id="menu1" class="PieContextMenu" style="display: none;">
<li><a href="#"><img src="/icon1.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon2.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon3.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon4.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon5.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon6.gif" alt="" /></a></li>
<li><a href="#"><img src="/icon7.gif" alt="" /></a></li>
</ul>
<div class="container">Cointainer1</div>
</body>
</html>
|
Source code for webtoolkit.piemenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/**
*
* Javascript pie menu
* http://www.webtoolkit.info/
*
**/
var PieContextMenu = {
// private properties.
itemWidth : 34,
itemHeight : 34,
menuClassName : 'PieContextMenu',
menus : new Array,
menuElement : null,
menuElementId : null,
// private properties. Browser detect. Do not touch! :)
IE : ( document.all && document.getElementById && !window.opera ),
FF : (!document.all && document.getElementById && !window.opera),
// private method. Initialize
init : function () {
if ( PieContextMenu.FF ) {
document.addEventListener("DOMContentLoaded", PieContextMenu.domReady, false);
}
if ( PieContextMenu.IE ) {
document.write("<scr" + "ipt id=__ieinit_PieContextMenu defer=true " +
"src=//:><\/script>");
var script = document.getElementById("__ieinit_PieContextMenu");
script.onreadystatechange = function() {
if ( this.readyState != "complete" ) return;
this.parentNode.removeChild( this );
PieContextMenu.domReady();
};
script = null;
}
},
// private method. Initialize all menus.
domReady : function () {
var menus = document.body.getElementsByTagName('ul');
for(var i = 0; i < menus.length; i++) {
var className = menus[i].className.replace(/^\s+/g, "").replace(/\s+$/g, "");
var classArray = className.split(/[ ]+/g);
for (var k = 0; k < classArray.length; k++) {
if (classArray[k] == PieContextMenu.menuClassName && menus[i].id) {
PieContextMenu.initMenu(menus[i].id);
}
}
}
PieContextMenu.initEvents();
},
// private method.
initMenu : function (menuId) {
var list = document.getElementById(menuId);
list.style.position = "absolute";
list.style.margin = "0px";
list.style.padding = "0px";
list.style.listStyleType = "none";
var items = list.getElementsByTagName('li');
var images = list.getElementsByTagName('img');
for(var i=0; i<images.length; i++) {
images[i].style.border = "0px";
if (document.all && document.getElementById && !window.opera) {
images[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/icon_bg.png", sizingMethod="crop")';
} else {
images[i].style.background = "url(/icon_bg.png)";
}
}
var step = (2.0 * Math.PI) / (1.0 * (items.length));
var rotation = (90 / 180) * Math.PI;
var radius = Math.sqrt(this.itemWidth) * Math.sqrt(items.length) * 2.5;
for(var i=0; i<items.length; i++) {
var x = Math.cos(i*step - rotation) * radius - (this.itemWidth / 2);
var y = Math.sin(i*step - rotation) * radius - (this.itemHeight / 2);
items[i].style.position = "absolute";
items[i].style.display = "block";
items[i].style.fontSize = "0px";
items[i].style.left = Math.round(x) + "px";
items[i].style.top = Math.round(y) + "px";
}
},
// public method. Attaches context menus to specific class names
attach : function (classNames, menuId) {
if (typeof(classNames) == "string") {
PieContextMenu.menus[classNames] = menuId;
}
if (typeof(classNames) == "object") {
for (x = 0; x < classNames.length; x++) {
PieContextMenu.menus[classNames[x]] = menuId;
}
}
},
// public method. Sets up events
initEvents : function () {
if ( PieContextMenu.IE || PieContextMenu.FF ) {
document.oncontextmenu = PieContextMenu.show;
document.onclick = PieContextMenu.hide;
}
},
// private method. Shows context menu
show : function (e) {
PieContextMenu.hide();
var menuElementId = PieContextMenu.getMenuElementId(e);
if (menuElementId) {
var m = PieContextMenu.getMousePosition(e);
var s = PieContextMenu.getScrollPosition(e);
PieContextMenu.menuElement = document.getElementById(menuElementId);
PieContextMenu.menuElement.style.left = m.x + s.x + 'px';
PieContextMenu.menuElement.style.top = m.y + s.y + 'px';
PieContextMenu.menuElement.style.display = 'block';
return false;
}
return false;
},
// private method. Hides context menu
hide : function () {
if (PieContextMenu.menuElement) {
PieContextMenu.menuElement.style.display = 'none';
PieContextMenu.menuElement = null;
}
},
// private method. Get which context menu to show
getMenuElementId : function (e) {
if (PieContextMenu.IE) {
PieContextMenu.attachedElement = event.srcElement;
} else {
PieContextMenu.attachedElement = e.target;
}
while(PieContextMenu.attachedElement != null) {
var className = PieContextMenu.attachedElement.className;
if (typeof(className) != "undefined") {
className = className.replace(/^\s+/g, "").replace(/\s+$/g, "")
var classArray = className.split(/[ ]+/g);
for (i = 0; i < classArray.length; i++) {
if (PieContextMenu.menus[classArray[i]]) {
return PieContextMenu.menus[classArray[i]];
}
}
}
if (PieContextMenu.IE) {
PieContextMenu.attachedElement = PieContextMenu.attachedElement.parentElement;
} else {
PieContextMenu.attachedElement = PieContextMenu.attachedElement.parentNode;
}
}
return null;
},
// private method. Returns mouse position
getMousePosition : function (e) {
e = e ? e : window.event;
var position = {
'x' : e.clientX,
'y' : e.clientY
}
return position;
},
// private method. Get document scroll position
getScrollPosition : function () {
var x = 0;
var y = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
x = window.pageXOffset;
y = window.pageYOffset;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
x = document.body.scrollLeft;
y = document.body.scrollTop;
}
var position = {
'x' : x,
'y' : y
}
return position;
}
}
PieContextMenu.init();
|