/**
* jquery wbox plugin
* wbox鏄竴涓交閲忕骇鐨勫脊鍑虹獥鍙query鎻掍欢锛屽熀浜巎query1.4.2寮€鍙戯紝
* 涓昏瀹炵幇寮瑰嚭妗嗙殑鏁堟灉锛屽苟涓斿姞鍏ヤ簡寰堝鏈夎叮鐨勫姛鑳斤紝姣斿鍙痠mg鐏鏁堟灉锛宑allback鍑芥暟锛屾樉绀洪殣钘忓眰锛孉jax椤甸潰锛宨frame宓屽叆椤甸潰绛夊姛鑳?
* @name wbox
* @url http://www.linzl.com
*/
(function($){
//class涓?wbox_close涓哄叧闂?
$.fn.wbox = function(options){
var defaults = {
wboxurl: "wbox/",
opacity: 0.5,//鑳屾櫙閫忔槑搴?
callback: null,
notitle: false,
show:false,
timeout:0,
target:null,
requesttype:null,//iframe,ajax,img
title: "wbox title",
drag:true,
iframewh: {//iframe 璁剧疆楂樺
width: 400,
height: 300
},
html: ''//wbox鍐呭
},_this=this;
this.yq = $.extend(defaults, options);
var wboxhtml = '
', b = null, c = null, $win = $(window),$t=$(this);//b鑳屾櫙锛孋鍐呭jquery div
this.showbox=function (){
$("#wbox_overlay").remove();
$("#wbox").remove();
b = $("").hide().addclass('wbox_overlaybg').css('opacity', _this.yq.opacity).dblclick(function(){
_this.close();
}).appendto('body').fadein(300);
c = $(wboxhtml).appendto('body');
handleclick();
}
/*
* 澶勭悊鐐瑰嚮
* @param {string} what
*/
function handleclick(){
var con = c.find("#wboxcontent");
if (_this.yq.requesttype && $.inarray(_this.yq.requesttype, ['iframe', 'ajax','img'])!=-1) {
con.html("");
if (_this.yq.requesttype === "img") {
var img = $("");
img.attr("src",_this.yq.target);
img.load(function(){
img.appendto(con.empty());
setposition();
});
}
else
if (_this.yq.requesttype === "ajax") {
$.get(_this.yq.target, function(data){
con.html(data);
c.find('.wbox_close').click(_this.close);
setposition();
})
}
else {
ifr = $("");
ifr.appendto(con.empty());
ifr.load(function(){
try {
$it = $(this).contents();
$it.find('.wbox_close').click(_this.close);
fh = $it.height();//iframe height
fw = $it.width();
w = $win;
neww = math.min(w.width() - 40, fw);
newh = w.height() - 25 - (_this.yq.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) {
}
});
}
}
else
if (_this.yq.target) {
$(_this.yq.target).clone(true).show().appendto(con.empty());
}
else
if (_this.yq.html) {
con.html(_this.yq.html);
}
else {
$t.clone(true).show().appendto(con.empty());
}
afterhandleclick();
}
/*
* 澶勭悊鐐瑰嚮涔嬪悗鐨勫鐞?
*/
function afterhandleclick(){
setposition();
c.show().find('.wbox_close').click(_this.close).hover(function(){
$(this).addclass("on");
}, function(){
$(this).removeclass("on");
});
$(document).unbind('keydown.wbox').bind('keydown.wbox', function(e){
if (e.keycode === 27)
_this.close();
return true
});
typeof _this.yq.callback === 'function' ? _this.yq.callback() : null;
!_this.yq.notitle&&_this.yq.drag?drag():null;
if(_this.yq.timeout){
settimeout(_this.close,_this.yq.timeout);
}
}
/*
* 璁剧疆wbox鐨勪綅缃?
*/
function setposition(){
if (!c) {
return false;
}
var width = c.width(), lt = calposition(width);
c.css({
left: lt[0],
top: lt[1]
});
var $h = $("body").height(), $wh = $win.height(),$hh=$("html").height();
$h = math.max($h, $wh);
b.height($h).width($win.width())
}
/*
* 璁$畻wbox鐨勪綅缃?
* @param {number} w 瀹藉害
*/
function calposition(w){
l = ($win.width() - w) / 2;
t = $win.scrolltop() + $win.height() /9;
return [l, t];
}
/*
* 鎷栨嫿鍑芥暟drag
*/
function drag(){
var dx, dy, moveout;
var t = c.find('.wbox_dragtitle').css('cursor', 'move');
t.bind("selectstart", function(){
return false;
});
t.mousedown(function(e){
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);
});
/*
* 绉诲姩鏀瑰彉鐢熸椿
* @param {object} e 浜嬩欢
*/
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
});
}
/*
* 浣犲凡缁弌ut鍟︼紒
* @param {object} e 浜嬩欢
*/
function out(e){
moveout = true;
settimeout(function(){
moveout && up(e);
}, 10);
}
/*
* 鏀惧純
* @param {object} e浜嬩欢
*/
function up(e){
c.unbind("mousemove", move).unbind("mouseout", out).css('opacity', 1);
t.unbind("mouseup", up);
}
}
/*
* 鍏抽棴寮瑰嚭妗嗗氨鏄щ闄よ繕鍘?
*/
this.close=function (){
if (c) {
b.remove();
c.stop().fadeout(300, function(){
c.remove();
})
}
}
/*
* 瑙﹀彂click浜嬩欢
*/
$win.resize(function(){
setposition();
});
_this.yq.show?_this.showbox():$t.click(function(){
_this.showbox();
return false;
});
return this;
};
})(jquery);