延遲菜單

獲取class兼容方法:

    function getByClass(oParent, sClass) {
        var aEle = oParent.getElementsByTagName('*');
        var result = [] //最后返回数组
        var re = new RegExp('\\b' + sClass + '\\b', 'i');//正则对象
        for (var i = 0; i < aEle.length; i++) {
            if (re.test(aEle[i].className)) {
                result.push(aEle[i])
            }
        }
        return result;
    }

js代碼:

window.onload = function(){
    var ul = document.getElementsByTagName('ul')[0]
    var a = getByClass(document, 'normal')
    var span1 = getByClass(document, 'float_layer');
    var timer = null;
    for (var i = 0; i < a.length; i++) {
        a[i].index = i;
        a[i].onmouseover = function () { //移到a.normal
            for (var i = 0; i < a.length; i++) { //清除所有顯示
                span1[i].style.display = 'none'
            }
            span1[this.index].style.display = 'block' //當前顯示
            clearInterval(timer) //在a.normal上也須清除定時器
        }
        a[i].onmouseout = function () { //移開a.normal
            var This = this
            timer = setTimeout(function () {
                span1[This.index].style.display = 'none' //this指setTimeout的對象window,改變this指向
            }, 1000)
        }
        span1[i].onmouseover = function () { //在span.float_layer也顯示,所以需要清除timer
            clearInterval(timer)
        }
        span1[i].onmouseout = function () { //離開span.float_layer,消失
            var This = this;
            timer = setTimeout(function () {
                This.style.display = 'none'
            }, 1000)
        }
    }
}

運行

CSS-Reset

html,body,h1,h2,h3,h4,h5,h6,hr,p,iframe,dl,dd,ul,ol,li,pre,form,fieldset,button,input,textarea,th,td{margin: 0;padding: 0}
html{background-color: #FFF}
body,button,input,select,textarea,fieldset,label{font: 12px/1.5 "Lucida Grande",tahoma,arial,\5b8b\4f53;-ms-overflow-style:scrollbar}
h1,h2,h3,h4,h5,h6{font-size: 100%}
ul,ol,menu{list-style: none}
fieldset,img{border: 0 none}
img{-ms-interpolation-mode: bicubic}
img,input,textarea,button,select{vertical-align:middle}
i,em,optgroup,address,cite{font-style: normal}
table{border-collapse: collapse;border-spacing: 0}
caption,th{text-align:inherit}
input,select,textarea,button{font-size: 100%;-webkit-box-sizing: content-box;-moz-box-sizing: content-box;box-sizing: content-box}
article,aside,footer,header,section,nav,figure,figcaption,hgroup,details,menu{display:block}
audio,canvas,video{display:inline-block;*display:inline;*zoom:1}
input,textarea,button,select,a{outline:0 none}
button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}
button,input[type=button],input[type=submit]{cursor: pointer}
button[disabled],html input[disabled]{cursor:default}
input[type=search]{-webkit-appearance: textfield}
textarea{overflow-y: auto;resize: vertical}
sup,sub{vertical-align:baseline}
a{color: #333;text-decoration: none}
a:hover{text-decoration: underline}
.clearfix{zoom: 1}
.clearfix:after{content: ' ';display: block;clear: both;height: 0;visibility: hidden}

精簡版

html,body,h1,h2,h3,h4,h5,h6,hr,p,iframe,dl,dd,ul,ol,li,pre,form,fieldset,button,input,textarea,th,td{margin: 0;padding: 0}
body,button,input,select,textarea,fieldset,label{font: 12px/1.5 "Lucida Grande",tahoma,arial,\5b8b\4f53;}
h1,h2,h3,h4,h5,h6{font-size: 100%}
ul,ol{list-style: none}
fieldset,img{border: 0 none}
img,input,textarea,button,select{vertical-align:middle}
i,em{font-style: normal}
table{border-collapse: collapse;border-spacing: 0}
caption,th{text-align:inherit}
input,select,textarea,button{font-size: 100%;outline:0 none}
button,input[type=button],input[type=submit]{cursor: pointer}
textarea{overflow-y: auto;resize: vertical}
sup,sub{vertical-align:baseline}
a:hover{text-decoration: underline}
.clearfix{zoom: 1}
.clearfix:after{content: ' ';display: block;clear: both;height: 0;visibility: hidden}