废话不多说,先给出CSS
body{margin:100px 0}
.clear:after{content:"."; display:block; height:0; clear:both; visibility:hidden}.clear{display:inline-block}.clear{display:block}
div#nav{height:70px; background:url(img/nav_bg.png) repeat-x}
div#nav ul{width:960px; margin:0 auto; list-style:none}
div#nav ul li{float:left; height:35px; overflow:hidden; padding:0 2px 0 0; font: bold 12px/35px Arial; background:url(img/nav_right.png) repeat-y right 0}
div#nav ul li a{float:left; height:100%; padding:0 20px; background:url(img/nav_sub.png) repeat-x; color:#fff; text-decoration:none}
div#nav ul li a.hover{clear:both; background-position:0 -35px}
div#nav ul li.on a{background-position:0 -35px}
div#nav ul li.nobg{background:none}html
<div id="nav">
<ul class="clear">
<li class="on"><a href="javascript:;" mce_href="javascript:;">首页</a></li>
<li><a href="javascript:;" mce_href="javascript:;">麦鸡</a></li>
<li><a href="javascript:;" mce_href="javascript:;">麦鸡</a></li>
<li><a href="javascript:;" mce_href="javascript:;">麦鸡</a></li>
<li><a href="javascript:;" mce_href="javascript:;">MacJi</a></li>
<li><a href="javascript:;" mce_href="javascript:;">麦鸡</a></li>
<li><a href="javascript:;" mce_href="javascript:;">麦鸡</a></li>
<li class="nobg"><a href="http://www.macji.com" mce_href="http://www.macji.com">麦鸡的博客</a></li>
</ul>
</div>JavaScript
function nav(c, config){
this.config = config || {speed: 10, num: 2};
this.container = (typeof(c)=="object") ? c : document.getElementById(c);
this.lineHeight = this.container.offsetHeight;
this.scrollTimeId = null;
var _this = this;
this.__construct = function (){
var inner,el,href;
inner = _this.container.childNodes[0].innerHTML;
href = _this.container.childNodes[0].href;
el = document.createElement("a");
el.innerHTML = inner;
el.href = href;
el.className = 'hover';
_this.container.appendChild(el);
//注册事件
_this.container.onmouseover = function (){_this.start()};
_this.container.onmouseout = function (){_this.end()};
}();
this.start = function (){
_this.clear();
_this.scrollTimeId = setTimeout(function(){_this.scrollUp();}, _this.config.speed);
};
this.end = function (){
_this.clear();
_this.scrollTimeId = setTimeout(function(){_this.scrollDown();}, _this.config.speed);
};
this.scrollUp = function (){
var c = _this.container;
if(c.scrollTop >= _this.lineHeight){c.scrollTop = _this.lineHeight;return;}
c.scrollTop += _this.config.num;
_this.scrollTimeId = setTimeout(function(){_this.scrollUp();}, _this.config.speed);
};
this.scrollDown = function (){
var c = _this.container;
if(c.scrollTop <= 0){c.scrollTop = 0;return;}
c.scrollTop -= _this.config.num;
_this.scrollTimeId = setTimeout(function(){_this.scrollDown();}, _this.config.speed);
};
this.clear = function (){clearTimeout(_this.scrollTimeId)};
}调用方法
(function(){
var container = document.getElementById('nav');
var el_li = container.getElementsByTagName('li');
var arr = [];
for( var i = 0; i < el_li.length; i++){
//如果不是当前页面(className == 'on')就实例化
if(el_li[i].className == 'on') continue;
arr[i] = new nav(el_li[i], {speed: 10, num: 4});
}
})();本博客所有文章遵循创作共用版权协议,要求署名、非商业、保持一致。转载时请先阅读以上许可协议,并以超链接形式注明出处。
这篇文章发表于2008年02月01日 06:37:53, 并被分类于HTML/CSS/JS/PHP. 您可以通过订阅 RSS 2.0 跟踪对这篇文章的评论, 也可以发表你的评论, 或者在您自己的网站中引用(trackback)该篇日志.
已有 2 条评论
朋友,能否发个演示给我,我该如何调用
以前的演示地址被我删了,请查看 http://labs.macji.com/nav/