JQuery警报不是垂直居中的

时间:2015-11-21 13:13:01

标签: jquery plugins alerts

我正在使用旧的jquery插件来替换警报消息。我遇到的问题是警报是弹出屏幕顶部而不是浮动到中心。我试图修改CSS以包含

margin-left: auto; 
margin-right: auto;

无济于事。

我想修改这个jquery函数,所以它会自动垂直和水平居中,不会影响页面的高度或宽度。这是一个JSFiddle,给出了一个问题的例子。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我给了

高度:200像素;

到#popup_container

并在jquery函数make

verticalOffset:0,

请试试并说出结果。

修改

这是[我的小提琴] [1]

我刚刚编辑了 verticalOffset:0

在jquery中。多数民众赞成。

    [1]: http://jsfiddle.net/oneNiceFriend/ms8kwdj7/1/

答案 1 :(得分:0)

我能够通过将定位从绝对更改为固定来解决此问题。这是一个带有更改Working example on JSFiddle

的jSFiddle
// jQuery Alert Dialogs Plugin
//
// Version 1.1
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 14 May 2009
//
// Website: http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/
//
// Usage:
//      jAlert( message, [title, callback] )
//      jConfirm( message, [title, callback] )
//      jPrompt( message, [value, title, callback] )
// 
// History:
//
//      1.00 - Released (29 December 2008)
//
//      1.01 - Fixed bug where unbinding would destroy all resize events
//
// License:
// 
// This plugin is dual-licensed under the GNU General Public License and the MIT License and
// is copyright 2008 A Beautiful Site, LLC.
//
(function($) {

$.alerts = {

    // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time

    verticalOffset: -175,                // vertical offset of the dialog from center screen, in pixels
    horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
    repositionOnResize: true,           // re-centers the dialog on window resize
    overlayOpacity: .01,                // transparency level of overlay
    overlayColor: '#FFF',               // base color of overlay
    draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
    okButton: ' OK ',         // text for the OK button
    cancelButton: ' Cancel ', // text for the Cancel button
    dialogClass: null,                  // if specified, this class will be applied to all dialogs

    // Public methods

    alert: function(message, title, fieldFocus) {
        if( title == null ) title = 'Alert';
        $.alerts._show(title, message, null, 'alert', fieldFocus);
    },

    confirm: function(message, title) {
        if( title == null ) title = 'Confirm';
        $.alerts._show(title, message, null, 'confirm');
    },

    prompt: function(message, value, title) {
        if( title == null ) title = 'Prompt';
        $.alerts._show(title, message, value, 'prompt');
    },

    // Private methods

    _show: function(title, msg, value, type, field) {
        fieldFocus = '[name="' + field + '"]'; 
        $.alerts._hide();
        $.alerts._overlay('show');

        $("BODY").append(
          '<div id="popup_container">' +
            '<h1 id="popup_title"></h1>' +
            '<div id="popup_content">' +
              '<div id="popup_message"></div>' +
            '</div>' +
          '</div>');

        if( $.alerts.dialogClass ) $("#popup_container").addClass($.alerts.dialogClass);

        // IE6 Fix
        var pos = 'fixed';
        //var pos = 'absolute'; 
        var rel = 'relative'; 

        $("#popup_container").css({
            position: pos,
            zIndex: 99999,
            padding: 0,
            margin: 0
        });

        $("#popup_title").text(title);
        $("#popup_content").addClass(type);
        $("#popup_message").text(msg);
        $("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );

        $("#popup_container").css({
            minWidth: $("#popup_container").outerWidth(),
            maxWidth: $("#popup_container").outerWidth()
        });

        $.alerts._reposition();
        $.alerts._maintainPosition(true);

        switch( type ) {
            case 'alert':
                $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /></div>');
                $("#popup_container").click( function() {
                    $.alerts._hide();
                    $(fieldFocus).focus();
                });
                $("#popup_ok").click( function() {
                    $.alerts._hide();
                    $(fieldFocus).focus();
                });
                $("#popup_ok").focus().keypress( function(e) {
                    if( e.keyCode == 13 || e.keyCode == 27 ) $("#popup_ok").trigger('click');
                });
            break;
            case 'confirm':
                $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                $("#popup_ok").click( function() {
                    $.alerts._hide();
                });
                $("#popup_cancel").click( function() {
                    $.alerts._hide();
                });
                $("#popup_ok").focus();
                $("#popup_ok, #popup_cancel").keypress( function(e) {
                    if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
                    if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
                });
            break;
            case 'prompt':
                $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                $("#popup_prompt").width( $("#popup_message").width() );
                $("#popup_ok").click( function() {
                    var val = $("#popup_prompt").val();
                    $.alerts._hide();
                });
                $("#popup_cancel").click( function() {
                    $.alerts._hide();
                });
                $("#popup_prompt, #popup_ok, #popup_cancel").keypress( function(e) {
                    if( e.keyCode == 13 ) $("#popup_ok").trigger('click');
                    if( e.keyCode == 27 ) $("#popup_cancel").trigger('click');
                });
                if( value ) $("#popup_prompt").val(value);
                $("#popup_prompt").focus().select();
            break;
        }

        // Make draggable
        if( $.alerts.draggable ) {
            try {
                $("#popup_container").draggable({ handle: $("#popup_title") });
                $("#popup_title").css({ cursor: 'move' });
            } catch(e) { /* requires jQuery UI draggables */ }
        }
    },

    _hide: function() {
        $("#popup_container").remove();
        $.alerts._overlay('hide');
        $.alerts._maintainPosition(false);
    },

    _overlay: function(status) {
        switch( status ) {
            case 'show':
                $.alerts._overlay('hide');
                $("BODY").append('<div id="popup_overlay"></div>');
                $("#popup_overlay").css({
                    position: 'fixed',
                    zIndex: 99998,
                    top: '0px',
                    left: '0px',
                    width: '100%',
                    height: $(document).height(),
                    background: $.alerts.overlayColor,
                    opacity: $.alerts.overlayOpacity
                });
            break;
            case 'hide':
                $("#popup_overlay").remove();
            break;
        }
    },

    _reposition: function() {
        var top = (($(window).height() / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
        var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
        if( top < 0 ) top = 0;
        if( left < 0 ) left = 0;


        $("#popup_container").css({
            top: top + 'px',
            left: left + 'px'
        });
        $("#popup_overlay").height( $(document).height() );
    },

    _maintainPosition: function(status) {
        if( $.alerts.repositionOnResize ) {
            switch(status) {
                case true:
                    $(window).bind('resize', $.alerts._reposition);
                break;
                case false:
                    $(window).unbind('resize', $.alerts._reposition);
                break;
            }
        }
    }

}

// Shortuct functions
jAlert = function(message, title, fieldFocus) {
    $.alerts.alert(message, title, fieldFocus);
}

jConfirm = function(message, title) {
    $.alerts.confirm(message, title);
};

jPrompt = function(message, value, title) {
    $.alerts.prompt(message, value, title);
};

})(jQuery);

$("#test").on('click', function() {
jAlert("This is a test", 'Error', 'test');
});