当前位置:萝卜系统下载站 > 技术开发教程 > 详细页面

解读 M$ 源代码:JavaScript + CSS 完成动态菜单显示

解读 M$ 源代码:JavaScript + CSS 完成动态菜单显示

更新时间:2022-05-30 文章作者:未知 信息来源:网络 阅读次数:

<!--解读 M$ 源代码:JavaScript + CSS 实现动态菜单显示-->
<!--Default.htm-->
<!--小弟的 JavaScript 水平实在有够差劲,故以下注解均为个人理解,有疏漏及不当之处还请各位大虾指正-->
<!--以下源代码来源于 M$ Windows 2000 Professional 安装光盘的 DISCOVER/EASY2USE 目录,更改之处见文档的最后注解-->
<!--有关“单位”与“集合”的定义,请参见文档后面的注解-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>更易使用</TITLE><!--注1-->
<SCRIPT language="javascript">

var whichIm;
var varColor = "DDEEFF";

document.onmouseover = mouseOver;
document.onmouseout = mouseOut;
document.onmousedown = mouseDown;
document.onmouseup = mouseUp;

function mouseOver() {
var vSrc = window.event.srcElement;
if (vSrc.className == "child2" || vSrc.className == "child3" || vSrc.className == "parent")/*判断对象是否为“单位”*/ {
fColor = vSrc.style.backgroundColor;
vSrc.style.backgroundColor = varColor;
/*注2*/
}
}

function mouseOut() {
var vSrc = window.event.srcElement;
if (vSrc.className == "child2" || vSrc.className == "child3" || vSrc.className == "parent") {
vSrc.style.backgroundColor = fColor;
/*注2*/
}
}

function mouseDown() {
var vSrc = window.event.srcElement;
divColl=document.all.tags("DIV");
for (i=0; i < divColl.length; i++) {
if (((divColl(i).className == "parent")||(divColl(i).className == "child2")||(divColl(i).className == "child3"))&&(divColl(i).id != vSrc.id))/*注3*/ {
divColl(i).style.backgroundColor = "";
}
if (((divColl(i).className == "parent")||(divColl(i).className == "child2")||(divColl(i).className == "child3"))&&(divColl(i).id == vSrc.id))/*注3*/ {
 divColl(i).style.backgroundColor = "#FFFFFF";
 fColor = "#FFFFFF";
}
}
}

function mouseUp() {
var vSrc = window.event.srcElement;
if (vSrc.className == "child2" || vSrc.className == "child3" || vSrc.className == "parent") {
/*注2*/
}
}
//<!-- Navigation functions -->

function closeAll(el) {
divColl=document.all.tags("DIV");
for (i=0; i<divColl.length; i++) {
felEl=eval(el + "Child1");
if ((divColl(i).className == "child1") && (felEl != divColl(i))) {
divColl(i).style.display="none";
}
}
}

function navOnload() {
divColl=document.all.tags("DIV");
for (i=0; i< divColl.length; i++) {
if (divColl(i).className == "child1" || divColl(i).className == "child4") {
if (divColl(i).className == "child4" && divColl(i).style.display == "block") {
whichArrow = eval(divColl(i).Arr);
whichArrow.src = "../images/tocuparw.gif";
}
}
}
}

function expandIE(el) {
whichEl=eval(el + "Child1");
closeAll(el);
if (whichEl.style.display != "block") {
whichEl.style.display = "block";
whichEl.isExpanded=true;
}/*注4*/else {
whichEl.style.display = "none";
whichEl.isExpanded=false;
}/*注4*/
}


function expandIE2(el) {
whichEl=eval(el + "Child3");
whichIm=event.srcElement;
if (whichEl.style.display != "block") {//当被点击对象没有展开下级菜单时,展开下级菜单
whichEl.style.display = "block";
whichEl.isExpanded=true;
whichArrow = eval(whichIm.Arr);
whichArrow.src = "../images/tocuparw.gif";
} else {//当被点击对象已经展开下级菜单时,隐藏下级菜单
whichEl.style.display="none";
whichEl.isExpanded=false;
whichArrow = eval(whichIm.Arr);
whichArrow.src = http://cfan.net.cn/info/".images/tocdnarw.gif";
}
}
</SCRIPT>
<!--注1-->
<style>
A:link
{
 color:000000;
 text-decoration: none;
}
A:visited
{
 color:666666;
 text-decoration: none;
 }
Body
{
 background-repeat:repeat-y ;
 font-family:宋体,Arial;
}
.parent
{
 position:relative;
 display:block;
 font-weight:bold;
 font-size:9pt;
 padding:1pt;
 padding-left:8pt;
 width:168px;
 top:0;
 cursor:hand;
 margin-bottom:2;
}
.child1
{
 display:none;
 font-size:9pt;
 width:168px;
 padding-bottom:4pt;
 cursor:hand;
 margin-bottom:2;
}
.child2
{
 display:block;
 font-size:9pt;
 padding:1pt;
 width:168px;
 padding-left:14pt;
 cursor:hand;
 margin-bottom:2;
}
.child3
{
 display:block;
 font-size:9pt;
 padding:1pt;
 padding-left:20pt;
 width:168px;
 cursor:hand;
 margin-bottom:2;
}
.child4
{
 display:block;
 font-size:9pt;
 width:168px;
 cursor:hand;
 margin-bottom:2;
}
.master
{
 position:relative;
 display:block;
 padding:1pt;
 width:168px;
 padding-top:12px;
}
#Navigation
{
 position:absolute;
 top:101;
 left:0;
 z-index:1;
 width:170px;
 height:340px;
 overflow:auto;
}
</style>
<BASE TARGET="SubMain">
</head>
<Body onload="navOnload();this.focus();" LEFTMARGIN="0" TOPMARGIN="0" bgcolor="White" background="bluetoc.gif" text="Black" link="Black" vlink="#005BAA">
<DIV ID="Navigation">
<DIV ID="MasterOne" CLASS="Master">
<A href="wrkwfile/mainwork.htm">
<DIV ID="Files" onkeypress="expandIE('etuOne');mouseDown()" onClick="expandIE('etuOne')" CLASS="parent">
处理文件
</DIV>
</A>
<DIV ID="etuOneChild1" CLASS="child1">
<A href="wrkwfile/trakdocs.htm">
<DIV onkeypress="expandIE2('etuTwo');mouseDown()" onClick="expandIE2('etuTwo')" ID="Track" CLASS="child2" Arr="Arrow1">
<IMG id="Arrow1" src=http://cfan.net.cn/info/".images/tocdnarw.gif" width="9" height="9" border="0" alt="" align="middle" hspace="0">
跟踪文档
</DIV>
</A>
<DIV ID="etuTwoChild3" CLASS="child4" style="display:none" Arr="Arrow1">
<A href="wrkwfile/mynetwrk.htm">
<DIV ID="MyNet" CLASS="child3" onkeypress="mouseDown();">
网上邻居
</DIV>
</A><A href="wrkwfile/mydocs.htm">
<DIV ID="Mydocs" CLASS="child3" onkeypress="mouseDown();">
我的文档
</DIV>
</A><A href="wrkwfile/opensave.htm">
<DIV ID="Open" CLASS="child3" onkeypress="mouseDown();">
文件打开/另存为
</DIV>
</A><A href="wrkwfile/history.htm">
<DIV ID="Histroy" CLASS="child3" onkeypress="mouseDown();">
历史文件夹
</DIV>
</A>
</DIV>
<A href="wrkwfile/associat.htm">
<DIV ID="Assoc" CLASS="child2" onkeypress="mouseDown();">
关联文件类型
</DIV>
</A><A href="wrkwfile/imageman.htm">
<DIV ID="Imageman" CLASS="child2" onkeypress="mouseDown();">
处理图像
</DIV>
</A><A href="wrkwfile/rdyavprt.htm">
<DIV ID="Readily" CLASS="child2" onkeypress="mouseDown();">
管理打印
</DIV>
</A>
</DIV>
<A href="findinfo/mainfind.htm">
<DIV ID="Find" onKeypress="expandIE('etuThree');mouseDown()" onClick="expandIE('etuThree')" CLASS="parent">
查找信息
</DIV>
</A>
<DIV ID="etuThreeChild1" CLASS="child1">
<A href="findinfo/indexing.htm">
<DIV ID="Index" CLASS="child2" onkeypress="mouseDown();">
索引内容
</DIV>
</A><A href="findinfo/search.htm">
<DIV ID="Search" CLASS="child2" onkeypress="mouseDown();">
集成搜索
</DIV>
</A><A href="findinfo/actsrch.htm">
<DIV ID="ActSear" CLASS="child2" onkeypress="mouseDown();">
Active Directory
</DIV>
</A>
</DIV>
<A href="personal/mainprsn.htm">
<DIV ID="Person" onkeypress="expandIE('etuFour');mouseDown()" onClick="expandIE('etuFour')" CLASS="parent">
个人化
</DIV>
</A>
<DIV ID="etuFourChild1" CLASS="child1">
<A href="personal/strtmenu.htm">
<DIV ID="StartM" CLASS="child2" onkeypress="mouseDown();">
开始菜单
</DIV>
</A><A href="personal/custtlbr.htm">
<DIV ID="Custo" CLASS="child2" onkeypress="mouseDown();">
工具栏
</DIV>
</A><A href="personal/actdsktp.htm">
<DIV ID="Acti" CLASS="child2" onkeypress="mouseDown();">
活动桌面
</DIV>
</A><A href="personal/access.htm">
<DIV ID="Acc" CLASS="child2" onkeypress="mouseDown();">
辅助向导
</DIV>
</A><A href="personal/multlang.htm">
<DIV ID="Multi" CLASS="child2" onkeypress="mouseDown();">
多语言技术
</DIV>
</A>
</DIV>
<A href="wrkonweb/mainwork.htm">
<DIV ID="Web" onkeypress="expandIE('etuFive');mouseDown()" onClick="expandIE('etuFive')" CLASS="parent">
在 Web 上工作
</DIV>
</A>
<DIV ID="etuFiveChild1" CLASS="child1">
<A href="wrkonweb/inetcwiz.htm">
<DIV ID="inetCon" CLASS="child2" onkeypress="mouseDown();">
Internet 连接向导
</DIV>
</A><A href="wrkonweb/favui.htm">
<DIV ID="FavUI" CLASS="child2" onkeypress="mouseDown();">
个人收藏夹窗格
</DIV>
</A>
</DIV>
<A href="workremo/mainremo.htm">
<DIV ID="Remote" onkeypress="expandIE('etuSix');mouseDown()" onClick="expandIE('etuSix'); mouseDown();" CLASS="parent">
远程工作
</DIV>
</A>
<DIV ID="etuSixChild1" CLASS="child1">
<A href="workremo/remotely.htm">
<DIV ID="Remotely" onkeypress="expandIE2('etuSeven');mouseDown()" onClick="expandIE2('etuSeven')" Arr="Arrow2" ID="" CLASS="child2">
<IMG id="Arrow2" src=http://cfan.net.cn/info/".images/tocdnarw.gif" width="9" height="9" border="0" alt="" align="middle" hspace="0">
脱机工作
</DIV>
</A>
<DIV ID="etuSevenChild3" CLASS="child4" style="display:none" Arr="Arrow2">
<A href="workremo/offlnfld.htm">
<DIV ID="Offfile" CLASS="child3" onkeypress="mouseDown();">
脱机文件
</DIV>
</A><A href="workremo/offlnwb.htm">
<DIV ID="Offweb" CLASS="child3" onkeypress="mouseDown();">
脱机浏览 Web
</DIV>
</A><A href="workremo/syncman.htm">
<DIV ID="Sync" CLASS="child3" onkeypress="mouseDown();">
同步管理器
</DIV>
</A>
</DIV>
<A href="workremo/netconmn.htm">
<DIV ID="NetCon" CLASS="child2" onkeypress="mouseDown();">
网络和拨号连接文件夹
</DIV>
</A><A href="workremo/efs.htm">
<DIV ID="Efs" CLASS="child2" onkeypress="mouseDown();">
加密文件系统
</DIV>
</A><A href="workremo/conspowr.htm">
<DIV ID="Cons" CLASS="child2" onkeypress="mouseDown();">
电源管理
</DIV>
</A><A href="workremo/infrared.htm">
<DIV ID="Infra" CLASS="child2" onkeypress="mouseDown();">
自动红外支持
</DIV>
</A>
</DIV>
</DIV>
</DIV>
<IFRAME src="etu_main.htm" name="SubMain" scrolling="auto" width="470" height="100%" marginwidth="4" marginheight="4" frameborder="No" style="position:relative; top:2; left:180;"></IFRAME>
</Body>
</html>
<!--
本代码中菜单结构如下图:

*************************************************************************************
* *
*处理文件-------------------------------------------------------------CLASS="parent"*
* 跟踪文档-------------------------------CLASS="child2"--|*
* 网上邻居--CLASS="child3"--||*
* 我的文档--CLASS="child3"||*
* 文件....--CLASS="child3"|-CLASS="child4" |*
* 历史....--CLASS="child3"__||-CLASS="child1" *
* 关联文件类型---------------------------CLASS="child2"|*
* 处理图像-------------------------------CLASS="child2"|*
* 管理打印-------------------------------CLASS="child2"__|*
*查找信息-------------------------------------------------------------CLASS="parent"*
* 索引内容-------------------------------CLASS="child2"--|*
* 集成搜索-------------------------------CLASS="child2"|-CLASS="child1" *
* Active Directory-----------------------CLASS="child2"__|*
*个人化---------------------------------------------------------------CLASS="parent"*
* 开始菜单-------------------------------CLASS="child2"--|*
* 工具栏---------------------------------CLASS="child2"|*
* 活动桌面-------------------------------CLASS="child2"|-CLASS="child1" *
* 辅助向导-------------------------------CLASS="child2"|*
* 多语言技术-----------------------------CLASS="child2"__|*
*在 Web 上工作--------------------------------------------------------CLASS="parent"*
* Internet 连接向导----------------------CLASS="child2"--|*
* 个人收藏夹窗格-------------------------CLASS="child2"__|-CLASS="child1" *
*远程工作-------------------------------------------------------------CLASS="parent"*
* 脱机工作-------------------------------CLASS="child2"--|*
* 脱机文件--CLASS="child3"--||*
* 脱机浏览--CLASS="child3"|-CLASS="child4" |*
* Web ....--CLASS="child3"__||*
* 网络和拨号连接文件夹-------------------CLASS="child2"|-CLASS="child1" *
* 加密文件系统---------------------------CLASS="child2"|*
* 电源管理-------------------------------CLASS="child2"|*
* 自动红外支持---------------------------CLASS="child2"__|*
* *
*************************************************************************************

上图中,每个 Class 定义了一个层,在 JavaScript 及 CSS 中,对层进行操作。每个层的 Class 名称标志了他们在菜单中的相对位置。
定义:child2、child3 和 parent 各自标志了一个层,我们称它为“单位”(其实我也不知道准确的名称应该是什么,姑且先这么叫吧:-)),而 child1 和 child4 则标志了处于同一相对位置的几个子层所组成的父层,我们称它为“集合”。
注1:原(M$)文件中引用了../Javax.htm 及 ../NAVSTYLE.CSS 两个文件,为叙述方便,上述两个文件中与本文有关的代码被直接添加到本文中。
注2:原(M$)代码中此处有一语句"window.event.cancelbubble == true;",我没看懂,删了~~
注3:原(M$)代码中条件还包含"(divColl(i).className == "child4")||",我实在没看明白有什么作用,删了,有知道的大虾还请指教;
注4:原(M$)代码中不包含此部分代码,我加上的,让 parent 也能主动关闭。
最后还有一点说明,在每一个有子菜单的层上加一个语句onmouseOver="...","..."就是onClick里边的东东,试试看,你看到了什么?
-->
<!--CopyRight By Cheery_Ke,04-07-2002,All Reserved-->

温馨提示:喜欢本站的话,请收藏一下本站!

本类教程下载

系统下载排行

网站地图xml | 网站地图html