在浏览器中dragdrop和resize对象

来源:互联网 发布:淘宝链接转换无线工具 编辑:程序博客网 时间:2024/05/24 01:41
参考了网上的一些资料,完成以下代码
  1. // 计算鼠标的当前坐标
  2. function getCursorPos(e) {
  3.     var p = {X: 0, Y: 0};
  4.     p.X = e.layerX ? e.layerX : e.offsetX;
  5.     p.Y = e.layerY ? e.layerY : e.offsetY;
  6.     var se = e.srcElement || e.target;
  7.     while (se) {
  8.         p.X += se.layerLeft ? se.layerLeft : se.offsetLeft;
  9.         p.Y += se.layerTop ? se.layerTop : se.offsetTop;
  10.         se = se.offsetParent;
  11.     }       
  12.     return p;
  13. }
  14. /* 说明
  15.  * 功能:使对象可以拖动
  16.  * 参数:
  17.  *       o   要拖动的对象
  18.  *       evt 此参数可以包含4个对象
  19.                 begin(userData, bl, bp);    // 开始拖动 userData 用户数据 bl 对象起始位置坐标 bp 鼠标起始坐标
  20.                 dragging(userData, mp, dp); // 拖动中 userData 用户数据 mp 鼠标当前坐标 dp mp相对于bp的变化
  21.                 end(userData);              // 拖动停止
  22.                 resizer                     // 如果该对象还要缩放大小,则传入,类型为Resizer
  23.  *       userData 用户数据
  24.  */
  25. function dragable(o, evt, userData) {
  26.     var oldEvent = o.onmousedown;
  27.     o.onmousedown = function(a) {
  28.         if (oldEvent)
  29.             oldEvent(a);
  30.         var d = document;
  31.         a = a || window.event;
  32.         var bl = {Y: parseInt(o.style.top), X: parseInt(o.style.left)};
  33.         var bp = getCursorPos(a);
  34.         if (evt)
  35.             if (evt.begin)
  36.                 evt.begin(userData, bl, bp);
  37.         if(o.setCapture)
  38.             o.setCapture();
  39.         else if(window.captureEvents)
  40.                 window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  41.         d.onmousemove = function(a) {
  42.             a = a || window.event;
  43.             var mp = getCursorPos(a);   // 鼠标当前坐标       
  44.             if (mp) {
  45.                 var dp = {X: mp.X- bp.X, Y: mp.Y - bp.Y};   // 鼠标位置变化值
  46.                 o.style.left = bl.X + dp.X; 
  47.                 o.style.top = bl.Y + dp.Y;
  48.                 
  49.                 if (evt) {
  50.                     if (evt.dragging)
  51.                         evt.dragging(userData, mp, dp);
  52.                     if (evt.resizer)
  53.                         evt.resizer.showTags();
  54.                 }
  55.             }       
  56.         };
  57.         d.onmouseup = function(){
  58.             if(o.releaseCapture)
  59.                 o.releaseCapture();
  60.             else if(window.captureEvents)
  61.                     window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  62.             d.onmousemove = null;
  63.             d.onmouseup = null;
  64.             if (evt)
  65.             {
  66.                 if (evt.end)
  67.                     evt.end(userData);
  68.                 if (evt.resizer)
  69.                     evt.resizer.showTags();
  70.             }
  71.         };
  72.     };
  73. }
  74. /* 说明
  75.  * 功能:使对象可以缩放,在对象可以拖动时,一般把创建的Resizer作为evt.resizer传给dragable
  76.  * 参数:
  77.  *       o   要缩放的对象
  78.  *       limit 此参数可以包含4个对象
  79.             maxWidth     最大宽度
  80.             minWidth     最小宽度
  81.             maxHeight    最大高度
  82.             minHeight    最小高度
  83.  */
  84. function Resizer(o, limit) {
  85.     var self = this;
  86.     var tags = null;
  87.     var bound = {};
  88.     function getSize() {
  89.         var style = o.style;
  90.         var left = parseInt(style.left);
  91.         var top = parseInt(style.top);
  92.         var te = o.offsetParent;
  93.         while(te && (te != o.parentNode)) {
  94.             left += te.layerLeft ? te.layerLeft : te.offsetLeft;
  95.             top += te.layerTop ? te.layerTop : te.offsetTop;
  96.             te = te.offsetParent;
  97.         }
  98.         var width = style.width == 'auto' ? obj.scrollWidth : parseInt(style.width);
  99.         var height = style.height == 'auto' ? obj.scrollHeight : parseInt(style.height);
  100.         bound = {x: left, y: top, rx: left + width, by: top + height, w: width, h: height};
  101.     }
  102.     function setPos(obj, x, y, v) {
  103.         obj.style.top = y; 
  104.         obj.style.left = x; 
  105.         obj.style.visibility = v;
  106.     }
  107.     this.showTags = function (h) {      
  108.         if (! tags) {
  109.             tags = {};
  110.             tags['nw-resize'] = document.createElement('div');
  111.             tags['n-resize'] = document.createElement('div');
  112.             tags['ne-resize'] = document.createElement('div');
  113.             tags['e-resize'] = document.createElement('div');
  114.             tags['se-resize'] = document.createElement('div');
  115.             tags['s-resize'] = document.createElement('div');
  116.             tags['sw-resize'] = document.createElement('div');
  117.             tags['w-resize'] = document.createElement('div');
  118.             var node = o.parentNode;
  119.             if (! node) node = document.body;
  120.             for (var p in tags) {
  121.                 tags[p].style.position = 'absolute';
  122.                 tags[p].style.border = 'solid 1px black';
  123.                 tags[p].style.backgroundColor = 'white';
  124.                 tags[p].style.fontSize = 1;
  125.                 tags[p].style.width = 5;
  126.                 tags[p].style.height = 5;
  127.                 tags[p].style.cursor = p;
  128.                 node.appendChild(tags[p]);
  129.                 dragable(tags[p], self, p);
  130.             }
  131.         }
  132.         getSize();
  133.         var visible = 'visible';
  134.         if (h) visible = 'hidden';
  135.         setPos(tags['nw-resize'], bound.x - 1, bound.y - 1, visible);
  136.         setPos(tags['n-resize'], bound.x + bound.w / 2 - 1, bound.y - 1, visible);
  137.         setPos(tags['ne-resize'], bound.rx - 2, bound.y - 1, visible);
  138.         setPos(tags['e-resize'], bound.rx - 2, bound.y + bound.h / 2 , visible);
  139.         setPos(tags['se-resize'], bound.rx - 2, bound.by - 2, visible);
  140.         setPos(tags['s-resize'], bound.x + bound.w / 2 - 1, bound.by - 2, visible);
  141.         setPos(tags['sw-resize'], bound.x - 1, bound.by - 2, visible);
  142.         setPos(tags['w-resize'], bound.x - 1, bound.y + bound.h / 2, visible);
  143.     }
  144.     var bl = {h: parseInt(o.style.height), w: parseInt(o.style.width)};
  145.     function LimitW(w) {
  146.         if (limit) {
  147.             if (limit.maxWidth)
  148.                 if (w > limit.maxWidth)
  149.                     w = limit.maxWidth;
  150.             if (limit.minWidth)
  151.                 if (w < limit.minWidth)
  152.                     w = limit.minWidth;                 
  153.         }
  154.         return w;
  155.     }
  156.     function LimitH(h) {
  157.         if (limit) {
  158.             if (limit.maxHeight)
  159.                 if (h > limit.maxHeight)
  160.                     h = limit.maxHeight;
  161.             if (limit.minWidth)
  162.                 if (h < limit.minHeight)
  163.                     h = limit.minHeight;                    
  164.         }
  165.         return h;
  166.     }
  167.     this.dragging = function(type, mp, dp) {
  168.         var w = parseInt(o.style.width);
  169.         var h = parseInt(o.style.height);
  170.         if (type == 'e-resize') {
  171.             if (bl.w + dp.X > 0) 
  172.                 w = LimitW(bl.w + dp.X);
  173.         }
  174.         if (type == 'se-resize') {
  175.             if (bl.w + dp.X > 0) 
  176.                 w = LimitW(bl.w + dp.X);
  177.             if (bl.h + dp.Y > 0) 
  178.                 h = LimitH(bl.h + dp.Y);
  179.         }
  180.         if (type == 's-resize') {
  181.             if (bl.h + dp.Y > 0) 
  182.                 h = LimitH(bl.h + dp.Y);
  183.         }
  184.         o.style.width = w;
  185.         o.style.height = h;
  186.         self.showTags();
  187.     }
  188.     this.end = function(type) {
  189.         bl = {h: parseInt(o.style.height), w: parseInt(o.style.width)};
  190.     }
  191.     o.onmousedown = function (e) {
  192.         self.showTags();
  193.     }
  194. }
加入页面中有id为drag的对象,
如果只使用resize功能
  1. var r = new Resizer(document.getElementById('drag'), {minWidth:100, minHeight: 60, maxWidth:150, maxHeight:100});
如果只使用drag功能
  1. dragable(document.getElementById('drag'));
如果同时使用
  1. var r = new Resizer(document.getElementById('drag'), {minWidth:100, minHeight: 60, maxWidth:150, maxHeight:100});
  2. dragable(document.getElementById('drag'), {resizer: r});

原创粉丝点击