首页 技术 正文
技术 2022年11月14日
0 收藏 696 点赞 3,938 浏览 1469 个字

javascript自定义浏览器右键菜单

 

在书上看到document对象还有一个contextmenu事件,但是不知为什么w3school中找不到这个耶。。。

利用这个特性写了个浏览器的右键菜单,感觉挺不错,呵呵。。。

HTML部分(在<body></body>之间插入以下HTML):

查看代码

javascript自定义浏览器右键菜单

<div id="menu">
<ul>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 1</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 2</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 3</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 4</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 5</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >contextmenuitem 6</a></li>
</ul>
</div>

javascript自定义浏览器右键菜单

CSS部分:

查看代码

javascript自定义浏览器右键菜单

<style type="text/css">
*{margin:;padding:;}
#menu{position:absolute;width:150px;visibility:hidden;border:1px solid #666;border-bottom-width:;}
#menu li{list-style:none;text-indent:1em;}
#menu li a{display:block;height:30px;line-height:30px;border-bottom:1px solid #666;text-decoration:none;color:#666;font:12px/30px tahoma;}
#menu li a:hover{background:#eee;color:black;}
</style>

javascript自定义浏览器右键菜单

JavaScript部分:

查看代码

javascript自定义浏览器右键菜单

<script type="text/javascript">
document.oncontextmenu = function(e){
if(window.event) e = window.event;
var mymenu = document.getElementById("menu");
mymenu.style.visibility = "visible";
mymenu.style.left = e.clientX + 5 +"px";
mymenu.style.top = e.clientY + 5 + "px";
return false;
}
document.onclick = function(){
var mymenu = document.getElementById("menu");
mymenu.style.visibility = "hidden";
}
</script>

javascript自定义浏览器右键菜单

原理:

1.利用document.oncontextmenu将浏览器的右键菜单屏蔽(return false)。

2.在CSS中将div的visibility设置为hidden,即先把它给隐藏掉,position为absolute绝对定位,这样我们就能很容易地控制left与top的值的了。

3.在函数中获取menu为mymenu,visibility为可见(visible),再设置left与top和分别等于浏览器窗口的x,y坐标,最后再写一个document.onclick函数,只要点击,就将div的可见设置为(隐藏)hidden;

我分别测试了几款浏览器,兼容性还好吧,下面是chrome效果图:

javascript自定义浏览器右键菜单

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294