Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 弹出框不像';你应该是_Jquery_Html_Css - Fatal编程技术网

Jquery 弹出框不像';你应该是

Jquery 弹出框不像';你应该是,jquery,html,css,Jquery,Html,Css,我最近在我的javascript游戏项目中实现了弹出框;但是,它们有时会出现在错误的位置(主要是页面右侧),而不是像预期的那样居中 参见正确位置示例: 参见错误位置示例: 以下是被称为的弹出式函数: //buttons can be an object or string //if string (any string), will use default button function popup(title,message,buttons,time){ $('#popup p[n

我最近在我的javascript游戏项目中实现了弹出框;但是,它们有时会出现在错误的位置(主要是页面右侧),而不是像预期的那样居中

参见正确位置示例:

参见错误位置示例:

以下是被称为的弹出式函数:

//buttons can be an object or string
//if string (any string), will use default button
function popup(title,message,buttons,time){
    $('#popup p[name="title"]').text(title);
    $('#popup div[name="content"]').html(message);
    $('#popup span[name="buttons"] :not(button[name="continue"])').remove();
    $('#popup span[name="buttons"] button').hide();

    if(buttons != null){
        if(typeof buttons == 'object'){
            //snip
        }else{
            $('#popup button[name="continue"]').show();
        }
    }else{
        setTimeout(function(){
            $('#popup').hide(750);
        },(time+750));
    }

    //center
    $('#popup').position({
        my : 'center',
        at : 'center',
        of: $('body')
    });

    $('#popup').show(750);
}

你试过这个吗。定位:绝对

因为我看不到你实际的html布局设计。我建议你试试这个

$(document).ready(function () {

    $("#popup").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2,
        "z-index": 2000 
    });

});
试试这个

    $(document).ready(function () {
    //buttons can be an object or string
    //if string (any string), will use default button
    function popup(title, message, buttons, time) {
        $('#popup p[name="title"]').text(title);
        $('#popup div[name="content"]').html(message);
        $('#popup span[name="buttons"] :not(button[name="continue"])').remove();
        $('#popup span[name="buttons"] button').hide();

        if (buttons != null) {
            if (typeof buttons == 'object') {
                //snip
            } else {
                $('#popup button[name="continue"]').show();
            }
        } else {
            setTimeout(function () {
                $('#popup').hide(750);
            }, (time + 750));
        }

        //center
        $('#popup').position({
            my: 'center',
            at: 'center',
            of: $('body')
        });

        $('#popup').show(750);
    }
    $('#popup').css({ 'position': 'absolute', 'left': '0', 'right': '0', 'bottom': '0', 'top': '0', 'margin': 'auto' });
});

对于移动支持,我该怎么做呢?您也可以将其作为css添加到媒体查询下,如#popup{position:absolute;left:0;right:0;bottom:0;top:0;margin:auto;}