Macji Pro2010

记录一些应该记录的东东

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

February 1, 2008 - by Macji - HTML/CSS/JS/PHP - xhtml, javascript, animation

废话不多说,先给出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});
  }
})();

查看效果

2 条评论»

用javascript来实现动画导航

November 27, 2007 - by Macji - HTML/CSS/JS/PHP - javascript, animation

google是个大公司,全世界都有google的脚印,韩国的google动画效果非常不错,蓝色理想论坛里已经有人挖过来了,可惜js写的太多了,那自己写一个吧?好,就这么干!


原理


小时候,总喜欢看动画片吧,动画片是怎样实现的呢?记得妈妈说是一张画一张画切换过去(啊?那一部葫芦兄弟要画多少副画啊? -_-! ),其实我们现在做的也是这样,用一个图片,这个图片里有很多个小图,来显示动画轨迹.按时间来移动图片,那图片不是会动了啊?(不知道,表达清楚了没…语文很重要啊!!)


准备


我们需要一张图片,一个大脑,一张会笑的脸(不笑效果就出不来了….)!!下面是我准备的图片(ps水平有限^_^)…

代码


我们看到上面的图片,想象下,它动起来是多么的优美啊…


css

.Gnb_btn_div{
	width:90px;
	height:75px;
	overflow:hidden;
	display:block;
	position:absolute;
}     

.Gnb_btn_img{
	width:100%;
	height:525px;
	display:block;
	overflow:hidden;
	text-indent:-500px;
}
#gnb_btn_01 .Gnb_btn_img {
	background-image:url(http://www.wler.cn/blog/img/friend.gif)
}

javascript

<script type="text/javascript">
// <![CDATA[
function GNB(_7c){
	//初始化一些参数
	this.iImgNum=7;			//小图片个数
	this.iImgHeight=75;		//小图片高度
	this.iOverSpeed=50;		//鼠标经过时候切换的时间
	this.iOutSpeed=50;		//鼠标离开时候切换的时间
	this.eventObj=_7c;		//取得图片对象     

	this.MouseOverFlag=false;
	this.imageIndex=0;
	if(this.eventObj==null){return;}
	this.eventObj.parentClass=this;this.eventAssign();
}     

GNB.prototype.eventAssign=function(){//注册事件
	this.eventObj.onmouseover=this.menuMouseOver;
	this.eventObj.onmouseout=this.menuMouseOut;
};     

GNB.prototype.menuMouseOver=function(){//鼠标经过
	if(this.parentClass.MouseOverFlag!=false){return;}
	this.parentClass.MouseOverFlag=true;
	this.parentClass.clearTimeOut();
	this.parentClass.menuMouseOverTimer();
};     

GNB.prototype.menuMouseOut=function(){//鼠标离开
	this.parentClass.MouseOverFlag=false;
	this.parentClass.clearTimeOut();
	this.parentClass.menuMouseOutTimer();
};     

GNB.prototype.menuMouseOverTimer=function(){//经过图片位置递增
	var _7d=this;
	if(this.imageIndex>=this.iImgNum){return;}
	this.eventObj.scrollTop=this.imageIndex*this.iImgHeight;
	this.imageIndex++;
	this.setTimerID=setTimeout(function(){_7d.menuMouseOverTimer();},this.iOverSpeed);
};     

GNB.prototype.menuMouseOutTimer=function(){////经过图片位置递减
	var _7e=this;if(this.imageIndex<0){return;}
	this.eventObj.scrollTop=this.imageIndex*this.iImgHeight;
	this.imageIndex--;
	this.setTimerID=setTimeout(function(){_7e.menuMouseOutTimer();},this.iOutSpeed);
};     

GNB.prototype.clearTimeOut=function(){//取消定时
	clearTimeout(this.setTimerID);
};
// ]]>
</script>

xhtml

<div class="Gnb_btn_div" id="gnb_btn_01">

<a class="Gnb_btn_img" href="#1" mce_href="#1">找朋友</a>
</div>     

<script type="text/javascript">
// <![CDATA[
var GNB1=new GNB(document.getElementById("gnb_btn_01"));//实例单个按钮,当然也可以多个
// ]]>
</script>

演示

1 条评论»