JQuery插件第十五个:弹出框

<div style="border:1px red solid; width:800px; height:800px; background-color:Blue; " >
    <a href="javascript:void(0);" class="hit" rel="sub/Huiyuanzhongxinfabu/Chuangjianboke.html" >
        打开弹出框
    </a>
</div>
<script type="text/javascript" >
var popup={
	overlay:null,
	//此处放在弹出窗体页面<body onload="window.parent.popup.dyniframesize();" >,关闭按钮类设为 pClose
	dyniframesize:function (){
			var $ifr=$("#pIfra"),height=0,width=0,$contents=null;
			if($ifr.length>0){
				//自动调节iframe高度
				$contents=$ifr.contents();$ifr.show();
				//设置关闭弹出层
				$(".pClose",$contents).click(function(){
					window.parent.popup._close();
				}).css({cursor:'pointer'});
				
				if($("body>*",$contents).height()!=null){
					height=$("body>*",$contents).height();
					$ifr.height(height);
				}
				if($("body>*",$contents).width()!=null)
				{      
					width=$("body>*",$contents).width();    
					$ifr.css({"width":width
					,"position":"absolute"
					,"z-index":2
					,"left":($(document).width() - width)/2
					,"top":(document.documentElement.clientHeight-height)/2 + $(document).scrollTop()
					});
					this._loadOverlay();this.overlay.show();drag();
				}
			}
            //拖动弹出层窗体
            function drag() {
                var dx, dy, moveout;
                $contents.mousedown(function (e) {
                    dx = e.clientX - parseInt($ifr.css("left"));
                    dy = e.clientY - parseInt($ifr.css("top"));
                    $contents.mousemove(move).mouseout(out).mouseup(up);
                });
                move = function (e) {
                    moveout = false;
                    win = $(window);
                    var x, y;
                    if (e.clientX - dx < 0) {
                        x = 0;
                    } else {
                        if (e.clientX - dx > (win.width() - $contents.width())) {
                            x = win.width() - $contents.width();
                        } else {
                            x = e.clientX - dx;
                        }
                    }
                    if (e.clientY - dy < 0) {
                        y = 0;
                    } else {
                        y = e.clientY - dy;
                    }
                    $ifr.css({
                        left: x,
                        top: y
                    });
                }
                out = function (e) {
                    moveout = true;
                    setTimeout(function () {
                        moveout && up(e);
                    }, 10);
                }
                up = function (e) {
                    $contents.unbind("mousemove", move).unbind("mouseout", out).unbind("mouseup", up);
                }
            }			
	},
	_loadOverlay:function(){                //加载遮罩
		pageWidth=($.browser.version=="6.0")?$(document).width()-21:$(document).width();
		this.overlay?this.overlay.remove():null;
		this.overlay=$(document.createElement("div"));
		this.overlay.css({position:"absolute","z-index":1,left:0,top:0,display:"none",width:pageWidth,height:$(document).height(),"min-height":"100%","background-color":"#000",filter:"alpha(opacity=60)","-moz-opacity": 0.6,opacity: 0.6}).appendTo("body");
	},
	_close:function(){                      //关闭方法
		this.overlay.hide();$("#pIfra").hide();
	},
    showDialog:function (obj){
            $(obj).after("<iframe id='pIfra' frameborder='no' scrolling='no' style='display:none' ></iframe>");
            var href=$(obj).attr("rel");
            $(obj).next().attr("src",href+"?t="+new Date().getTime());
    }
}

$(function(){
        $(".hit").click(function(){
           popup.showDialog(this);
        });
});
</script>
点击按钮跳出弹出框,根据框内页面自动调节弹出框的宽度和高度
 

你可能感兴趣的:(JQuery插件第十五个:弹出框)