js div層漸變方式開啟和關閉效果程式碼例項

admin發表於2017-04-06

本章節分享一段程式碼例項,它實現了點選按鈕開啟或者關閉一個層的功能。

並且在開啟和關閉過程中具有漸變效果,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
body,span,div,td{
  font-size:12px;
  line-height:1.5em;
  color:#849BCA;
}
#bodyL{
  float:left;
  width:84px;
  margin-right:2px;
}
a.od{
  width:80px;
  height:25px;
  line-height:25px;
  text-align:center;
  font-weight:bold;
  border: 2px solid #849BCA;
  display:block;
  color:#547BC9;
  float:left;
  text-decoration:none;
  margin-top:2px;
}
a.od:link{
  background:#EEF1F8;
}
a.od:visited{
  background:#EEF1F8;
}
a.od:hover{
  background:#EEE;
}
a.od:active{
  background:#EEE;
}
#fd{
  width:500px;
  height:200px;
  background:#EDF1F8;
  border: 2px solid #849BCA;
  margin-top:2px;
  margin-left:2px;
  float:left;
  overflow:hidden;
  position:absolute;
  left:0px;
  top:0px;
  cursor:move;
  float:left;
}
.content{
  padding:10px;
}
</style>
<script type="text/javascript">
window.onload=function(){
  var prox;
  var proy;
  var proxc;
  var proyc;
  function show(id){/*--開啟--*/
    clearInterval(prox);
    clearInterval(proy);
    clearInterval(proxc);
    clearInterval(proyc);
    var o = document.getElementById(id);
    o.style.display = "block";
    o.style.width = "1px";
    o.style.height = "1px";
    prox = setInterval(function(){openx(o,500)},10);
  }
  function openx(o,x){/*--開啟x--*/
    var cx = parseInt(o.style.width);
    if(cx < x){
      o.style.width = (cx + Math.ceil((x-cx)/5)) +"px";
    }
    else{
      clearInterval(prox);
      proy = setInterval(function(){openy(o,200)},10);
    }
  }
  function openy(o,y){/*--開啟y--*/
    var cy = parseInt(o.style.height);
    if(cy < y){
      o.style.height = (cy + Math.ceil((y-cy)/5)) +"px";
    }
    else{
      clearInterval(proy);
    }
  }
  function closeed(id){/*--關閉--*/
    clearInterval(prox);
    clearInterval(proy);
    clearInterval(proxc);
    clearInterval(proyc);
    var o = document.getElementById(id);
    if(o.style.display == "block"){
      proyc = setInterval(function(){closey(o)},10);
    }
  }
  function closey(o){/*--開啟y--*/
    var cy = parseInt(o.style.height);
    if(cy > 0){
      o.style.height = (cy - Math.ceil(cy/5)) +"px";
    }
    else{
      clearInterval(proyc);
      proxc = setInterval(function(){closex(o)},10);
    }
  }
  function closex(o){/*--開啟x--*/
    var cx = parseInt(o.style.width);
    if(cx > 0){
      o.style.width = (cx - Math.ceil(cx/5)) +"px";
    }
    else{
      clearInterval(proxc);
      o.style.display = "none";
    }
  }
  /*滑鼠拖動*/
  var od = document.getElementById("fd");
  var dx,dy,mx,my,mouseD;
  var odrag;
  var isIE = document.all ? true : false;
  document.onmousedown = function(e){
    var e = e ? e : event;
    if(e.button == (document.all ? 1 : 0)){
      mouseD = true;
    }
  }
  document.onmouseup = function(){
    mouseD = false;
    odrag = "";
    if(isIE){
      od.releaseCapture();
      od.filters.alpha.opacity = 100;
    }
    else{
      window.releaseEvents(od.MOUSEMOVE);
      od.style.opacity = 1;
    }
  }
  od.onmousedown = function(e){
    odrag = this;
    var e = e ? e : event;
    if(e.button == (document.all ? 1 : 0)){
      mx = e.clientX;
      my = e.clientY;
      od.style.left = od.offsetLeft + "px";
      od.style.top = od.offsetTop + "px";
      if(isIE){
        od.setCapture();
        od.filters.alpha.opacity = 50;
      }
      else{
        window.captureEvents(Event.MOUSEMOVE);
        od.style.opacity = 0.5;
      }
    }
  }
  document.onmousemove = function(e){
    var e = e ? e : event;
    if(mouseD==true && odrag){
      var mrx = e.clientX - mx;
      var mry = e.clientY - my;
      od.style.left = parseInt(od.style.left) +mrx + "px";
      od.style.top = parseInt(od.style.top) + mry + "px";
      mx = e.clientX;
      my = e.clientY;
        }
  }
  var openW=document.getElementById("open");
  var closeW=document.getElementById("close");
  openW.onclick=function(){
    show('fd');
        return false;
  }
  closeW.onclick=function(){
    closeed('fd');
        return false;
  }
}
</script>
</head>
<body>
<div id="bodyL">
  <a href="#" class="od" id="open">[開啟層]</a>
  <a href="#" class="od" id="close">[關閉層]</a>
</div>
<div id="fd" style="display:none;filter:alpha(opacity=100);opacity:1;">
  <div class="content">移動層</div>
</div>
</body>
</html>

相關文章