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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 白白

Avril lavigne - Girlfriend(mv) ASP.NET 2.0 - Enter Key - Default Submit Button - 白白 圣射手双修心得 圣射手技能 Event事件详解 用Javascript实现Agent(实现右键菜单)(2) - 白白 - 博客园 利用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(网页精灵)(1)
白白 · 2006-02-17 · via 博客园 - 白白

一直觉得Agent是用来做网站导航和帮助的不错选择,可惜MS Agent只有IE支持,而且好像加载速度不是很快,分析了一下觉得用Javascript绝对可以做到,以前对Javascript都是抄来抄去,趁此机会也彻底学一学。OK,Let's Begin

Javascript既然是基于对象的语言,那么大体上我们也可以按照OO的思路来设计自己的Agent

我们希望客户端简单调用一下就OK了,应当是如下的形式。

<script src="agent.js"></script>

<script language="JavaScript">
<!--
var agent = new Agent();
agent.run();
//-->
</script>

那么首先我们就来创建一个agent.js,js创建一个对象很简单只需要提供一个构造函数就OK了

function Agent()
{
 this.imgAgent = "images/agent.gif";
}

那么,接下来的任务就是让这个对象拥有一个run方法,这个方法应当仅仅就是输出最后拼凑好的html,我们先简单实现一下。

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

从上面代码可以看出,我们实现的第一个精灵效果就是可以拖动精灵到界面任意一个地方,只需实现一下onmousedown,onmousemove,onmousemove三个事件即可

var currentMoveObj = null;        //当前拖动对象

this.setImage=function(img)
{
 imgAgent = img;
}
function dblclick(obj)
{
 obj.src= "images/chaosai.gif";
}
function mousedown(obj)
{
 currentMoveObj = obj;               //当对象被按下时,记录该对象
 //查了一下资料setCapture的意思是:
 //捕捉触发事件时的焦点对象并使鼠标焦点始终绑定该对象。
 //很关键不然灵敏度会很低
 currentMoveObj.setCapture()
 relLeft = event.x - currentMoveObj.style.pixelLeft;
 relTop = event.y - currentMoveObj.style.pixelTop;
}
function mouseup()
{
 if(currentMoveObj!=null)
 {
  currentMoveObj.releaseCapture();
  currentMoveObj=null;
 }
}
function mousemove()
{
 if(currentMoveObj != null)
 {
  currentMoveObj.style.pixelLeft=event.x-relLeft;
  currentMoveObj.style.pixelTop=event.y-relTop;
 }
}

posted on 2006-02-17 10:01  白白  阅读(476)  评论()    收藏  举报