拖动层的javasvript代码 十行代码即可写出兼容版拖动层

 

这是帮一个CSDN网友写的一个拖动层改变层尺寸DEMO

核心代码如下:


    
      
var  wrapId  =   " dragDiv " var  wrap  =  getElementById(wrapId); 
      
var  isChangeLayout;
      wrap.onmouseover 
=   function  ()  {
        isChangeLayout
=getElementById('layout').checked?true:false;
        wrap.style.cursor 
= isChangeLayout?"move":"se-resize";
        
if (window.ActiveXObject) 
          wrap.onselectstart 
= function () { event.returnValue = false; }
        document.onmousedown 
= function (evt) {
          
/* save the original coordinates */
          evt 
= window.event||evt; var a=getAbsoluteCoords(wrap);
          wrap.cx
=evt.clientX-(isChangeLayout?a.left:a.width); 
          wrap.cy
=evt.clientY-(isChangeLayout?a.top:a.height);
          document.onmousemove 
= function (evt) {
            
/* change the coords when mouse is moveing */
            evt 
= window.event||evt; try {
              
if (isChangeLayout) {
                wrap.style.left 
= (evt.clientX-wrap.cx)+"px";
                wrap.style.top 
= (evt.clientY-wrap.cy)+"px";
              }
 else {
                wrap.style.width 
= (evt.clientX-wrap.cx)+"px";
                wrap.style.height 
= (evt.clientY-wrap.cy)+"px";
              }

            }
 catch (ex) {};
          }
;
          document.onmouseup 
= function () {
            
/* drag end release the event */
            document.onmousemove 
= null;
            document.onmouseup 
= null;
            wrap.style.cursor
="default";
          }
;
        }
;
      }

 



上面的代码兼容了改变尺寸和改变布局的代码,所以多出了很多判断的代码,如果不做判断,10的确是可以把拖动的代码写完的。去掉判断语句即可。如下:

 

       var  wrapId  =   " dragDiv " var  wrap  =  getElementById(wrapId); 
      wrap.onmouseover 
=   function  ()  {
        wrap.style.cursor 
= "se-resize";
        document.onmousedown 
= function (evt) {
          evt 
= window.event||evt; var a=getAbsoluteCoords(wrap);
          wrap.cx
=evt.clientX-a.width; wrap.cy=evt.clientY-a.height
          document.onmousemove 
= function (evt) {
            evt 
= window.event||evt; 
            wrap.style.width 
= (evt.clientX-wrap.cx)+"px";
            wrap.style.height 
= (evt.clientY-wrap.cy)+"px";
          }
;
          document.onmouseup 
= function () {
            document.onmousemove 
= null;
            document.onmouseup 
= null;
            wrap.style.cursor
="default";
          }
;
        }
;
      }

demo:http://www.never-online.net/code/js/dragdemo/

转:http://www.never-online.net/blog/article.asp?id=148

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值