這個效果會在視窗四周畫上一圈細框,滑鼠移入不同元素時,外框的顏色與粗細會隨之變化。它的實作方式是靠 VML(Vector Markup Language)在頁面四邊各畫一條線,再用 JavaScript 改變 stroke 屬性。以下說明結構與原理,並給出現代的等效做法。
<STYLE>
.disableHscroll {
width: 100%;
overflow-x: hidden;
overflow-y: scroll;
}
v\:* { BEHAVIOR: url(#default#VML) }
</STYLE>
v\:* 這行是 VML 的命名空間宣告,作用是告訴 IE:「凡是 v 開頭的標籤,都請用內建的 VML 引擎來處理」。反斜線是 CSS 對冒號的跳脫寫法。
<SCRIPT>
var defaultcolor = "#999999";
var lineweighthighlight = 1;
var lineweightnormal = 1;
var distancetomouse = 0;
var marginleft = 0;
var margintop = 0;
var marginbottom = 0;
var marginright = 0;
var margincenterx, margincentery;
if (document.all && window.print) {
document.write('<div id="bodycontent" style="position:relative">');
}
function initiateanimation() {
marginbottom = document.body.clientHeight;
marginright = document.body.clientWidth;
margincenterx = Math.floor(marginright / 2);
margincentery = Math.floor(marginbottom / 2);
rectangulartopleft.to = marginleft + "," + margintop;
rectangulartopright.to = marginright + "," + margintop;
rectangularbottomleft.to = marginleft + "," + marginbottom;
rectangularbottomright.to = marginright + "," + marginbottom;
rectangulartop.to = margincenterx + "," + margintop;
rectangularright.to = marginright + "," + margincentery;
rectangularbottom.to = margincenterx + "," + marginbottom;
rectangularleft.to = marginleft + "," + margincentery;
rectangulartopleft.strokecolor = defaultcolor;
rectangulartopright.strokecolor = defaultcolor;
rectangularbottomleft.strokecolor = defaultcolor;
rectangularbottomright.strokecolor = defaultcolor;
rectangulartop.strokecolor = defaultcolor;
rectangularright.strokecolor = defaultcolor;
rectangularbottom.strokecolor = defaultcolor;
rectangularleft.strokecolor = defaultcolor;
}
function msover(thiscolorhighlight) {
if (document.all && window.print) {
rectangulartopleft.strokecolor = thiscolorhighlight;
rectangulartopright.strokecolor = thiscolorhighlight;
rectangularbottomleft.strokecolor = thiscolorhighlight;
rectangularbottomright.strokecolor = thiscolorhighlight;
rectangulartop.strokecolor = thiscolorhighlight;
rectangularright.strokecolor = thiscolorhighlight;
rectangularbottom.strokecolor = thiscolorhighlight;
rectangularleft.strokecolor = thiscolorhighlight;
rectangulartopleft.strokeweight = lineweighthighlight;
rectangulartopright.strokeweight = lineweighthighlight;
rectangularbottomleft.strokeweight = lineweighthighlight;
rectangularbottomright.strokeweight = lineweighthighlight;
rectangulartop.strokeweight = lineweighthighlight;
rectangularright.strokeweight = lineweighthighlight;
rectangularbottom.strokeweight = lineweighthighlight;
rectangularleft.strokeweight = lineweighthighlight;
}
}
function msout() {
if (document.all && window.print) {
/* 把八個 shape 的 strokecolor 設回 defaultcolor,
strokeweight 設回 lineweightnormal */
}
}
</SCRIPT>
觀察命名就會發現端倪:左上、右上、左下、右下四個直角,加上上、右、下、左四個邊中點,一共八個 shape。VML 的 <v:line> 或 <v:rect> 需要明確的起訖座標,因此作者把視窗的外框拆成八段來定位——四個角各放一個小方塊補齊轉角,四條邊各放一條線段負責主體。
座標系統來自 document.body.clientHeight 與 clientWidth,也就是 body 的可視區域尺寸。中心點則是寬高各除以二後取整數,用 Math.floor 避免半像素。
msover() 接受一個顏色參數,把八個圖形的 strokecolor 全部換成高亮色、strokeweight 換成高亮粗細;msout() 則全部還原為 defaultcolor 與 lineweightnormal。因此只要在任一輸入框上寫 onmouseover="msover('\#ff0000')"、onmouseout="msout()",滑過時整圈外框就會亮起。
VML 只存在於 IE。它是 Microsoft 在 SVG 標準化之前提出的向量格式,從未成為跨瀏覽器標準,IE9 之後即被移除。這段程式碼在其他瀏覽器中完全不會顯示。
document.all 與 window.print 的偵測。用這兩者來判斷瀏覽器能力,在今天是不可靠的。正確做法是 if ('VML' in ...) 這類特性偵測,或直接不支援。
重複程式碼。八個圖形各自寫一遍 style 指定,共二十四行;用陣列或 NodeList 迴圈處理可縮短到三行。這也是當時缺乏 querySelectorAll 的時代產物。
要在現代瀏覽器做出「滑過輸入框時邊框高亮」,完全不需要向量語言:
input {
border: 1px solid #999;
transition: border-color 0.2s, box-shadow 0.2s;
}
input:hover,
input:focus {
border-color: #3fadc7;
box-shadow: 0 0 4px #3fadc7;
}
若真要畫一層隨視窗變化的外框,用一個 position: fixed 的元素加上 box-shadow: inset 0 0 0 1px #999 即可,或直接用 outline:
.frame {
position: fixed;
inset: 0;
pointer-events: none;
box-shadow: inset 0 0 0 1px #999;
transition: box-shadow 0.2s;
}
body.highlight .frame {
box-shadow: inset 0 0 0 2px #3fadc7;
}
這版只要切換 body 上的一個 class,就能讓整圈外框同步變色,代碼量遠少於八個 VML 圖形,且具備平滑過渡。這個對比也說明了一件事:當標準逐步補齊之後,過去必須靠私有向量語言硬湊的效果,如今都是幾行 CSS 的事。