-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu-open.js
50 lines (44 loc) · 1.13 KB
/
menu-open.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
// Author: Remy Sharp / @rem
// Demo: http://jsbin.com/3/ideris/7/edit?javascript,live
var heading = document.querySelector('h2'),
container = heading.parentNode,
downon = null,
items = [].slice.call(container.getElementsByTagName('a'));
function removeHover() {
var i = items.length;
while (i--) {
items[i].className = '';
}
}
heading.onmousedown = function (e) {
e.preventDefault();
var className = container.className;
container.className = className === 'open' ? '' : 'open';
};
container.onmouseup = function (e) {
// you should close any open dropdowns now...
if (downon !== e.target) {
if (e.target.nodeName === 'A') {
if (e.target.getAttribute('target')) {
window.open(e.target.href);
} else {
window.location = e.target.href;
}
}
}
};
container.onmousedown = function (e) {
downon = e.target;
};
document.documentElement.onmouseup = function (e) {
if (e.target !== heading) {
removeHover();
container.className = '';
}
}
container.onmousemove = function (e) {
removeHover();
if (e.target.nodeName == 'A') {
e.target.className = 'hover';
}
};