强悍的JS托动DIV

 

<!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" />

  <head>

    <title> drag demo change size or change layout - http://www.never-online.net </title>

    <meta http-equiv="ImageToolbar" content="no" />

    <meta name="author" content="never-online, BlueDestiny"/>

    <meta name="keywords" content="drag, custom layout, never modules, Mozilla CSS, C#, .net, Reference, BlueDestiny, never-online"/>

    <meta name="description" content="drag demo change size or change layout, javascript reference, c sharp artilces"/>

    <meta name="creator.name" content="never-online, BlueDestiny" />

    <style type="text/css" media="all" title="Default">

      #dragDiv   { -moz-user-select:none; position:absolute; border:1px double #000; background-color:buttonface; width:200px; height:100px; color:#CC0000; text-align:center;}

    </style>

  </head>

  <body id="www.never-online.net">

     <div class="wraper">

    <div class="content">

    <input type="radio" value="size" name="custom" id="size" checked/>

    <label for="size">拖动改变尺寸</label>

    <input type="radio" value="layout" name="custom" id="layout"/>

    <label for="layout">拖动改变布局</label>

    </div>

    </div>

    <div id="dragDiv">

<input type=button />

</div>

    <script type="text/javascript">

    //<![CDATA[

      var getAbsoluteCoords = function (e) {

        var width = e.offsetWidth;

        var height = e.offsetHeight;

        var left = e.offsetLeft;

        var top = e.offsetTop;

        //while (e=e.offsetParent) {

         // left += e.offsetLeft;

         // top  += e.offsetTop;

       // };

        var right = left+width;

        var bottom = top+height;

        return {

          'width': width,

          'height': height,

          'left': left,

          'top': top,

          'right': right,

          'bottom': bottom

        };

      };

      var getElementById = function (sId) {

        return document.getElementById(String(sId));

      };

      /* 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";

          };

        };

      }

    //]]>

    </script>

  </body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值