Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30462430
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-14 11:50:47

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="">
  3.  <head>
  4.   <title> DragDrop </title>
  5.   <style type="text/css">
  6.   body {
  7.     padding: 0px;
  8.     margin: 0px;
  9.     font-size: 12px;
  10.   }

  11.   #drager {
  12.     width: 150px;
  13.     height: 100px;
  14.     background-color: #85053C;
  15.   }

  16.   #handler {
  17.     width: 100%;
  18.     height: 30px;
  19.     background-color: #FFCC66;
  20.     text-align: center;
  21.   }
  22.   </style>
  23.   <script type="text/javascript">
  24.     var $ = function(id) {
  25.         return 'string' == typeof id ? document.getElementById(id) : id;
  26.     }

  27.     var $d = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
  28.     
  29.     var isIE = navigator.userAgent.indexOf('MSIE') != -1;
  30.     function addEvent(oTarget, sType, fnHandler){
  31.         if(window.attachEvent){
  32.             oTarget.attachEvent("on"+sType, fnHandler)
  33.         }else if(window.addEventListener){
  34.             oTarget.addEventListener(sType, fnHandler, false);
  35.         }else{
  36.             oTarget["on"+sType] = fnHandler;
  37.         }
  38.     }

  39.     function removeEvent(oTarget, sType, fnHandler){
  40.         if(window.detachEvent){
  41.             oTarget.detachEvent("on"+sType, fnHandler);
  42.         }else if(window.removeEventListener){
  43.             oTarget.removeEventListener(sType, fnHandler, false);
  44.         }else {
  45.             oTarget["on"+sType] = null;
  46.         }
  47.     }

  48.     var Extend = function(destination, source){
  49.         for(var pro in source){
  50.             destination[pro] = source[pro];
  51.         }
  52.         return destination;
  53.     }

  54.     var Bind = function(object, fun){
  55.         var args = Array.prototype.slice.call(arguments, 2);
  56.         return function(){
  57.             return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
  58.         }
  59.     }

  60.     var BindAsEventListener = function(object, fun){
  61.         var args = Array.prototype.slice.call(arguments, 2);
  62.         return function(event){
  63.             return fun.apply(object, [event || window.event].concat(args));
  64.         }
  65.     }

  66.     var Class = {
  67.         create: function(){
  68.             return function(){
  69.                 this.initialize.apply(this, arguments);
  70.             }
  71.         }
  72.     }

  73.     var Drag = Class.create();
  74.     Drag.prototype = {
  75.         initialize: function(dragId, options){
  76.             var oThis = this;
  77.             this.Drager = $(dragId);
  78.             this.DisX = this.DisY = 0;
  79.             this.SetOptions(options);
  80.             Extend(this, this.options);
  81.             this.o = this.Handler || this.Drager;
  82.             with(this.Drager.style){position = position != "absolute" && "absolute"};
  83.             this._fM = BindAsEventListener(this, this.MouseMove);
  84.             this._fU = Bind(this, this.MouseUp);
  85.             addEvent(this.o, "mousedown", BindAsEventListener(this, oThis.MouseDown));
  86.             addEvent(this.o, "mouseover", function(){oThis.o.style.cursor = "pointer"});

  87.         },

  88.         SetOptions: function(options){
  89.             this.options = {
  90.                 isLockX: false,
  91.                 isLockY: false,
  92.                 isLock: false,
  93.                 isResize: true,
  94.                 isLimit: true,
  95.                 Handler: $('handler')
  96.             };
  97.             Extend(this.options, options || {});
  98.         },

  99.         MouseDown: function(event){
  100.             if(this.isLock) { this.MouseUp(); return;}
  101.             this.DisX = event.clientX - this.Drager.offsetLeft;
  102.             this.DisY = event.clientY - this.Drager.offsetTop;
  103.             isIE && this.Drager.setCapture();
  104.             addEvent(document,"mousemove",this._fM);
  105.             addEvent(document,"mouseup",this._fU);
  106.         },

  107.         MouseMove: function(event){
  108.             window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  109.             var iLeft = event.clientX - this.DisX; var iTop = event.clientY - this.DisY;
  110.             if(this.isLimit){
  111.                 var mxLeft = 0; var mxRight = $d.clientWidth;
  112.                 var mxTop = 0; var mxBottom = $d.clientHeight;
  113.                 iLeft = Math.max(Math.min(iLeft, mxRight - this.Drager.offsetWidth), mxLeft);
  114.                 iTop = Math.max(Math.min(iTop, mxBottom - this.Drager.offsetHeight), mxTop);
  115.             }
  116.             if(!this.isLockX) { this.Drager.style.left = iLeft + "px" };
  117.             if(!this.isLockY) { this.Drager.style.top = iTop + "px" };
  118.         },

  119.         MouseUp: function(){
  120.             removeEvent(document,"mousemove",this._fM);
  121.             removeEvent(document,"mouseup",this._fU);
  122.             isIE && this.Drager.releaseCapture();
  123.         }
  124.     }

  125.   </script>
  126.  </head>

  127.  <body>
  128.      <div id="drager">
  129.         <div id="handler"> Handler </div>
  130.      </div>
  131.  <script type="text/javascript">
  132.         var drag = new Drag('drager', {isLimit:true});
  133.  </script>
  134.  </body>
  135. </html>
阅读(1317) | 评论(0) | 转发(0) |
0

上一篇:Pop up image

下一篇:javascript画圆

给主人留下些什么吧!~~