Home > Web Front-end > JS Tutorial > Simulate the context menu effect code of the windows platform_javascript skills

Simulate the context menu effect code of the windows platform_javascript skills

WBOY
Release: 2016-05-16 18:09:53
Original
1051 people have browsed it

Code demo:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Structure: The code is as follows:

The code is like this: The code is as follows:

function $(id) {
return document.getElementById(id);
};

var EventUnit = {
addHandler: function(element, type, handler) {//添加事件处理程序
if(element.addEventListener) {
element.addEventListener(type, handler) , 거짓);
} else if(element.attachEvent) {
element.attachEvent('on' 유형, 핸들러);
} else {
요소['on' 유형] = 핸들러;
};
},
getEvent: function(event) {
return event ? 이벤트 : window.event;
},
preventDefault: function(event) {//取消事件默认动작
if(event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
};
}
}

EventUnit.addHandler(window, 'load', function() {
var Wrap = $('wrap');
var menu = $(' menu');
var menuStyle = menu.style.display;
var x = Wrap.offsetLeft Wrap.clientWidth, y = Wrap.offsetTop Wrap.clientHeight;
var w = 0, h = 0;
var left = 0, top = 0;

EventUnit.addHandler(wrap, 'contextmenu', function(event) {
event = EventUnit.getEvent(event);
EventUnit. PreventDefault(event);

menu.style.display = 'block';
w = menu.clientWidth;
h = menu.clientHeight; >= w) ? event.clientX - Wrap.offsetLeft : event.clientX - Wrap.offsetLeft - w;
top = (event.clientY h <= y) ? event.clientY - Wrap.offsetTop : 이벤트. clientY - Wrap.offsetTop - h;
menu.style.left = 왼쪽 'px';
menu.style.top = 상단 'px'
}); addHandler(document, 'click', function() {
menu.style.display = menuStyle;
})
});

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template