function Dialog(options) {
    var defaults = {
        // 默认值。
        title: '',       // 标题文本,若不想显示title请通过CSS设置其display为none
        type: 'text',    // id,img,iframe,url,text
        content: '',     // 要显示的内容
        showTitle: true,     // 是否显示标题栏。
        closeText: '关闭',  // 关闭按钮文字,若不想显示关闭按钮请通过CSS设置其display为none
        closeClass: '',
        buttons: null,     //按钮,
        topOffset: 0,
        padding: 0,
        //width: 250,
        //height: 100,
        position: "center",  // 弹出框位置  'center', 'left', 'right', 'top', 'bottom',
        draggable: false,    // 是否移动
        modal: true,         // 是否是模态对话框
        fixed: false,         // 是否跟随页面滚动。
        time: 0,             // 自动关闭时间,为0表示不会自动关闭。
        id: false,            // 对话框的id,若为false,则由系统自动产生一个唯一id。
        transparent: false
    };
    options = $.extend(defaults, options);
    options.id = options.id ? options.id : 'dialog-' + Dialog.__count; // 唯一ID
    var overlayId = options.id + '-overlay'; // 遮罩层ID
    var timeId = null; // 自动关闭计时器
    var isShow = false;
    var isIe = /msie/.test(navigator.userAgent.toLowerCase());
    var isIe6 = 'undefined' == typeof (document.body.style.maxHeight);
    var self = this;

    /* 对话框的布局及标题内容。*/

    var wrapper = $("<div class=\"wrapper\" style=\"position:relative; overflow:hidden; text-align:left;\">");
    if (options.width) {
        wrapper.width(options.width);
    }
    if (options.height) {
        wrapper.height(options.height);
    }
    if (options.background) {
        wrapper.css("background", options.background);
    }

    var dialog = $('<div id="' + options.id + '" class="dialog"></div>').hide();
    if (options.transparent) {
        dialog.addClass("transparent");
        options.showTitle = false;
    }

    var bar = null;
    if (options.showTitle == false) {
        bar = $('<a href="javascript:;" title="Close" class="close ' + options.closeClass + '"></a>');
        bar.css('z-index', ++Dialog.__zindex);
        dialog.append(bar);
    } else {
        bar = $('<div class="bar"><span class="title">' + options.title + '</span><a class="close ' + options.closeClass + '">' + options.closeText + '</a></div>');
        if (options.draggable) {
            bar.css({ cursor: "move" });
        }
        wrapper.append(bar);
    }

    wrapper.append('<div class="content"></div>');
    if (options.padding > 0) {
        $(".content", wrapper).css("padding", options.padding + "px");
    }

    var buttons = $('<div class="buttons"></div>');

    if (options.buttons != null) {
        $(options.buttons).each(function () {
            var item = this;
            var button = $('<input type="button" class="w-btn">');
            button.val(item.name);
            if (item.handler) {
                button.click(function () {
                    item.handler(item.name, self);
                });
            }
            buttons.append(button);
        });
        wrapper.append(buttons);
    }

    dialog.append(wrapper);
    $('body').append(dialog);

    /**
    * 重置对话框的位置。
    *
    * 主要是在需要居中的时候,每次加载完内容,都要重新定位
    *
    * @return void
    */
    var resetPos = function () {
        if (typeof options.position == 'string') {
            switch (options.position.toLowerCase()) {
                case 'center':
                    var left = ($(window).width() - dialog.width()) / 2;
                    var top = ($(window).height() - dialog.height()) / 2;
                    if (!isIe6 && options.fixed) {
                        dialog.css({ top: top + options.topOffset, left: left });
                    } else {
                        dialog.css({ top: top + $(document).scrollTop() + options.topOffset, left: left + $(document).scrollLeft() });
                    }
                    break;
            }
        } else if (typeof options.position == 'object') {
            dialog.css({ left: options.position[0], top: options.position[1] });
        }
    };

    /**
    * 初始化位置及一些事件函数。
    *
    * 其中的this表示Dialog对象而不是init函数。
    */
    var init = function () {
        /* 是否需要初始化背景遮罩层 */
        if (options.modal) {
            $('body').append('<div id="' + overlayId + '" class="dialog-overlay"></div>');
            $('#' + overlayId).css({
                'left': 0, 'top': 0,
                /*'width':$(document).width(),*/
                'width': '100%',
                /*'height':'100%',*/
                'height': $(document).height(),
                'z-index': ++Dialog.__zindex,
                'position': 'fixed'
            }).hide();
        }

        dialog.css({ 'z-index': ++Dialog.__zindex, 'position': options.fixed ? 'fixed' : 'absolute' });

        /*  IE6 兼容fixed代码 */
        if (isIe6 && options.fixed) {
            dialog.css('position', 'absolute');
            resetPos();
            var top = parseInt(dialog.css('top')) - $(document).scrollTop();
            var left = parseInt(dialog.css('left')) - $(document).scrollLeft();
            $(window).scroll(function () {
                dialog.css({ 'top': $(document).scrollTop() + top, 'left': $(document).scrollLeft() + left });
            });
        }

        /* 以下代码处理框体是否可以移动 */
        var mouse = { x: 0, y: 0 };
        function moveDialog(event) {
            var e = window.event || event;
            var top = parseInt(dialog.css('top')) + (e.clientY - mouse.y);
            var left = parseInt(dialog.css('left')) + (e.clientX - mouse.x);
            dialog.css({ top: top, left: left });
            mouse.x = e.clientX;
            mouse.y = e.clientY;
        };

        dialog.find('.bar').mousedown(function (event) {
            if (!options.draggable) { return; }

            var e = window.event || event;
            mouse.x = e.clientX;
            mouse.y = e.clientY;
            $(document).bind('mousemove', moveDialog);
        });

        $(document).mouseup(function () {
            $(document).unbind('mousemove', moveDialog);
        });

        /* 绑定一些相关事件。 */
        dialog.find('.close').bind('click', this.close);
        dialog.bind('mousedown', function () { dialog.css('z-index', ++Dialog.__zindex); });

        // 自动关闭
        if (0 != options.time) { timeId = setTimeout(this.close, options.time); }
    };

    this.getDialogObj = function () {
        return dialog;
    };

    /**
    * 设置对话框的内容。
    *
    * @param string c 可以是HTML文本。
    * @return void
    */
    this.setContent = function (c) {
        var div = dialog.find('.content');
        switch (c.type.toLowerCase()) {
            case 'id': // 将ID的内容复制过来,原来的还在。
                div.html($('#' + c.content).html());
                if (c.onLoad) {
                    c.onLoad.apply(self);
                }
                break;
            case 'img':
                div.html('<div class="loading"><img src="/images/loading.gif">加载中...</div>');
                $('<img alt="" />').load(function () { div.empty().append($(this)); resetPos(); })
                    .attr('src', c.content);
                if (c.onLoad) {
                    c.onLoad.apply(self);
                }
                break;
            case 'url':
                div.html('<div class="loading"><img src="/images/loading.gif">加载中...</div>');
                $.ajax({
                    url: c.content,
                    success: function (html) {
                        div.html(html);
                        resetPos();
                        if (c.onLoad) {
                            c.onLoad.apply(self);
                        }
                    },
                    error: function () {
                        div.html('出错啦');
                        if (c.onLoad) {
                            c.onLoad.apply(self);
                        }
                    }
                });
                break;
            case 'iframe':
                var iframe = $('<iframe frameborder="0" src="' + c.content + '" style="border:0px;display: block;" />');
                iframe.load(function () {
                    iframe.width(options.width);
                    iframe.height(options.height - bar.outerHeight());
                    resetPos();
                    if (c.onLoad) {
                        c.onLoad.apply(self);
                    }
                });
                div.append(iframe);
                break;
            case 'text':
                div.html(c.content);
                if (c.onLoad) {
                    c.onLoad.apply(self);
                }
                break;
            default:
                div.html(c);
                if (c.onLoad) {
                    c.onLoad.apply(self);
                }
                break;
        }
    };   /**
    * 显示对话框
    */
    this.show = function () {
        /* 是否显示背景遮罩层 */
        if (options.modal) {
            $('#' + overlayId).show();
        }

        dialog.fadeIn(500, function () {
            if (options.onShow) {
                options.onShow.apply(self);
            }
            isShow = true;
        });
        // 自动关闭
        if (0 != options.time) {
            timeId = setTimeout(this.close, options.time);
        }

        resetPos();
    };

    /**
    * 隐藏对话框。但并不取消窗口内容。
    */
    this.hide = function () {
        if (!isShow) { return; }

        dialog.fadeOut('slow', function () {
            if (options.onHide) {
                options.onHide.apply(self);
            }
        });

        if (options.modal) {
            $('#' + overlayId).fadeOut('slow');
        }

        isShow = false;
    };

    /**
    * 关闭对话框
    *
    * @return void
    */
    this.close = function () {
        dialog.fadeOut('slow', function () {
            if (options.onClose) {
                options.onClose.apply(self);
            }
            $(this).remove();
            isShow = false;
        });
        if (options.modal) {
            $('#' + overlayId).fadeOut('slow', function () {
                $(this).remove();
            });
        }
        clearTimeout(timeId);
    };

    init.call(this);
    this.setContent(options);

    Dialog.__count++;
    Dialog.__zindex++;
}
Dialog.__zindex = 900;
Dialog.__count = 1;
Dialog.version = '1.0';

jquery Dialog弹框插件的更多相关文章

  1. jquery Dialog弹框插件使用

    var dialog = new Dialog({ title: '购物车', type: 'url', width: 520, content: "Uplolo.aspx", s ...

  2. 解决 jquery dialog 弹框destroy销毁方法不能把弹出元素设置成初始状态

    在使用jquery ui中的dialog弹出窗口的时候遇到一个问题,就是页面弹出窗口关闭后希望表单元素能回到初始状态 例如文本框输入内容后关闭dialog后里面的内容清除,使用了destroy方法也不 ...

  3. jQuery Dialog弹出层对话框插件

    Dialog.js的相关注释已经添加,可以按照注释,进行相关样式的修改,适用于自定义的各个系统! dialog.js /** * jQuery的Dialog插件. * * @param object ...

  4. 自己写的基于bootstrap风格的弹框插件

    自己写的一款基于bootstrap风格的弹框插件,暂时只有确认框.提示框.后续功能扩展.bug修改再更新. ;(function($){ //默认参数 var PARAMS; var DEFAULTP ...

  5. 非常强大的jQuery万能浮动框插件

    支持hover, click, focus以及无事件触发:支持多达12种位置的定位,出界自动调整:支持页面元素加载,Ajax加载,下拉列表,提示层效果,tip类效果等:可自定义装载容器:内置UI不错的 ...

  6. jQuery实现搜索框插件+豆瓣音乐接口实现豆瓣搜索框

    jQuery实现搜索框插件 豆瓣音乐接口实现豆瓣搜索框 豆瓣接口有时不稳定,网络请求会报400,不要惊慌.我主要是练习一下jQuery的JSONP和封装插件. <div class=" ...

  7. 基于jQuery消息提示框插件Tipso

    今天要分享的这款jQuery消息提示框插件名叫Tipso,它的特点是可以定义提示框的显示位置,以及动态改变提示框的提示内容,应该说是一款相当灵活的jQuery消息提示框插件.效果图如下: 在线预览   ...

  8. vue项目中使用vue-layer弹框插件

    vue-layer弹框插件  安装 npm i --save vue-layer 引用 import layer from 'vue-layer' Vue.prototype.$layer = lay ...

  9. jQuery下拉框插件8种效果

    jQuery自定义漂亮的下拉框插件8种效果 jquery美化选择器实例有:边框.下划线. 伸缩 .滑动. 覆盖. 旋转. 弹出层选择 .环形效果. 在线预览 <body class=" ...

随机推荐

  1. BZOj 4540: [Hnoi2016]序列 [莫队 st表 预处理]

    4540: [Hnoi2016]序列 题意:询问区间所有子串的最小值的和 不强制在线当然上莫队啦 但是没想出来,因为不知道该维护当前区间的什么信息,维护前后缀最小值的话不好做 想到单调栈求一下,但是对 ...

  2. c语言中的register int

    register int a=1; 明确声明必须要把变量存放在寄存器中,直到变量消失. 一般是默认register,大多数的情况下是不用写register

  3. 【Tools】ubuntu无法virtualenv创建python虚拟环境的解决

    刚有人问我Ubuntu python虚拟环境无法创建问题,报错same file error,防止今后遇到忘记,记录下可能的问题. 1.先在windows上试了下: pip install virtu ...

  4. SDP(8):文本式数据库-MongoDB-Scala基本操作

    MongoDB是一种文本式数据库.与传统的关系式数据库最大不同是MongoDB没有标准的格式要求,即没有schema,合适高效处理当今由互联网+商业产生的多元多态数据.MongoDB也是一种分布式数据 ...

  5. 让网站通过Https访问

    Prerequisites Before you begin, you should have some configuration already taken care of. We will be ...

  6. Windows API Finishing

    input { font-size: 14px; height: 26px } td { border-style: none; border-color: inherit; border-width ...

  7. GB 标准

    std::map<int, std::string> GB2261 = { { 0,"未知的性别" }, { 1,"男性" }, { 2," ...

  8. uva208

    一道简单的路径打印,首先需要一次dfs判断能否从1到达目标点,否则可能会超时.还有一点就是那个格式需要注意下,每条路径前没有空格(看起来好像有3个空格)-. AC代码: #include<cst ...

  9. POJ - 2492 种类并查集

    思路:保存每个点与其父节点的关系,注意合并和路径压缩即可. AC代码 #include <cstdio> #include <cmath> #include <cctyp ...

  10. 下载visual studio 的离线包

    我们知道,visual studio是微软发布的软件开发的集成平台,十分好用,可以说是目前最好用的软件编辑工具. 最新版的是visual studio 2017,但是在微软的官网上只支持下载在线安装器 ...