var zindex=100; //弹出窗口插件 (function($){ var b=null; var c=null; $.fn.window=function(options){ $win=$(window); var options=$.extend({},$.fn.window.defaults,options); return this.each(function(){ var _this=this; if(options.shadow) b = $("
").hide().addclass('win_overlaybg').css('opacity', options.opacity).dblclick(function(){ if(b!=null)b.remove(); $(_this).css("display","none"); }).appendto('body').show(); if($(_this).find(".window-header").length==0){ zindex=zindex+1; //win标题 var title_name=""; $(_this).attr("title")?title_name=$(_this).attr("title") : title_name=options.title; var iframestr="" +"
"+title_name+"
"; $("body").append($(_this).wrapinner("
").prepend(iframestr)); } //关闭win win_close=function(e){ if(b!=null)b.remove(); var st=$(e).parent().parent(); if(options.cleardata) st.html(""); st.css("display","none"); } if(options.closed){ if(options.cleardata) $(_this).html(""); $(_this).css("display","none"); if(b!=null)b.remove(); }else{ $(_this).css("z-index",zindex); handleclick(); $(_this).show(); if(options.move)drag(); setwincenter(); } function handleclick(){ var con = $(_this).find(".wincontent"); if (options.requesttype && $.inarray(options.requesttype, ['iframe', 'ajax','img'])!=-1) { con.html("
"); if (options.requesttype === "img") { var img = $(""); img.attr("src",options.target); img.appendto(con.empty()); $(_this).find(".window-header").width(img.width()); setwincenter(); } else if (options.requesttype === "ajax") { $.get(options.target, function(data){ con.html(data); $(_this).find(".window-header").width($(_this).width()); setwincenter(); }); } else { ifr = $(""); ifr.appendto(con.empty()); ifr.load(function(){ try { $it = $(this).contents(); fh = $it.height();//iframe height fw = $it.width(); w = $win; neww = math.min(w.width() - 40, fw); newh = w.height() - 25 - (options.notitle ? 0 : 30); newh = math.min(newh, fh); if (!newh) return; var lt = calposition(neww); c.css({ left: lt[0], top: lt[1] }); $(this).css({ height: newh, width: neww }); } catch (e) { } }); $(_this).find(".window-header").width($(_this).width()); } } else if (options.target) { $(options.target).clone(true).show().appendto(con.empty()); } } /* * 居中显示 */ function setwincenter(){ var width = $(_this).width(), lt = calposition(width); $(_this).css({ left: lt[0], top: lt[1] }); if(options.top>0){ $(_this).css({ top: options.top }); } if(options.left>0){ $(_this).css({ left: options.left }); } if(b!=null){ var $h = $("body").height(), $wh = $win.height(),$hh=$("html").height(); $h = math.max($h, $wh); b.height($h).width($win.width()+$win.scrollleft()) ; } } /* * 计算window的位置 */ function calposition(w){ l = $win.scrollleft()+(($win.width() - w) / 2); if(l<0)l=0; t = $win.scrolltop() + $win.height() /9; if(parseint($(_this).css("top"))>t){ t=parseint($(_this).css("top")); } return [l, t]; } /* * 拖拽函数drag */ function drag(){ $(_this).find('.window-header-move').mousedown(function(e){ var dx, dy, moveout; c=$(this).parent().parent(); var t =$(this).parent(); t.bind("selectstart", function(){ return false; }); dx = e.clientx - parseint(c.css("left")); dy = e.clienty - parseint(c.css("top")); c.mousemove(move).mouseout(out).css('opacity', 0.8); t.mouseup(up); function move(e){ moveout = false; if (e.clientx - dx < 0) { l = 0; } else if (e.clientx - dx > $win.width() - c.width()) { l = $win.width() - c.width(); } else { l = e.clientx - dx } c.css({ left: l, top: e.clienty - dy }); } function out(e){ moveout = true; settimeout(function(){ moveout && up(e); }, 10); } function up(e){ c.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1); t.unbind("mouseup", up); } }); } }); }; $.fn.window.defaults={ notitle: false, opacity:0.7, title:"菜单窗口", shadow: false, target:null, requesttype:null,//iframe,ajax,img winurl:"img/", iframewh: { width: 400, height: 300 }, top:0, left:0, move:true, cleardata:false, closed:true }; })(jquery);