js特效-下拉菜单

来源:互联网 发布:mac打印色彩调整图案 编辑:程序博客网 时间:2024/04/30 01:28

js源码slide.js:

// JavaScript Document
/**
 * @function slide向一边显示块
 *  @direction objId显示块的id,direction方向,speed速度
 */
function slide(objId, direction, speed, destorySign){
 var effect = this;
 var obj = null;
 var top = 0;
 var bottom = 0;
 var left = 0;
 var right = 0;
 var container = null;
 var direction = direction;
 var speed = speed ? speed : 20;
 var timer = null;
 var destorySign = destorySign ? destorySign : false;
 var init = function(){
  obj = document.getElementById(objId);
  obj.style.position= "absolute";
  obj.style.display = "block";
  top = obj.offsetTop;
  left = obj.offsetLeft;
  var parent = obj.parentNode;
  container = document.createElement("div");
  container.style.overflow = "hidden";
  container.style.position = "absolute";
  container.style.zIndex = 1000;
  container.appendChild(obj);
  parent.appendChild(container);
  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";
  }
  if(direction == "up"){
   obj.style.top = 0-obj.offsetHeight+"px";
   obj.style.left = left+"px";
  }
  else if(direction == "down"){
   obj.style.top = container.offsetHeight+"px";
   obj.style.left = left+"px";
  }
  else if(direction == "left"){
   obj.style.left = 0-obj.offsetWidth+"px";
   obj.style.top = top+"px";
  }
  else{
   obj.style.left = container.offsetWidth+"px";
   obj.style.top = top+"px";
  }
  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";
   else{
    obj.style.top = top+"px";
    clearInterval(timer);
    if(func)
     func();
   }
  }
  else if(direction == "down"){
   if(obj.offsetTop - speed >= top)
    obj.style.top = obj.offsetTop - speed +"px";
   else{
    obj.style.top = top+"px";
    clearInterval(timer);
    if(func)
     func();
   }
  }
  else if(direction == "left"){
   if(obj.offsetLeft + speed <= left)
    obj.style.left = obj.offsetLeft + speed + "px";
   else{
    obj.style.left = left+"px";
    clearInterval(timer);
    if(func)
     func();
   }
  }
  else{
   if(obj.offsetLeft - speed >= left)
    obj.style.left = obj.offsetLeft - speed  + "px";
   else{
    obj.style.left = left+"px";
    clearInterval(timer); 
    if(func)
     func();
   }
  }
  
 };
 this.animateHide = function(func){
  container.style.display = "block";
  if(direction == "up"){
   if(obj.offsetTop - speed >= 0-obj.offsetHeight){
    obj.style.top = obj.offsetTop - speed + "px";
   }
   else{
    obj.style.top = 0-obj.offsetHeight + "px";
    container.style.display = "none";
    clearInterval(timer);
    this.destory();
    if(func)
     func();
   }
  }
  else if(direction == "down"){
   if(obj.offsetTop + speed <= container.offsetHeight)
    obj.style.top = obj.offsetTop + speed +"px";
   else{
    obj.style.top = container.offsetHeight+"px";
    container.style.display = "none";
    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";
   else{
    obj.style.left = 0 - right + "px";
    container.style.display = "none";
    clearInterval(timer);
    this.destory();
    if(func)
     func();
   }
  }
  else{
   if(obj.offsetLeft + speed <= container.offsetWidth)
    obj.style.left = obj.offsetLeft + speed  + "px";
   else{
    obj.style.left = container.offsetWidth+"px";
    container.style.display = "none";
    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>slide</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">
     <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="slide.js"></script>
<script language="javascript">
var m = new menu("menu_title", "menu_list", "blind", "up");
function menu(menuId, menuListId, effect, direction){
 var effectObj = null;
 var menu = null;
 var init = function(){
  menu = document.getElementById(menuId);
  effectObj = slide(menuListId, direction, 30);
  menu.parentNode.onmouseover = effectObj.show;
  menu.parentNode.onmouseout = effectObj.hide;
 };
 init();
 return this;
};
</script>
</html>

原创粉丝点击