posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          html 簡單 div 拖動

          Posted on 2007-05-08 12:28 G_G 閱讀(3896) 評論(5)  編輯  收藏 所屬分類: javascript
          問題:? 興趣
          解決:?? div的 onclike ( this?)? 得到選擇?
          ????????????由 window.event 得到 鼠標 x y?
          ????????????由 this.style.top/left 改變 div 位置?


          ????????
          var?x,y;
          ????????
          function?mousedown(obj)
          ????????{
          ????????????obj.onmousemove?
          =?mousemove;
          ????????????obj.onmouseup?
          =?mouseup;
          ????????????
          ????????????oEvent?
          =?window.event???window.event?:?event;
          ????????????x?
          =?oEvent.clientX;
          ????????????y?
          =?oEvent.clientY;
          ????????}
          ????????
          function?mousemove()
          ????????{
          ????????????oEvent?
          =?window.event???window.event?:?event;
          ????????????
          var?_top?=?oEvent.clientY?-?y?+?parseInt(this.style.top)?+?"px"; // oEvent.clientY?-?y? 為div 上 移動的位置 再反映到 ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?? //parseInt(this.style.top)
          ????????????var?_left?=?oEvent.clientX?-?x?+?parseInt(this.style.left)?+"px";
          ????????????
          this.style.top?=?_top;
          ????????????
          this.style.left?=?_left;
          ????????????x?
          =??oEvent.clientX;
          ????????????y?
          =??oEvent.clientY
          ????????}
          ????????
          function?mouseup()
          ????????{
          ????????????
          this.onmousemove?=?null;
          ????????????
          this.onmouseup?=?null;
          ????????}

          心得: 興趣決定一切

          評論

          # re: html 簡單 div 拖動  回復  更多評論   

          2007-05-08 12:45 by G_G
          <div id="div1" style="width: 100px; height: 100px; top:10px; left:15px; cursor:move; background-color:Blue; position:absolute;" onmousedown="mousedown(this)" > </div>

          # 拖動 帶點   回復  更多評論   

          2007-05-08 13:14 by G_G


          var start = '<script src=test.js></script><body ondblclick=test()>';
          var end = '</body>';

          function test(){

          oEvent = window.event ? window.event : event;

          start += '<div id="div1" style="width: 100px; height: 100px; top:'+oEvent.clientY+'px; left:'+oEvent.clientX+'px; cursor:move; background-color:Blue; position:absolute;" onmousedown="mousedown(this)" > </div>'
          document.write(start+end);
          }

          function mousedown(obj)
          {
          obj.onmousemove = mousemove;
          obj.onmouseup = mouseup;

          oEvent = window.event ? window.event : event;
          x = oEvent.clientX;
          y = oEvent.clientY;
          }
          function mousemove()
          {
          oEvent = window.event ? window.event : event;
          var _top = oEvent.clientY - y + parseInt(this.style.top) + "px";
          var _left = oEvent.clientX - x + parseInt(this.style.left) +"px";
          this.style.top = _top;
          this.style.left = _left;
          x = oEvent.clientX;
          y = oEvent.clientY
          }
          function mouseup()
          {
          this.onmousemove = null;
          this.onmouseup = null;
          }

          # 拖動 帶點   回復  更多評論   

          2007-05-08 13:14 by G_G
          <script src='test.js'></script>
          <body ondblclick='test()'>

          </body>

          # 半終結 版 哈哈 [未登錄]  回復  更多評論   

          2007-05-08 17:28 by G_G

          function test(){
          e = window.event ? window.event : event;
          var x = e.clientY;
          var y = e.clientX;
          var div = document.createElement("<div onmousedown=mousedown(this)>");
          div.style.top = x;
          div.style.left = y;
          div.style.width = 50 ;
          div.style.height = 50;
          div.style.cursor = "move";
          div.style.position= "absolute";
          div.style.backgroundColor = "Blue";
          document.body.appendChild(div);
          }



          function mousedown(obj)
          {
          obj.onmousemove = mousemove;
          obj.onmouseup = mouseup;

          oEvent = window.event ? window.event : event;
          x = oEvent.clientX;
          y = oEvent.clientY;
          }
          function mousemove()
          {
          oEvent = window.event ? window.event : event;
          var _top = oEvent.clientY - y + parseInt(this.style.top) + "px";
          var _left = oEvent.clientX - x + parseInt(this.style.left) +"px";
          this.style.top = _top;
          this.style.left = _left;
          x = oEvent.clientX;
          y = oEvent.clientY
          }
          function mouseup()
          {
          this.onmousemove = null;
          this.onmouseup = null;
          }

          # re: html 簡單 div 拖動  回復  更多評論   

          2010-04-14 09:59 by 火星華人
          好。
          主站蜘蛛池模板: 灵宝市| 兴业县| 武功县| 桦甸市| 巴林右旗| 宁晋县| 镇巴县| 邹平县| 山阳县| 拉萨市| 德昌县| 德阳市| 宣威市| 修武县| 逊克县| 锡林浩特市| 清水县| 重庆市| 随州市| 垦利县| 普兰店市| 尼玛县| 莆田市| 千阳县| 邮箱| 兖州市| 吉木萨尔县| 扬州市| 孟津县| 贵定县| 屏东市| 巴彦淖尔市| 大厂| 吉林市| 广灵县| 彭阳县| 个旧市| 蓬溪县| 江都市| 门头沟区| 札达县|