RSS
热门关键字:  CSS教程  CSS布局  CSS样式  CSS hack  CSS JS
当前位置 :| 主页>CSS菜单>

仿flash动画导航条(html+css+js)

来源:cssplay 作者:cssplay 时间:2008-08-31 点击:

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(http://www.cssplay.org.cn/example/images/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(http://www.cssplay.org.cn/example/images/nav_right.png) repeat-y right 0}
div#nav ul li a{float:left; height:100%; padding:0 20px; background:url(http://www.cssplay.org.cn/example/images/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}

h2{text-align:center}


HTML代码

<div id="nav">
<ul class="clear">
<li class="on"><a href="http://www.cssplay.org.cn/">网站首页</a></li>
<li><a href="http://www.cssplay.org.cn/css-tutorial/">CSS教程</a></li>
<li><a href="http://www.cssplay.org.cn/css-layout/">CSS布局</a></li>
<li><a href="http://www.cssplay.org.cn/css-menu/">CSS菜单</a></li>
<li><a href="http://www.cssplay.org.cn/css-style/">CSS样式</a></li>
<li><a href="http://www.cssplay.org.cn/css-template/">CSS模板</a></li>
<li class="nobg"><a href="http://www.cssplay.org.cn/">玩转CSS - CSSPLAY中文官方网站 致力于CSS技术的提高</a></li>
</ul>
</div>

<h2><a href="http://www.cssplay.org.cn/" title="玩转CSS - CSSPLAY中文官方网站 致力于CSS技术的提高">玩转CSS - CSSPLAY中文官方网站 致力于CSS技术的提高</a><h2>


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++){
  if(el_li[i].className == 'on') continue;
  arr[i] = new nav(el_li[i], {speed: 10, num: 4});
 }
})();


实例运行代码框


 提示:您可以先修改部分代码再运行


最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
热点实例