js源碼:
// JavaScript Document
/**
* @function drop漸現進入
* @direction objId顯示塊的id,direction方向(up,down,left,right),speed速度
*/
function drop(objId, direction, speed, destorySign){
var effect = this;
var obj = null;
var top = 0;
var left = 0;
var container = null;
var direction = direction;
var speed = speed ? speed : 20;
var filterSpeed = 10;
var opacity = 0;
var timer = null;
var objId = objId;
var destorySign = destorySign ? destorySign : false;
var init = function(){
obj = document.getElementById(objId);
obj.style.position= "absolute";
obj.style.opacity = opacity;
obj.style.filter = "alpha(opacity:"+ opacity+");";
obj.style.display="block";
top = obj.offsetTop;
left = obj.offsetLeft;
var parent = obj.parentNode;
container = document.createElement("div");
container.style.position = "absolute";
container.style.overflow = "hidden";
container.style.zIndex = 1000;
if(parent.tagName != "BODY"){
container.style.width = obj.offsetWidth + "px";
container.style.height = obj.offsetHeight + "px";
container.style.top = top+"px";
container.style.left = left+"px";
top = 0;
left = 0;
}
else{
container.style.width = "100%";
container.style.height = "100%";
container.style.top = "0px";
container.style.left = "0px";
}
container.appendChild(obj);
parent.appendChild(container);
if(direction == "up"){
obj.style.top = 0-obj.offsetHeight+"px";
obj.style.left = left + "px";
filterSpeed = parseInt(100/((container.offsetHeight-top)/speed));
}
else if(direction == "down"){
obj.style.top = container.offsetHeight+"px";
obj.style.left = left + "px";
filterSpeed = parseInt(100/((container.offsetHeight-top)/speed));
}
else if(direction == "left"){
obj.style.left = 0-obj.offsetWidth+"px";
obj.style.top = top + "px";
filterSpeed = parseInt(100/((container.offsetWidth-left)/speed));
}
else{
obj.style.left = container.offsetWidth+"px";
obj.style.top = top + "px";
filterSpeed = parseInt(100/((container.offsetWidth-left)/speed));
}
obj.style.margin = "0px";
container.style.display = "none";
};
this.show = function(func){
clearInterval(timer);
timer = setInterval(function(){effect.animateShow(func);}, 50);
};
this.hide = function(func){
clearInterval(timer);
timer = setInterval(function(){effect.animateHide(func);}, 50);
};
this.animateShow = function(func){
container.style.display = "block";
obj.style.display = "block";
if(direction == "up"){
if(obj.offsetTop + speed <= top){
obj.style.top = (obj.offsetTop + speed) + "px";
opacity = opacity + filterSpeed > 100 ? 100 : opacity + filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.top = top+"px";
opacity = 100;
obj.style.opacity = "";
obj.style.filter = "";
container.style.overflow = "visible";
clearInterval(timer);
if(func)
func();
}
}
else if(direction == "down"){
if(obj.offsetTop - speed >= top){
obj.style.top = obj.offsetTop - speed +"px";
opacity = opacity + filterSpeed > 100 ? 100 : opacity + filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.top = top+"px";
opacity = 100;
obj.style.opacity = "";
obj.style.filter = "";
container.style.overflow = "visible";
clearInterval(timer);
if(func)
func();
}
}
else if(direction == "left"){
if(obj.offsetLeft + speed <= left){
obj.style.left = obj.offsetLeft + speed + "px";
opacity = opacity + filterSpeed > 100 ? 100 : opacity + filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.left = left+"px";
opacity = 100;
obj.style.opacity = "";
obj.style.filter = "";
container.style.overflow = "visible";
clearInterval(timer);
if(func)
func();
}
}
else{
if(obj.offsetLeft - speed >= left){
obj.style.left = obj.offsetLeft - speed + "px";
opacity = opacity + filterSpeed > 100 ? 100 : opacity + filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.left = left + "px";
opacity = 100;
obj.style.opacity = "";
obj.style.filter = "";
container.style.overflow = "visible";
clearInterval(timer);
if(func)
func();
}
}
};
this.animateHide = function(func){
container.style.display = "block";
container.style.overflow = "hidden";
if(direction == "up"){
if(obj.offsetTop - speed >= 0-obj.offsetHeight){
obj.style.top = obj.offsetTop - speed + "px";
opacity = opacity - filterSpeed < 0 ? 0 : opacity - filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.top = 0-obj.offsetHeight + "px";
container.style.display = "none";
opacity = 0;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
clearInterval(timer);
this.destory();
if(func)
func();
}
}
else if(direction == "down"){
if(obj.offsetTop + speed <= container.offsetHeight){
obj.style.top = obj.offsetTop + speed +"px";
opacity = opacity - filterSpeed < 0 ? 0 : opacity - filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.top = container.offsetHeight+"px";
container.style.display = "none";
opacity = 0;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
clearInterval(timer);
this.destory();
if(func)
func();
}
}
else if(direction == "left"){
if(obj.offsetLeft - speed >= 0-obj.offsetWidth){
obj.style.left = obj.offsetLeft - speed + "px";
opacity = opacity - filterSpeed < 0 ? 0 : opacity - filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.left = 0-obj.offsetWidth + "px";
container.style.display = "none";
opacity = 0;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
clearInterval(timer);
this.destory();
if(func)
func();
}
}
else{
if(obj.offsetLeft + speed <= container.offsetWidth){
obj.style.left = obj.offsetLeft + speed + "px";
opacity = opacity - filterSpeed < 0 ? 0 : opacity - filterSpeed;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
}
else{
obj.style.left = container.offsetWidth+"px";
container.style.display = "none";
opacity = 0;
obj.style.opacity = opacity/100;
obj.style.filter = "alpha(opacity:"+ opacity+");";
clearInterval(timer);
this.destory();
if(func)
func();
}
}
};
this.destory = function(){
if(destorySign){
var parent = container.parentNode;
parent.removeChild(container);
container.childNodes[0].removeAttribute("style");
parent.appendChild(container.childNodes[0]);
}
};
init();
return this;
};
測試源碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>drop</title>
<style>
*{
padding:0px;
margin:auto;
}
#menu{
width:100px;
}
#menu_titile{
border:1px #000000 solid;
width:100px;
}
.menu_list{
border:1px #CCCCCC solid;
width:100px;
background:#FFF;
}
li{
list-style-type:none;
}
</style>
</head>
<body>
<div id="menu">
<div id="menu_title">menu</div>
<div id="menu_list" class="menu_list">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
<li>menu4</li>
<li>menu5</li>
</ul>
</div>
</div>
<div>
</div>
</body>
<script language="javascript" src="drop.js"></script>
<script language="javascript">
var clip_menu = menu("menu_title", "menu_list", "drop", "up")
function menu(menuId, menuListId, effect, direction){
var effectObj = null;
var menu = null;
var init = function(){
menu = document.getElementById(menuId);
effectObj = drop(menuListId, direction, 10);
menu.parentNode.onmouseover = effectObj.show;
menu.parentNode.onmouseout = effectObj.hide;
};
init();
return this;
};
</script>
</body>
</html>