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

本文介绍了一种使用十行代码实现拖动层的方法,该方法通过监听鼠标按下、移动和释放事件来改变元素的位置或尺寸,并且兼容多种浏览器。

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

十行代码即可写出兼容版拖动层
[ 2007-04-10 22:29:16 | Author: never-online ]
Font Size: Large | Medium | Small
Close Advertisement
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
DEMO的地址: http://www.never-online.net/code/js/dragdemo/

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

核心代码如下:
      /* Kernel code, drag div change the coords */
      /* by never-online, http://www.never-online.net */
    
      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";
          };
        };
      }

思路就不用说了,就是down->move->up几个事件。没有用captureEvent,主要是因为在down事件后,才把move的事件写出来,而up的时候把拖动的事件release。这种方法简单,而且可兼容其它的浏览器,可以说是“价美物廉”的产品了,我现在一般写拖动层也都是应用这种模式,也许可以想想更简单有效的方法。上面的代码兼容了改变尺寸和改变布局的代码,所以多出了很多判断的代码,如果不做判断,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";
          };
        };
      }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值