Javascript-拖拽
Css:WinMould.css
/*窗体模板*/
.win-mould { position: absolute; top: 0px; left: -10000px; zoom: 1; z-index: 900000; overflow: hidden; display: none }
.win-mould .win-mask { width: 100%; height: 100%; background: #293541; position: absolute; left: 0px; top: 0px; filter: alpha(opacity=50); opacity: .5; z-index: 1; _z-index: 999; }
.win-mould .win-content { background: #fff; position: relative; left: 0px; top: 0px; z-index: 2; }
.win-content .win-html { position: relative; border: 1px solid #fff; *zoom: 1; }
.win-content .win-html:after { content: 'clear'; clear: both; display: block; height: 0px; overflow: hidden; visibility: hidden; }
.win-content .win-html iframe { display: block; }
.win-content .win-iframe { width: 100%; height: 100%; background: #fff; filter: alpha(opacity=0); opacity: 0; top: 0px; left: 0px; z-index: 1000; position: absolute; display: none; }
.win-content .win-tit { background: #293541; height: 35px; cursor: move; overflow: hidden; box-shadow: 2px 0px 5px #888; }
.win-tit .win-close { width: 21px; height: 21px; overflow: hidden; display: block; background: #293541 url(/images/newcloes.png) no-repeat 3px 3px; float: right; margin: 6px 6px 0px 0px; _display: inline; border: 1px solid #707274; }
.win-tit .win-close:hover { background-color: #151c22; }
.tit-text { line-height: 35px; text-indent: 10px; padding: 0px; margin: 0px; color: #fff; float: left; font-family: 'Microsoft YaHei'; font-size: 17px; letter-spacing: 1px; }
.win-mask2 { width: 100%; height: 100%; position: absolute; left: 0px; top: 0px; -webkit-user-select: none; -moz-user-select: none; }
Js:WindowForm.js
(function (w, $) {
function loadCss(url) {
var s = document.createElement("link");
s.href = url;
s.rel = "stylesheet";
s.type = "text/css";
document.getElementsByTagName("head")[0].appendChild(s);
}
loadCss('/Css/WinMould.css');
w.WF = {
winform: function (obj) {
return new winform({
tit: obj.tit,
$dom: obj.$dom,
html: obj.html,
w: obj.w,
fn: function (fn) { obj.fn(fn); },
fn2: function () { obj.fn2(); },
h: obj.h
});
}
}
w.WF2 = {
winform: function (obj) {
var newobj = new winform2({
$dom: obj.$dom,
w: obj.w,
fn: function (fn) { obj.fn(fn); },
fn2: function () { obj.fn2(); }
});
newobj.load();
return newobj;
}
}
var titbox_height = 35;
///弹出窗体
var winform = function (obj) {
this._$mask = ".win-mask";//遮罩层
this._$p_box = ".win-mould";//box层
this.$z_box = obj.$dom //".shelter";//包容层
this._$p_box_mask = ".win-mask2";//内部遮罩层
this.htmltemp = obj.html;//;//包容html
this.htmlwin = '<div class="win-mask"style="width:100%;height:100%;background:#000;position:absolute;z-index:7000;top:0px;left:0px;opacity:0.45;filter:alpha(opacity=45);display:none;"></div><div class="win-mould"><div class="win-content"><div class="win-tit"><p class="tit-text" style="float:left"></p><a href="javascript:void(0)"class="win-close"></a></div><div class="win-html">{$html}<div class="win-iframe"></div></div></div><div class="clear"></div><div class="win-mask2" onselectstart="return false" style="width: 100%; height: 100%;position: absolute; left: 0px; top: 0px; -webkit-user-select:none; -moz-user-select:none;" ></div></div>';//boxhtml
this.children_w = obj.w//380;//嵌套宽度
this.children_h = obj.h;//嵌套高度
this._p_border_w = 1;//box边框
this._p_maring_w = 0;//box边距
this._p_paddint_w = 0;//box内边距
this._pheight = 0;//计录窗口高度
this._pwidth = 0;
this.fn = obj.fn;//回调函数
this.fn_close = obj.fn2;//function () { };//关闭函数
this.tit = obj.tit;
}
winform.prototype = {
//初始化
init: function () {
var ts = this;
var htmlall = ts.htmlwin.replace('{$html}', ts.htmltemp);
$("body").append(htmlall);
ts._$mask = $(ts._$mask);
ts._$p_box = $(ts._$p_box);
ts.$z_box = $(ts.$z_box);
ts._$p_box_mask = $(ts._$p_box_mask);
$(".win-close").click(function (e) {
e.preventDefault();
e.stopPropagation();
ts._$p_box.animate({
marginTop: -1000
}, 500, 'swing', function () {
ts.hide_box();//消灭box
ts.fn_close();//执行关闭函数
});
}).mousedown(function (e) {
e.stopPropagation();
});
$(".tit-text").text(ts.tit);
ts.css();
},
//自动调整
css: function () {
var ts = this;
ts._$p_box.show();//
ts._pwidth = ts.children_w + ts._p_maring_w * 2 + ts._p_border_w * 2;
ts._pheight = ts.$z_box.outerHeight(true) + ts._p_maring_w * 2 + ts._p_border_w * 2 + titbox_height;
ts._$p_box.width(ts._pwidth).height(ts._pheight);//设置高宽;
ts._$p_box_mask.width(ts._pwidth).height(ts._pheight);//设置高宽;
ts.left_top();
},
//消灭容器
hide_box: function (fn) {
var ts = this;
ts.add_fn = null;
ts._$p_box.hide();//
ts._$mask.hide();//
ts._$p_box.remove();
ts._$mask.remove();
if (typeof fn == 'function') {
fn();
}
},
//自动居中
left_top: function () {
var ts = this;
var height_1 = document.documentElement.scrollHeight || document.body.scrollHeight;//内容高度
var clheight = document.documentElement.clientHeight || document.body.clientHeight; //当前可视高度
if (height_1 < clheight) {//当内容高度小于可视高度时
height_1 = clheight;
ts._$mask.height(height_1);//设置遮罩层高度
}
else {
ts._$mask.height(height_1);
}
var _width = document.documentElement.scrollWidth || document.body.scrollWidth;
var _width2 = document.documentElement.clientWidth || document.body.clientWidth;
if (_width2 < _width) {//当可视宽度小于内容宽度时
ts._$mask.width(_width);//设置遮罩层高度
}
else {
ts._$mask.css("width", "100%");//设置遮罩层宽度
}
var _top = document.documentElement.scrollTop || document.body.scrollTop; //当前滚动条滚动高度
var h1 = ts._pheight;
var w1 = ts._pwidth;
if (h1 > clheight) {//当可视高度过低时将position:absolute
if (height_1 <= h1) ts._$mask.height(h1);
ts._$p_box.css({ "position": "absolute", "top": _top + "px", "left": _width / 2 - (w1 / 2) + "px" });
if (h1 < height_1) {
if ((h1 + _top) > height_1) {
ts._$p_box.css({ "position": "absolute", "top": height_1 - h1 + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
else {
ts._$p_box.css({ "position": "absolute", "top": 0 + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
else {
if (h1 > height_1) {//当内容高度小于弹出窗口高度
ts._$p_box.css({ "position": "absolute", "top": _top + "px", "left": _width / 2 - (w1 / 2) + "px" })
}
else {
ts._$p_box.css({ "position": "absolute", "top": (clheight / 2 - h1 / 2) + _top + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
ts.show();
},
//绘制显示
show: function () {
var ts = this;
ts._$p_box.css({ "margin-top": "-1000px" });
ts._$p_box.animate({
marginTop: 0
}, 500, 'swing');
ts._$mask.show();//
ts.fn.call(ts, function () {
ts.hide_box();//返回关闭窗口的方法
});
ts.add_fn();
},
add_fn: function () {
var movethis = this;
dra_list_win.config.dom = $(movethis._$p_box)[0];
dra_list_win.config.dom1 = $("div.win-tit")[0];
dra_list_win.config.domw = $(movethis._$p_box)[0].scrollWidth;
dra_list_win.config.domh = $(movethis._$p_box)[0].scrollHeight;
dra_list_win.init();
},
un_end: function () {
}
}
///已有窗体显示
var winform2 = function (obj) {
this._$mask1 = ".win-mask";//遮罩层
this._$mask = ".win-mask";//遮罩层
this._$p_box = ".win-mould";//box层
this.$z_box = obj.$dom //".shelter";//包容层
this._$p_box_mask = ".win-mask2";//内部遮罩层
this.htmltemp = obj.html;//;//包容html
this.htmlwin = '<div class="win-mask"style="width:100%;height:100%;background:#000;position:absolute;z-index:7000;top:0px;left:0px;opacity:0.45;filter:alpha(opacity=45);display:none;"></div>';//boxhtml
this.children_w = obj.w//380;//嵌套宽度
this.children_h = obj.h;//嵌套高度
this._p_border_w = 1;//box边框
this._p_maring_w = 0;//box边距
this._p_paddint_w = 0;//box内边距
this._pheight = 0;//计录窗口高度
this._pwidth = 0;
this.fn = obj.fn;//回调函数
this.fn_close = obj.fn2;//function () { };//关闭函数
this.tit = obj.tit;
}
winform2.prototype = {
//初始化
load: function () {
var ts = this;
$(".win-close").click(function (e) {
ts._$p_box.animate({
marginTop: -1000
}, 500, 'swing', function () {
ts.hide_box();//消灭box
ts.fn_close();//执行关闭函数
});
}).mousedown(function (e) {
e.stopPropagation();
});
},
//初始化
pop_up: function () {
var ts = this;
var htmlall = ts.htmlwin;
$("body").append(htmlall);
ts._$mask = $(ts._$mask1);
ts._$p_box = $(ts._$p_box);
ts.$z_box = $(ts.$z_box);
ts.css();
},
//自动调整
css: function () {
var ts = this;
ts._$p_box.show();
ts.children_w=ts.$z_box.outerWidth(true)>ts.children_w?ts.$z_box.outerWidth(true):ts.children_w;
ts._pwidth = ts.children_w + ts._p_maring_w * 2 + ts._p_border_w * 2;
ts._pheight = ts.$z_box.outerHeight(true) + ts._p_maring_w * 2 + ts._p_border_w * 2 + titbox_height;
ts._$p_box.width(ts._pwidth).height(ts._pheight);//设置高宽;
ts.left_top();
},
//消灭容器
hide_box: function (fn) {
var ts = this;
ts._$mask.hide();
ts._$mask.remove();
if (typeof fn == 'function') {
fn();
}
},
//自动居中
left_top: function () {
var ts = this;
var height_1 = document.documentElement.scrollHeight || document.body.scrollHeight;//内容高度
var clheight = document.documentElement.clientHeight || document.body.clientHeight; //当前可视高度
if (height_1 < clheight) {//当内容高度小于可视高度时
height_1 = clheight;
ts._$mask.height(height_1);//设置遮罩层高度
}
else {
ts._$mask.height(height_1);
}
var _width = document.documentElement.scrollWidth || document.body.scrollWidth;
var _width2 = document.documentElement.clientWidth || document.body.clientWidth;
if (_width2 < _width) {//当可视宽度小于内容宽度时
ts._$mask.width(_width);//设置遮罩层高度
}
else {
ts._$mask.css("width", "100%");//设置遮罩层宽度
}
var _top = document.documentElement.scrollTop || document.body.scrollTop; //当前滚动条滚动高度
var h1 = ts._pheight;
var w1 = ts._pwidth;
if (h1 > clheight) {//当可视高度过低时将position:absolute
if (height_1 <= h1) ts._$mask.height(h1);
ts._$p_box.css({ "position": "absolute", "top": _top + "px", "left": _width / 2 - (w1 / 2) + "px" });
if (h1 < height_1) {
if ((h1 + _top) > height_1) {
ts._$p_box.css({ "position": "absolute", "top": height_1 - h1 + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
else {
ts._$p_box.css({ "position": "absolute", "top": 0 + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
else {
if (h1 > height_1) {//当内容高度小于弹出窗口高度
ts._$p_box.css({ "position": "absolute", "top": _top + "px", "left": _width / 2 - (w1 / 2) + "px" })
}
else {
ts._$p_box.css({ "position": "absolute", "top": (clheight / 2 - h1 / 2) + _top + "px", "left": _width / 2 - (w1 / 2) + "px" });
}
}
ts.show();
},
//绘制显示
show: function () {
var ts = this;
ts._$p_box.css({ "margin-top": "-1000px" });
ts._$p_box.animate({
marginTop: 0
}, 500, 'swing');
ts._$mask.show();
ts.fn.call(ts, function () {
ts.hide_box();
});
ts.add_fn();
},
add_fn: function () {
var movethis = this;
dra_list_win.config.dom = $(movethis._$p_box)[0];
dra_list_win.config.dom1 = $("div.win-tit")[0];
dra_list_win.config.domw = $(movethis._$p_box)[0].scrollWidth;
dra_list_win.config.domh = $(movethis._$p_box)[0].scrollHeight;
dra_list_win.init();
},
unload: function () {
var ts = this;
ts._$p_box.animate({
marginTop: -1000
}, 500, 'swing', function () {
ts.hide_box();//消灭box
ts.fn_close();//执行关闭函数
});
}
}
///拖拽
var dra_list_win = {
//初始化
config: {
dom: '',// document.getElementById("drafting"),//拖拽容器
dom1: '',// document.getElementById("dramove"),//拖拽dom
domw: 0,// document.getElementById("drafting").scrollWidth,
domh: 0,// document.getElementById("drafting").scrollHeight,
startleft: 0,//起始left位置
starttop: 0,//起始top位置
endleft: 0,//结束left位置
endtop: 0,//结束top位置
x: 0,//鼠标相对窗口x坐标
y: 0,//鼠标相对窗口y坐标
clientX: 0,//起始鼠标位置
clientY: 0//起始鼠标位置
},
clearselect: window.getSelection ? function () { window.getSelection().removeAllRanges(); } : function () { document.selection.empty(); },//清空用户文体选中
//事件绑定
init: function () {
var ts = this;
ts.config.dom1.onmousedown = function (e) {
$(".win-iframe").show();
ts.config.startleft = ts.config.dom.offsetLeft;//防止其他函数改变位置
ts.config.starttop = ts.config.dom.offsetTop;//防止其他函数改变位置
ts.config.domw = ts.config.dom.scrollWidth;
ts.config.domh = ts.config.dom.scrollHeight;
e = e || window.event;
ts.config.clientX = e.clientX;
ts.config.clientY = e.clientY;
ts.config.x = ts.config.clientX - ts.config.startleft;
ts.config.y = ts.config.clientY - ts.config.starttop;
//设置捕获范围
if (ts.config.dom.setCapture) {
ts.config.dom.setCapture();
}
else {
if (window.captureEvents) {
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP)
}
}
document.body.style.MozUserSelect = "none";
document.onselectstart = function () { return false; };
document.onmousemove = function (e) {
ts.movestart(e || window.event);
}
document.onmouseup = function (e) {
$(".win-iframe").hide();
ts.moveend(e || window.event);
}
if (navigator.userAgent.indexOf('Firefox') >= 0) { return false; }
};
},
movestart: function (e) {
var ts = this;
e = e || window.event;
var x = ts.config.clientX - e.clientX;//x移动距离
var y = ts.config.clientY - e.clientY;//y移动距离
ts.config.endleft = ts.config.startleft - x;
ts.config.endtop = ts.config.starttop - y;
//document.getElementById("span").innerText = ts.config.endleft + "," + ts.config.endtop;
var height_1 = document.documentElement.scrollHeight || document.body.scrollHeight;//内容高度
var clienth = document.documentElement.clientHeight || document.body.clientHeight; //当前可视高度
if (height_1 > clienth) {//当内容高度小于可视高度时
clienth = height_1;
}
var _width = document.documentElement.scrollWidth || document.body.scrollWidth;
var clientw = document.documentElement.clientWidth || document.body.clientWidth;
if (_width > clientw) {
clientw = _width;
}
if ((ts.config.endleft + ts.config.domw) >= clientw) {
ts.config.endleft = clientw - ts.config.domw;
}
if ((ts.config.endtop + ts.config.domh) >= clienth) {
ts.config.endtop = clienth - ts.config.domh;
}
ts.config.endleft = ts.config.endleft <= 0 ? 0 : ts.config.endleft;
ts.config.endtop = ts.config.endtop <= 0 ? 0 : ts.config.endtop;
ts.config.dom.style.left = ts.config.endleft + "px";
ts.config.dom.style.top = ts.config.endtop + "px";
e.preventDefault ? e.preventDefault() : e.returnValue = false;
return false;
},
moveend: function (e) {
e = e || window.event;
//设置捕获范围
var ts = this;
if (ts.config.dom.setCapture) {
ts.config.dom.releaseCapture();
}
else {
if (window.captureEvents) {
window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP)
}
}
document.onselectstart = function () { return true; };
document.body.style.MozUserSelect = "";
ts.config.startleft = ts.config.endleft;//记录结束left位置
ts.config.starttop = ts.config.endtop;//记录结束top位置
document.onmousemove = null;
document.onmouseup = null;
return false;
}
}
})(window, $);
///系统窗体提示
使用:
调用1:
new WF.winform({
tit: '标题',
$dom: '.box',
html:‘html字符串’,
w: 880,
fn: function (fn) { },//进入后执行的回调事件
fn2: function () { },//结束回调事件
h: 480
}).init();
调用2:页面添加html模板
<div class="win-mould" style="display:none;">
<div class="win-content">
<div class="win-tit">
<p class="tit-text" id="win-tit-text" style="float:left">添加公司账号</p><a href="javascript:void(0)" class="win-close"></a>
</div>
<div class="win-html">
<!--内容-->
<div class="win-iframe"></div>
</div>
</div>
</div>
var winformbox = WF2.winform({
$dom: '.box',
fn: function (fn) {},//进入后执行的事件
fn2: function () {},//结束时执行的事件
w: 1173
});
//调用1
winformbox.pop_up();//显示窗体
//调用2
winformbox.unload();//关闭窗体//清除窗体
浙公网安备 33010602011771号