原本计划实现这样一个需求: 前台点击触发某业务动作,需要用户补充信息,不做跳转页面,弹窗的形式进行补充信息。 折腾出来了,但是最终没有用到。
代码还有些毛躁,提供大概实现逻辑。
实现思路:在窗口铺上蒙板以屏蔽原窗口功能按钮操作,在蒙板上层绝对定位实现弹窗,弹窗中的数据交互采用ajax方式。 出发弹窗事件用onclick.
关键细节:弹窗和原窗体本质是同页面,为了描述方便,姑且称底层窗体为父窗体,弹窗为子窗体。为了实现字父窗体的交互,需要在父窗体中做一些特别标签,以便选择器选择,并操作插入新的dom对象。
如此,首先看下父窗体的代码,关键部分我是有注释的。
测试弹窗 *{ margin: 0; padding: 0; text-align: center; text-decoration: none; } body{ font: 12px/1.5 宋体,Tahoma, Arial,sans-serif; font-family: "微软雅黑"; width:320px; height: auto; margin:0 auto; } .content{ border: #ccc solid 1px; margin:60px 10px 10px; background:#fff; overflow:hidden; color:#6b6b6b; font-size:14px; border-radius:5px; } 对话测试
接着给出选择器部分代码,也就是outil.js的代码,当然之前的jquery以及jquery ui就不说了。 其核心是绑定click事件。
jQuery.extend({
getcookie : function(sName) {
var acookie = document.cookie.split("; ");
for (var i=0; i < acookie.length; i++){
var aCrumb = acookie[i].split("=");
if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
}
return '';
},
setcookie : function(sName, sValue, sExpires) {
var scookie = sName + "=" + encodeURIComponent(sValue);
if (sExpires != null) scookie += "; expires=" + sExpires;
document.cookie = scookie;
},
removecookie : function(sName) {
document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
});
$(function(){
$('*[ectype="dialog"]').click(function(){
var id = $(this).attr('dialog_id');
var title = $(this).attr('dialog_title') ? $(this).attr('dialog_title') : '';
var url = $(this).attr('uri');
var width = $(this).attr('dialog_width');
ajax_form(id, title, url, width);
return false;
});
});
function drop_confirm(msg, url){
if(confirm(msg)){
window.location = url;
}
}
function ajax_form(id, title, url, width)
{
if (!width)
{
width = 400;
}
var d = DialogManager.create(id);
d.setTitle(title);
d.setContents('ajax', url);
d.setWidth(width);
d.show('center');
return d;
}
function go(url){
window.location = url;
}
function set_zindex(parents, index){
$.each(parents,function(i,n){
if($(n).css('position') == 'relative'){//alert('relative');
//alert($(n).css('z-index'));
$(n).css('z-index',index);
}
});
}
function js_success(dialog_id)
{
DialogManager.close(dialog_id);
var url = window.location.href;
url = url.indexOf('#') > 0 ? url.replace(/#/g, '') : url;
window.location.replace(url);
}
function js_fail(str)
{
$('#warning').html('');
$('#warning').show();
}
function check_pint(v)
{
var regu = /^[0-9]{1,}$/;
if(!regu.test(v))
{
alert(lang.only_int);
return false;
}
return true;
}
function transform_char(str)
{
if(str.indexOf('&'))
{
str = str.replace(/&/g, "%26");
}
return str;
}
// 复制到剪贴板
function copyToClipboard(txt) {
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
} else if(navigator.userAgent.indexOf("Opera") != -1) {
window.location = txt;
} else if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
return false;
}
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip)
return false;
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans)
return false;
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext = txt;
str.data = copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid = Components.interfaces.nsIClipboard;
if (!clip)
return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
绑定事件的相关代码就是dialog的核心代码(dialog.js)了,这个是在网上找到的,在此感谢,代码如下所示:
__DIALOG_WRAPPER__ = {};
DialogManager = {
'create' :function(id){
var d = {};
if (!__DIALOG_WRAPPER__[id])
{
d = new Dialog(id);
__DIALOG_WRAPPER__[id] = d;
}
else
{
d = DialogManager.get(id);
}
return d;
},
'get' :function(id){
return __DIALOG_WRAPPER__[id];
},
'close' :function(id){
if (__DIALOG_WRAPPER__[id].close())
{
__DIALOG_WRAPPER__[id] = null;
}
},
'onClose' :function (){
return true;
},
'loadStyle' :function(){
var _dialog_js_path = $('#dialog_js').attr('src');
var _path = _dialog_js_path.split('/');
var _dialog_css = _path.slice(0, _path.length - 1).join('/') + '/dialog.css';
$('#dialog_js').after('');
}
};
ScreenLocker = {
'style' : {
'position' : 'absolute',
'top' : '0px',
'left' : '0px',
'backgroundColor' : '#000',
'opacity' : 0.5,
'zIndex' : 999
},
'masker' : null,
'lock' : function(zIndex){
if (this.masker !== null)
{
this.masker.width($(document).width()).height($(document).height());
return true;
}
this.masker = $('');
if ($.browser.msie)
{
$('select').css('visibility', 'hidden');
}
//var _iframe = $('').css({'opacity':0, 'width':'100%', 'height':'100%'});
//this.masker.append(_iframe);
this.masker.css(this.style);
if (zIndex)
{
this.masker.css('zIndex', zIndex);
}
this.masker.width($(document).width()).height($(document).height());
$(document.body).append(this.masker);
},
'unlock' : function(){
if (this.masker === null)
{
return true;
}
this.masker.remove();
this.masker = null;
if ($.browser.msie)
{
$('select').css('visibility', 'visible');
}
}
};
Dialog = function (id){
this.id = id;
this.init();
};
Dialog.prototype = {
'id' : null,
'dom' : null,
'lastPos' : null,
'status' : 'complete',
'onClose' : function (){
return true;
},
'tmp' : {},
'init' : function(){
this.dom = {'wrapper' : null, 'body':null, 'head':null, 'title':null, 'close_button':null, 'content':null};
this.dom.wrapper = $('').get(0);
this.dom.body = $('').get(0);
this.dom.head = $('').get(0);
this.dom.title = $('').get(0);
//this.dom.close_button = $('close').get(0);
this.dom.content = $('').get(0);
$(this.dom.head).append('').append($('').append(this.dom.title)).append(this.dom.close_button);
$(this.dom.body).append(this.dom.head).append(this.dom.content);
$(this.dom.wrapper).append(this.dom.body).append('');
$(this.dom.wrapper).css({
'zIndex' : 9999,
'display' : 'none',
'position' : 'absolute'
});
$(this.dom.body).css({
'position' : 'relative'
});
$(this.dom.head).css({
'cursor' : 'move'
});
$(this.dom.close_button).css({
'position' : 'absolute',
'text-indent': '-9999px',
'cursor' : 'pointer',
'overflow' : 'hidden'
});
$(this.dom.content).css({
'margin' : '0px',
'padding' : '0px'
});
var self = this;
$(this.dom.close_button).click(function(){
DialogManager.close(self.id);
});
$(this.dom.wrapper).draggable({
'handle' : this.dom.head
});
$(document.body).append(this.dom.wrapper);
},
'hide' : function(){
$(this.dom.wrapper).hide();
},
'show' : function(pos){
if (pos)
{
this.setPosition(pos);
}
ScreenLocker.lock(999);
$(this.dom.wrapper).show();
},
'close' : function(){
if (!this.onClose())
{
return false;
}
$(this.dom.wrapper).remove();
ScreenLocker.unlock();
return true;
},
'setTitle' : function(title){
$(this.dom.title).html(title);
},
'setContents' : function(type, options){
contents = this.createContents(type, options);
if (typeof(contents) == 'string')
{
$(this.dom.content).html(contents);
}
else
{
$(this.dom.content).empty();
$(this.dom.content).append(contents);
}
},
'setStyle' : function(style){
if (typeof(style) == 'object')
{
$(this.dom.wrapper).css(style);
}
else
{
$(this.dom.wrapper).addClass(style);
}
},
'setWidth' : function(width){
this.setStyle({'width' : width + 'px'});
},
'setHeight' : function(height){
this.setStyle({'height' : height + 'px'});
},
'createContents' : function(type, options){
var _html = '',
self = this,
status= 'complete';
if (!options)
{
this.setStatus(status);
return type;
}
switch(type){
case 'ajax':
$.get(options, {ajax:1}, function(data){
if(data.substr(0,1) == '{' && data.substr(data.length - 1, 1) == '}'){
var JSON = eval('(' + data + ')');
if(!JSON.done){
self.setContents(JSON.msg);
return;
}
}
self.setContents(data);
self.setPosition(self.lastPos);
//>>addByZZY20160909: 根据后台返回信息决定该窗口是否展示
if(data.substr(0,5) == 'close'){
self.close();
}
});
_html = this.createContents('loading', {'text' : 'loading...'});
break;
case 'loading':
_html = '' + options.text + '';
status = 'loading';
break;
case 'message':
var type = 'notice';
if (options.type)
{
type = options.type;
}
_message_body = $('');
_message_contents = $('' + options.text + '');
_buttons_bar = $('');
switch (type){
case 'notice':
case 'warning':
var button_name = lang./confirm/i;
if (options.button_name)
{
button_name = options.button_name;
}
_ok_button = $('');
$(_ok_button).click(function(){
if (options.onclick)
{
if(!options.onclick.call())
{
return;
}
}
DialogManager.close(self.id);
});
$(_buttons_bar).append(_ok_button);
break;
case '/confirm/i':
var yes_button_name = lang.yes;
var no_button_name = lang.no;
if (options.yes_button_name)
{
yes_button_name = options.yes_button_name;
}
if (options.no_button_name)
{
no_button_name = options.no_button_name;
}
_yes_button = $('');
_no_button = $('');
$(_yes_button).click(function(){
if (options.onClickYes)
{
if (options.onClickYes.call() === false)
{
return;
}
}
DialogManager.close(self.id);
});
$(_no_button).click(function(){
if (options.onClickNo)
{
if (!options.onClickNo.call())
{
return;
}
}
DialogManager.close(self.id);
});
$(_buttons_bar).append(_yes_button).append(_no_button);
break;
}
_html = $(_message_body).append(_message_contents).append(_buttons_bar);
break;
}
this.setStatus(status);
return _html;
},
'setPosition' : function(pos){
this.lastPos = pos;
if (typeof(pos) == 'string')
{
switch(pos){
case 'center':
var left = 0;
var top = 0;
var dialog_width = $(this.dom.wrapper).width();
var dialog_height = $(this.dom.wrapper).height();
left = $(window).scrollLeft() + ($(window).width() - dialog_width) / 2;
top = $(window).scrollTop() + ($(window).height() - dialog_height) / 2;
$(this.dom.wrapper).css({left:left + 'px', top:top + 'px'});
break;
}
}
else
{
var _pos = {};
if (typeof(pos.left) != 'undefined')
{
_pos.left = pos.left;
}
if (typeof(pos.top) != 'undefined')
{
_pos.top = pos.top;
}
$(this.dom.wrapper).css(_pos);
}
},
'setStatus' : function(code){
this.status = code;
},
'getStatus' : function(){
return this.status;
},
'disableClose' : function(msg){
this.tmp['oldOnClose'] = this.onClose;
this.onClose = function(){
if(msg)alert(msg);
return false;
};
},
'enableClose' : function(){
this.onClose = this.tmp['oldOnClose'];
this.tmp['oldOnClose'] = null;
}
};
//RemoveByZZY20160909: 手动引入样式文件
//DialogManager.loadStyle();
好了,以上就是核心逻辑及代码实现,代码很好的解释了整个过程,没必要浪费文字了。这里面把子窗体我再贴下。
.btn{ margin:10px 5px; width: 100px; }
最后再贴一张效果图吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



