模拟windows平台的上下文菜单效果代码_javascript技巧
程序员文章站
2022-04-19 12:49:11
...
代码演示:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
结构:
code是这样:
function $(id) {
return document.getElementById(id);
};
var EventUnit = {
addHandler: function(element, type, handler) {//添加事件处理程序
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else if(element.attachEvent) {
element.attachEvent('on' + type, handler);
} else {
element['on' + type] = handler;
};
},
getEvent: function(event) {
return event ? 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;
left = (x - event.clientX >= w) ? event.clientX - wrap.offsetLeft : event.clientX - wrap.offsetLeft - w;
top = (event.clientY + h menu.style.left = left + 'px';
menu.style.top = top + 'px';
});
EventUnit.addHandler(document, 'click', function() {
menu.style.display = menuStyle;
});
});
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
结构:
复制代码 代码如下:
code是这样:
复制代码 代码如下:
function $(id) {
return document.getElementById(id);
};
var EventUnit = {
addHandler: function(element, type, handler) {//添加事件处理程序
if(element.addEventListener) {
element.addEventListener(type, handler, false);
} else if(element.attachEvent) {
element.attachEvent('on' + type, handler);
} else {
element['on' + type] = handler;
};
},
getEvent: function(event) {
return event ? 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;
left = (x - event.clientX >= w) ? event.clientX - wrap.offsetLeft : event.clientX - wrap.offsetLeft - w;
top = (event.clientY + h menu.style.left = left + 'px';
menu.style.top = top + 'px';
});
EventUnit.addHandler(document, 'click', function() {
menu.style.display = menuStyle;
});
});
上一篇: php getcwd()函数获取当前文件所在目录详解
下一篇: vue2.0获取JS Bin值
推荐阅读
-
js实现改进的仿蓝色论坛导航菜单效果代码_javascript技巧
-
模拟windows平台的上下文菜单效果代码_javascript技巧
-
JS模拟的腾讯微博app撕纸效果的实例代码_javascript技巧
-
JS基于Mootools实现的个性菜单效果代码_javascript技巧
-
js实现的早期滑动门菜单效果代码_javascript技巧
-
js实现改进的仿蓝色论坛导航菜单效果代码_javascript技巧
-
JS+CSS实现的竖向简洁折叠菜单效果代码_javascript技巧
-
js+CSS实现模拟华丽的select控件下拉菜单效果_javascript技巧
-
js实现的动画导航菜单效果代码_javascript技巧
-
JS模拟的腾讯微博app撕纸效果的实例代码_javascript技巧