js特效-两边向中显示菜单、窗口

js源码:

// JavaScript Document
/**
 * @function 同时向两边显示
 * @direction objId显示块id,direction方向v为垂直,h为水平
 */
function clip(objId, direction, speed, destorySign){
 var effect = this;
 var obj = null;
 var objArr = new Array();
 var direction = direction;
 var speed = speed ? speed : 20;
 var timer = null;
 var left = 0;
 var top = 0;
 var width = 0;
 var height = 0;
 var destorySign = destorySign ? destorySign : false;
 var init = function(){
  obj = document.getElementById(objId);
  obj.style.position = "absolute";
  obj.style.display = "block";
  obj.style.zIndex = 1000;
  top = obj.offsetTop;
  left = obj.offsetLeft;
  width = obj.offsetWidth;
  height = obj.offsetHeight;
  obj.style.top = top + "px";
  obj.style.left = left + "px";
  obj.style.margin = "0px";
  for(var i = 0; i < 2; i++){
   var temp = obj.cloneNode(true);
   temp.setAttribute('id', '');
   var div = document.createElement("div");
   div.style.width = width + "px";
   div.style.height = height + "px";
   div.style.left = left + "px";
   div.style.top = top + "px";
   if(direction == "v")
    div.style.height = "0px";
   else
    div.style.width = "0px";
   div.style.position = "absolute";
   div.style.overflow = "hidden";
   div.style.display = "none";
   div.appendChild(temp);
   objArr.push(div);
   obj.parentNode.appendChild(div);
  }
  if(direction == "v"){
   objArr[0].style.top=top+"px";
   objArr[0].childNodes[0].style.top = "0px";
   objArr[0].childNodes[0].style.left = "0px";
   objArr[1].style.top=top+height+"px";
   objArr[1].childNodes[0].style.top = 0-height+"px";
   objArr[1].childNodes[0].style.left = "0px";
  }else{
   objArr[0].style.left=left+"px";
   objArr[0].childNodes[0].style.left = "0px";
   objArr[0].childNodes[0].style.top = "0px";
   objArr[1].style.left=left+width+"px";
   objArr[1].childNodes[0].style.left = 0-width+"px";
   objArr[1].childNodes[0].style.top = "0px";
  }
  objArr[0].style.zIndex = 1000;
  objArr[1].style.zIndex = 1000;
  obj.style.zIndex = 1000;
  obj.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){
  objArr[0].style.display = "block";
  objArr[1].style.display = "block";
  if(direction == "v"){
   if(objArr[0].offsetHeight + speed <= height / 2){
    objArr[0].style.height = objArr[0].offsetHeight + speed + "px";
    objArr[0].style.top = top+"px";
    objArr[0].childNodes[0].style.top = "0px";
    
    objArr[1].style.height = objArr[1].offsetHeight + speed + "px";
    objArr[1].style.top = objArr[1].offsetTop - speed + "px";
    objArr[1].childNodes[0].style.top = objArr[1].childNodes[0].offsetTop + speed + "px";
   }
   else{
    objArr[0].style.height = height /2 + "px";
    objArr[0].style.top = top+"px";
    objArr[0].childNodes[0].style.top = "0px";
    
    objArr[1].style.height = height /2 + "px";
    objArr[1].style.top = top+height/2 + "px";
    objArr[1].childNodes[0].style.top = 0-height/2+"px";
    
    objArr[0].style.display = "none";
    objArr[1].style.display = "none";
    obj.style.display = "block";
    clearInterval(timer);
    
    if(func)
     func();
   }
  }
  else{
   if(objArr[0].offsetWidth + speed <= width / 2){
    objArr[0].style.width = objArr[0].offsetWidth + speed + "px";
    objArr[0].style.left = left+"px";
    objArr[0].childNodes[0].style.left = "0px";
    
    objArr[1].style.width= objArr[1].offsetWidth + speed + "px";
    objArr[1].style.left = objArr[1].offsetLeft - speed + "px";
    objArr[1].childNodes[0].style.left = objArr[1].childNodes[0].offsetLeft + speed + "px";
   }
   else{
    objArr[0].style.width = width / 2 + "px";
    objArr[0].style.left = left+"px";
    objArr[0].childNodes[0].style.left = "0px";
    
    objArr[1].style.width = width /2 + "px";
    objArr[1].style.left = left+width / 2 + "px";
    objArr[1].childNodes[0].style.left = 0-width/2+"px";
    
    objArr[0].style.display = "none";
    objArr[1].style.display = "none";
    obj.style.display = "block";
    clearInterval(timer);
 
    if(func)
     func();
   }
  }
 };
 this.animateHide = function(func){
  objArr[0].style.display = "block";
  objArr[1].style.display = "block";
  obj.style.display = "none";
  if(direction == "v"){
   if(objArr[0].offsetHeight - speed >= 0){
    objArr[0].style.height = objArr[0].offsetHeight - speed + "px";
    objArr[0].style.top = top+"px";
    objArr[0].childNodes[0].style.top = "0px";
    
    objArr[1].style.height = objArr[1].offsetHeight - speed + "px";
    objArr[1].style.top = objArr[1].offsetTop + speed + "px";
    objArr[1].childNodes[0].style.top = objArr[1].childNodes[0].offsetTop - speed + "px";
   }
   else{
    objArr[0].style.height = "0px";
    objArr[0].style.top = top+"px";
    objArr[0].childNodes[0].style.top = "0px";
    
    objArr[1].style.height = "0px";
    objArr[1].style.top = top+height+"px";
    objArr[1].childNodes[0].style.top = 0 - height + "px";
    
    objArr[0].style.display = "none";
    objArr[1].style.display = "none";
    obj.style.display = "none";
    clearInterval(timer);
    
    this.destory();
    if(func)
     func();
   }
  }
  else{
   if(objArr[0].offsetWidth - speed >= 0){
    objArr[0].style.width = objArr[0].offsetWidth - speed + "px";
    objArr[0].style.left = left+"px";
    objArr[0].childNodes[0].style.left = "0px";
    
    objArr[1].style.width= objArr[1].offsetWidth - speed + "px";
    objArr[1].style.left = objArr[1].offsetLeft + speed + "px";
    objArr[1].childNodes[0].style.left = objArr[1].childNodes[0].offsetLeft - speed + "px";
   }
   else{
    objArr[0].style.width = "0px";
    objArr[0].style.left = left+"px";
    objArr[0].childNodes[0].style.left = "0px";
    
    objArr[1].style.width = "0px";
    objArr[1].style.left = left + width + "px";
    objArr[1].childNodes[0].style.left = 0-width+"px";
    clearInterval(timer);

    this.destory();
    if(func)
     func();
   }
  }
 };
 this.destory  = function(){
  if(destorySign){
   var parent = obj.parentNode;
   for(var i = 0; i < objArr.length; i++){
    parent.removeChild(objArr[i]);
   }
   obj.removeAttribute("style");
  }
 };
 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>clip</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>
<div id="text">
</div>
</body>
<script language="javascript" src="clip.js"></script>
<script language="javascript">
var clip_menu = menu("menu_title", "menu_list", "clip", "h");
function menu(menuId, menuListId, effect, direction){
 var effectObj = null;
 var menu = null;
 var init = function(){
  menu = document.getElementById(menuId);
  effectObj = clip(menuListId, direction, 10);
  menu.parentNode.onmouseover = effectObj.show;
  menu.parentNode.onmouseout = effectObj.hide;
 };
 init();
 return this;
};
</script>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值