var isie = (document.all) ? true : false; var isie6 = isie && ([/msie (\d)\.0/i.exec(navigator.useragent)][0][1] == 6); function each(list, fun){ for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); } }; var $ = function (id) { return "string" == typeof id ? document.getelementbyid(id) : id; }; var class = { create: function() { return function() { this.initialize.apply(this, arguments); } } } object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; } function.prototype.bind = function(object) { var __method = this, args = array.apply(null, arguments); args.shift(); return function() { return __method.apply(object, args); } } var overlay = class.create(); overlay.prototype = { initialize: function(overlay, options) { this.lay = $(overlay);//覆盖层 //ie6设置覆盖层大小程序 this._size = function(){}; this.setoptions(options); this.color = this.options.color; this.opacity = parseint(this.options.opacity); this.zindex = parseint(this.options.zindex); this.set(); }, //设置默认属性 setoptions: function(options) { this.options = {//默认值 color: "#fff",//背景色 opacity: 50,//透明度(0-100) zindex: 1000//层叠顺序 }; object.extend(this.options, options || {}); }, //创建 set: function() { this.lay.style.display = "none"; this.lay.style.zindex = this.zindex; this.lay.style.left = this.lay.style.top = 0; if(isie6){ this.lay.style.position = "absolute"; this._size = function(){ this.lay.style.width = math.max(document.documentelement.scrollwidth, document.documentelement.clientwidth) + "px"; this.lay.style.height = math.max(document.documentelement.scrollheight, document.documentelement.clientheight) + "px"; }.bind(this); //遮盖select this.lay.innerhtml = '' } else { this.lay.style.position = "fixed"; this.lay.style.width = this.lay.style.height = "100%"; } }, //显示 show: function() { //设置样式 this.lay.style.backgroundcolor = this.color; //设置透明度 if(isie){ this.lay.style.filter = "alpha(opacity:" + this.opacity + ")"; } else { this.lay.style.opacity = this.opacity / 100; } //兼容ie6 if(isie6){ this._size(); window.attachevent("onresize", this._size); } this.lay.style.display = "block"; }, //关闭 close: function() { this.lay.style.display = "none"; if(isie6){ window.detachevent("onresize", this._size); } } }; var lightbox = class.create(); lightbox.prototype = { initialize: function(box, overlay, options) { this.box = $(box);//显示层 this.overlay = new overlay(overlay, options);//覆盖层 this.setoptions(options); this.fixed = !!this.options.fixed; this.over = !!this.options.over; this.center = !!this.options.center; this.onshow = this.options.onshow; this.box.style.zindex = this.overlay.zindex + 1; this.box.style.display = "none"; //兼容ie6用的属性 if(isie6){ this._top = this._left = 0; this._select = []; } }, //设置默认属性 setoptions: function(options) { this.options = {//默认值 fixed: false,//是否固定定位 over: true,//是否显示覆盖层 center: false,//是否居中 onshow: function(){}//显示时执行 }; object.extend(this.options, options || {}); }, //兼容ie6的固定定位程序 _fixed: function(){ var itop = document.documentelement.scrolltop - this._top + this.box.offsettop, ileft = document.documentelement.scrollleft - this._left + this.box.offsetleft; //居中时需要修正 if(this.center){ itop += this.box.offsetheight / 2; ileft += this.box.offsetwidth / 2; } this.box.style.top = itop + "px"; this.box.style.left = ileft + "px"; this._top = document.documentelement.scrolltop; this._left = document.documentelement.scrollleft; }, //显示 show: function(options) { //固定定位 if(this.fixed){ if(isie6){ //兼容ie6 this.box.style.position = "absolute"; this._top = document.documentelement.scrolltop; this._left = document.documentelement.scrollleft; window.attachevent("onscroll", this._fixed.bind(this)); } else { this.box.style.position = "fixed"; } } else { this.box.style.position = "absolute"; } //覆盖层 if(this.over){ //显示覆盖层,覆盖层自带select隐藏 this.overlay.show(); } else { //ie6需要把不在box上的select隐藏 if(isie6){ this._select = []; var othis = this; each(document.getelementsbytagname("select"), function(o){ if(othis.box.contains ? !othis.box.contains(o) : !(othis.box.comparedocumentposition(o) & 16)){ o.style.visibility = "hidden"; othis._select.push(o); } }) } } this.box.style.display = "block"; //居中 if(this.center){ this.box.style.top = this.box.style.left = "50%"; //显示后才能获取 var itop = - this.box.offsetheight / 2, ileft = - this.box.offsetwidth / 2; //ie6或不是固定定位要修正 if(!this.fixed || isie6){ itop += document.documentelement.scrolltop; ileft += document.documentelement.scrollleft; } this.box.style.margintop = itop + "px"; this.box.style.marginleft = ileft + "px"; } this.onshow(); }, //关闭 close: function() { this.box.style.display = "none"; this.overlay.close(); if(isie6){ window.detachevent("onscroll", this._fixed); each(this._select, function(o){ o.style.visibility = "visible"; }); } } }; function addeventhandler(otarget, seventtype, fnhandler) { if (otarget.addeventlistener) { otarget.addeventlistener(seventtype, fnhandler, false); } else if (otarget.attachevent) { otarget.attachevent("on" + seventtype, fnhandler); } else { otarget["on" + seventtype] = fnhandler; } }; function removeeventhandler(otarget, seventtype, fnhandler) { if (otarget.removeeventlistener) { otarget.removeeventlistener(seventtype, fnhandler, false); } else if (otarget.detachevent) { otarget.detachevent("on" + seventtype, fnhandler); } else { otarget["on" + seventtype] = null; } }; if(!isie){ htmlelement.prototype.__definegetter__("currentstyle", function () { return this.ownerdocument.defaultview.getcomputedstyle(this, null); }); } //拖放程序 var drag = class.create(); drag.prototype = { //拖放对象,触发对象 initialize: function(obj, drag, options) { var othis = this; this._obj = $(obj);//拖放对象 this.drag = $(drag);//触发对象 this._x = this._y = 0;//记录鼠标相对拖放对象的位置 //事件对象(用于移除事件) this._fm = function(e){ othis.move(window.event || e); } this._fs = function(){ othis.stop(); } this.setoptions(options); this.limit = !!this.options.limit; this.mxleft = parseint(this.options.mxleft); this.mxright = parseint(this.options.mxright); this.mxtop = parseint(this.options.mxtop); this.mxbottom = parseint(this.options.mxbottom); this.mxcontainer = this.options.mxcontainer; this.onmove = this.options.onmove; this.lock = !!this.options.lock; this._obj.style.position = "absolute"; addeventhandler(this.drag, "mousedown", function(e){ othis.start(window.event || e); }); }, //设置默认属性 setoptions: function(options) { this.options = {//默认值 limit: false,//是否设置限制(为true时下面参数有用,可以是负数) mxleft: 0,//左边限制 mxright: 0,//右边限制 mxtop: 10,//上边限制 mxbottom: 0,//下边限制 mxcontainer: null,//指定限制在容器内 onmove: function(){},//移动时执行 lock: false//是否锁定 }; object.extend(this.options, options || {}); }, //准备拖动 start: function(oevent) { if(this.lock){ return; } //记录鼠标相对拖放对象的位置 this._x = oevent.clientx - this._obj.offsetleft; this._y = oevent.clienty - this._obj.offsettop; //mousemove时移动 mouseup时停止 addeventhandler(document, "mousemove", this._fm); addeventhandler(document, "mouseup", this._fs); //使鼠标移到窗口外也能释放 if(isie){ addeventhandler(this.drag, "losecapture", this._fs); this.drag.setcapture(); }else{ addeventhandler(window, "blur", this._fs); } }, //拖动 move: function(oevent) { //判断是否锁定 if(this.lock){ this.stop(); return; } //清除选择(ie设置捕获后默认带这个) window.getselection && window.getselection().removeallranges(); //当前鼠标位置减去相对拖放对象的位置得到offset位置 var ileft = oevent.clientx - this._x, itop = oevent.clienty - this._y; //设置范围限制 if(this.limit){ //如果设置了容器,因为容器大小可能会变化所以每次都要设 if(!!this.mxcontainer){ this.mxleft = this.mxtop = 0; this.mxright = this.mxcontainer.clientwidth; this.mxbottom = this.mxcontainer.clientheight; } //获取超出长度 var iright = ileft + this._obj.offsetwidth - this.mxright, ibottom = itop + this._obj.offsetheight - this.mxbottom; //这里是先设置右边下边再设置左边上边,可能会不准确 if(iright > 0) ileft -= iright; if(ibottom > 0) itop -= ibottom; if(this.mxleft > ileft) ileft = this.mxleft; if(this.mxtop > itop) itop = this.mxtop; } //设置位置 //由于offset是把margin也算进去的所以要减去 this._obj.style.left = ileft - (parseint(this._obj.currentstyle.marginleft) || 0) + "px"; this._obj.style.top = itop - (parseint(this._obj.currentstyle.margintop) || 0) + "px"; //附加程序 this.onmove(); }, //停止拖动 stop: function() { //移除事件 removeeventhandler(document, "mousemove", this._fm); removeeventhandler(document, "mouseup", this._fs); if(isie){ removeeventhandler(this.drag, "losecapture", this._fs); this.drag.releasecapture(); }else{ removeeventhandler(window, "blur", this._fs); } } };