JavaScript---动态打开和关闭层,而且还能拖拽

本文介绍了一种使用JavaScript实现DOM元素动态显示及隐藏的方法,并通过定时器逐步调整元素尺寸来达到平滑过渡的效果。此外,还实现了鼠标拖拽层的功能,使层能够被用户拖动到页面任意位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>DOM_text01</title>
  6. <style type="text/css">
  7. body,span,div,td{font-size:12px;line-height:1.5em;color:#849BCA;}
  8. #bodyL{
  9.     float:left;
  10.     width:84px;
  11.     margin-right:2px;
  12. }
  13. a.od{
  14.     width:80px;
  15.     height:25px;
  16.     line-height:25px;
  17.     text-align:center;
  18.     font-weight:bold;
  19.     border: 2px solid #849BCA;  
  20.     display:block;
  21.     color:#547BC9;
  22.     float:left;
  23.     text-decoration:none;
  24.     margin-top:2px;
  25. }
  26. a.od:link{
  27.     background:#EEF1F8;
  28. }
  29. a.od:visited{
  30.     background:#EEF1F8;
  31. }
  32. a.od:hover{
  33.     background:#EEE;
  34. }
  35. a.od:active{
  36.     background:#EEE;
  37. }
  38. #fd{    
  39.     width:500px;
  40.     height:200px;
  41.     background:#EDF1F8; 
  42.     border: 2px solid #849BCA;
  43.     margin-top:2px;
  44.     margin-left:2px;
  45.     float:left;
  46.     overflow:hidden;
  47.     position:absolute;
  48.     left:100px;
  49.     top:100px;
  50.     cursor:move;
  51.     float:left;
  52.     /*filter:alpha(opacity=50);*/
  53.     
  54. }
  55. .content{
  56.     padding:10px;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div id="bodyL">
  62.     <a href="#" class="od" onclick = "show('fd');return false;">
  63.         [打开层]
  64.     </a>
  65.     <a href="#" class="od" onclick = "closeed('fd');return false;">
  66.         [关闭层]
  67.     </a>
  68. </div>  

  69. <div id="fd" style="display:none;filter:alpha(opacity=100);opacity:1;">
  70.     <div class="content">移动层</div>  
  71. </div>
  72.     
  73. <script type="text/javascript">
  74.     var prox;
  75.     var proy;
  76.     var proxc;
  77.     var proyc;

  78.     function show(id){/*--打开--*/
  79.         clearInterval(prox);
  80.         clearInterval(proy);
  81.         clearInterval(proxc);
  82.         clearInterval(proyc);
  83.         var o = document.getElementById(id);
  84.         o.style.display = "block";
  85.         o.style.width = "1px";
  86.         o.style.height = "1px"
  87.         prox = setInterval(function(){openx(o,500)},10);
  88.     }   

  89.     function openx(o,x){/*--打开x--*/
  90.         var cx = parseInt(o.style.width);
  91.         if(cx < x)
  92.         {
  93.             o.style.width = (cx + Math.ceil((x-cx)/5)) +"px";
  94.         }
  95.         else
  96.         {
  97.             clearInterval(prox);
  98.             proy = setInterval(function(){openy(o,200)},10);
  99.         }
  100.     }
  101.     
  102.     function openy(o,y){/*--打开y--*/ 
  103.         var cy = parseInt(o.style.height);
  104.         if(cy < y)
  105.         {
  106.             o.style.height = (cy + Math.ceil((y-cy)/5)) +"px";
  107.         }
  108.         else
  109.         {
  110.             clearInterval(proy);            
  111.         }
  112.     }   
  113.     function closeed(id){/*--关闭--*/
  114.         clearInterval(prox);
  115.         clearInterval(proy);
  116.         clearInterval(proxc);
  117.         clearInterval(proyc);       
  118.         var o = document.getElementById(id);
  119.         if(o.style.display == "block")
  120.         {
  121.             proyc = setInterval(function(){closey(o)},10);          
  122.         }       
  123.     }   
  124.     function closey(o){/*--关闭y--*/  
  125.         var cy = parseInt(o.style.height);
  126.         if(cy > 0)
  127.         {
  128.             o.style.height = (cy - Math.ceil(cy/5)) +"px";
  129.         }
  130.         else
  131.         {
  132.             clearInterval(proyc);               
  133.             proxc = setInterval(function(){closex(o)},10);
  134.         }
  135.     }   
  136.     function closex(o){/*--关闭x--*/
  137.         var cx = parseInt(o.style.width);
  138.         if(cx > 0)
  139.         {
  140.             o.style.width = (cx - Math.ceil(cx/5)) +"px";
  141.         }
  142.         else
  143.         {
  144.             clearInterval(proxc);
  145.             o.style.display = "none";
  146.         }
  147.     }   
  148.     
  149.     
  150.     /*-------------------------鼠标拖动---------------------*/  
  151.     var od = document.getElementById("fd"); 
  152.     var dx,dy,mx,my,mouseD;
  153.     var odrag;
  154.     var isIE = document.all ? true : false;

  155.     document.onmousedown = function(e){
  156.         var ee = e ? e : event;
  157.         if(e.button == (document.all ? 1 : 0))
  158.         {
  159.             mouseD = true;          
  160.         }
  161.     }

  162.     document.onmouseup = function(){
  163.         mouseD = false;
  164.         odrag = "";
  165.         if(isIE)
  166.         {
  167.             od.releaseCapture();
  168.             od.filters.alpha.opacity = 100;
  169.         }
  170.         else
  171.         {
  172.             window.releaseEvents(od.MOUSEMOVE);
  173.             od.style.opacity = 1;
  174.         }       
  175.     }
  176.     
  177.     
  178.     //function readyMove(e){    
  179.     od.onmousedown = function(e){
  180.         odrag = this;
  181.         var ee = e ? e : event;
  182.         if(e.button == (document.all ? 1 : 0))
  183.         {
  184.             mx = e.clientX;
  185.             my = e.clientY;
  186.             odod.style.left = od.offsetLeft + "px";
  187.             odod.style.top = od.offsetTop + "px";
  188.             if(isIE)
  189.             {
  190.                 od.setCapture();                
  191.                 od.filters.alpha.opacity = 50;
  192.             }
  193.             else
  194.             {
  195.                 window.captureEvents(Event.mousemove);
  196.                 od.style.opacity = 0.5;
  197.             }
  198.             
  199.             //alert(mx);
  200.             //alert(my);
  201.             
  202.         } 
  203.     }
  204.     document.onmousemove = function(e){
  205.         var ee = e ? e : event;
  206.         
  207.         //alert(mrx);
  208.         //alert(e.button);      
  209.         if(mouseD==true && odrag)
  210.         {       
  211.             var mrx = e.clientX - mx;
  212.             var mry = e.clientY - my;   
  213.             od.style.left = parseInt(od.style.left) +mrx + "px";
  214.             od.style.top = parseInt(od.style.top) + mry + "px";         
  215.             mx = e.clientX;
  216.             my = e.clientY;
  217.             
  218.         }
  219.     }
  220.     
  221.     
  222. </script>
  223. </body>
  224. </html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值