惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
P
Privacy International News Feed
T
Tenable Blog
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
T
The Blog of Author Tim Ferriss
Scott Helme
Scott Helme
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Security Latest
Security Latest
Blog — PlanetScale
Blog — PlanetScale
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
博客园 - 三生石上(FineUI控件)
I
InfoQ
Spread Privacy
Spread Privacy
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog
C
CERT Recently Published Vulnerability Notes
A
About on SuperTechFans
博客园_首页
Engineering at Meta
Engineering at Meta
Project Zero
Project Zero
Latest news
Latest news

博客园 - 白白

Avril lavigne - Girlfriend(mv) ASP.NET 2.0 - Enter Key - Default Submit Button - 白白 圣射手双修心得 圣射手技能 Event事件详解 用Javascript实现Agent(网页精灵)(1) 利用vml制作统计图全攻略----饼图的制作(四) 利用vml制作统计图全攻略----饼图的制作 (三) 利用vml制作统计图全攻略----饼图的制作 (二) 利用vml制作统计图全攻略----饼图的制作(一) (Tip)教你通过windows的正版验证 GML、SVG、VML的比较 AJAX七宗罪(转) AJAX(转) HTML中字符实体集 ASCII码表 关于potentially dangerous Request.Cookies value的解决 最佳实践:string对象(string.empty与""的区别到底是什么呢?) C#来创建和读取XML文档
用Javascript实现Agent(实现右键菜单)(2) - 白白 - 博客园
changzheng · 2006-02-17 · via 博客园 - 白白

用Javascript实现Agent(实现右键菜单)

既然是显示右键菜单,那么我们就要截获对精灵按右键时的事件oncontextmenu

然后将右键菜单的html封装到一个函数,一上来这个菜单层是隐藏的

function getMenuHtml()
{
 var menuHtml ="<div id=menu style=\"text-align: left;position: absolute; visibility: hidden; width: 85px; z-index: 200;padding:1px\">";
 menuHtml += "<table border=1 width=100 height=100>";
 menuHtml += "<tr>";
 menuHtml += "<td>";
 menuHtml += "Menu1";
 menuHtml += "</td>";
 menuHtml += "</tr>";
 menuHtml += "</table>";
 menuHtml += "</div>";
 return menuHtml;
}

实现右键菜单,我们需要精灵截获oncontextmenu事件,需要对昨天的run函数进行一点小的修改

Agent.prototype.run=function()
{
 var agentHtml = "";
 agentHtml += "<img src="+this.imgAgent;
 agentHtml += " id=\"agent1\"";
 agentHtml += " style=\"position:absolute;left:"+this.agentLeft+";top:"+this.agentTop+";cursor:move\"";
 agentHtml += " onselectstart=\"return false\"";
 agentHtml += " onmousedown=\"mousedown(this)\"";
 agentHtml += " onmouseup=\"mouseup()\"";
 agentHtml += " onmousemove=\"mousemove()\"";
 agentHtml += " oncontextmenu=\"return showRightMenu()\"";
 agentHtml += ">";
 agentHtml += getMenuHtml();
 return document.write(agentHtml);
}

可以看出oncontextmenu调用的是showRightMenu函数,这个函数使菜单层可见,并且显示位置随着鼠标的位置而显示

注解:

scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
event.clientX:鼠标点击的x轴位置
event.clientY:鼠标点击的y轴位置

/*
* 右键菜单 v1.0
* author: 清风
*/
function showRightMenu()
{
 menu.style.left=document.body.scrollLeft+event.clientX
 menu.style.top=document.body.scrollTop+event.clientY
 menu.style.visibility="visible";
 return false;
}

菜单现在可以显示了,如何使其消失呢?应当是用户点击任意位置就可消失

document.onclick=click
function click()
{
 menu.style.visibility="hidden";
}

今天的效果如下:

                                                                                                                                                                                                             选择自 的 Blog