为了完成Internet Explorer的工具栏,当用户鼠标移过并移出时,图形必须翻转过来。先 点击这里看看其中的效果图2,再看一看实现的HTML代码和脚本: < HTML> < HEAD> < STYLE TYPE="text/css"> .but { border:1px buttonface solid; font-family:MS Sans Serif;font-size:8pt; } < /STYLE> . . . < /HEAD> < BODY> . . . < DIV ID="toolbar" NOWRAP STYLE="width:20;background-color:buttonface;padding:2px;"> < BUTTON CLASS=but> onClick="yourStopFunction()"< IMG SRC="StopOff.gif" WIDTH=19 HEIGHT=20 BORDER=0>< BR> Stop< /BUTTON>< BUTTON CLASS=but onClick="yourRefreshFunction()"IMG SRC="RefreshOff.gif" WIDTH=17 HEIGHT=20 BORDER=0>< BR> Refresh< /BUTTON>< BUTTON CLASS=but onClick="yourHomeFunction()"IMG SRC="HomeOff.gif" WIDTH=19 HEIGHT=20 BORDER=0>< BR> Home< /BUTTON>< BUTTON CLASS=but onClick="yourSearchFunction()"IMG SRC="SearchOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> Search< /BUTTON>< BUTTON CLASS=but onClick="yourFavoritesFunction()"IMG SRC="FavoritesOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> Favorites< /BUTTON>< BUTTON CLASS=but onClick="yourHistoryFunction()"IMG SRC="HistoryOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> History< /BUTTON> < /DIV> . . . < SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript"> allBUTs = toolbar.children; maxWidth = 0; for (i=0;i< allBUTs.length;i++) { tSpan = allBUTs(i); tSpan.onselectstart = function(){return false} maxWidth = Math.max(maxWidth,tSpan.offsetWidth); tSpan.img = tSpan.children(0); tSpan.oversrc = tSpan.innerText + "On.gif"; tSpan.outsrc = tSpan.innerText + "Off.gif"; tSpan.onmouseover = function(){ this.style.border = "1px buttonhighlight outset"; this.img.src = this.oversrc; } tSpan.onmouseout = function(){ this.style.border = "1px buttonface solid"; this.img.src = this.outsrc; } tSpan.onmousedown = function(){ this.style.border = "1px buttonhighlight inset"; } tSpan.onmouseup = function(){ this.style.border = "1px buttonhighlight outset"; window.focus(); } } for (i=0;i< allBUTTONs.length;i++) { tSpan = allBUTTONs(i); tSpan.style.pixelWidth = maxWidth; } < /SCRIPT>> < /BODY> < /HTML> 增加的脚本负责执行一个简单的图形翻转功能,这是我们熟悉的功能。在例子中,为了尽力控制脚本的长度,我们严格地遵守命名方案。我们的工具栏条目都只显示一个词的文本,这有很大的帮助,因为我们可以用同一个词去识别相关的图形。因此,我们的图形的命名如下: StopOff.gif RefreshOff.gif HomeOff.gif SearchOff.gif FavoritesOff.gif HistoryOff.gif StopOn.gif RefreshOn.gif HomeOn.gif SearchOn.gif FavoritesOn.gif HistoryOn.gif 脚本首先要识别属于按钮的图形。每个按钮的第一个元素就是图形。也就是说,它是按钮子集中的第一个对象,其索引数为0。我们给图形分配一个按钮的定制属性img。 按钮同时有其他2个属性:oversrc 和 outsrc,这表示当发生mouseover和mouseout状态时的对于图形文件。按钮显示的文字(innerText)决定了使用哪个图形,根据情况附上后缀 Off.gif 和 On.gif。 最后,在mouseover 和 mouseout 事件处理程序中,我们将这些值分配给图形的src属性,从而产生了翻转。 现在来看看工具栏使用BUTTON的最后一个理由。
|