居中特殊的Javascript弹出窗口,如何

Centering Special Javascript Popup, How?

本文关键字:窗口 如何 Javascript      更新时间:2023-11-26

我有这个javascript代码lib.js。它管理我网站上的每个java弹出窗口。尽管它并没有被设置为显示在页面的绝对中心,但我已经为它的div尝试了几个css代码。但它不起作用。我也尝试过著名的/2代码,但它不起作用,或者我可能把它添加错了地方。

下面是我的lib.js:

var currentTime=新日期();

var topP=0;

$(文档,窗口).按键(函数(e){

if (e.keyCode == 27){
    $(".yekbox").fadeOut();
    $("#yekbox_overlay").hide();
}

});

$(document).ready(function){

$(".showme").unbind().hover(
    function(e) {
        $(".tooltip").remove();
        this.t = $(this).next(".description").html();
        $(this).append( '<div class="tooltip">' + this.t + '</div>' );
    },
    function() {
        //this.title = this.t;
        $(".tooltip").remove();
    }
).mousemove(
    function(e) {
        $(".tooltip").css({
            "top" : e.pageY + 20,
            "left" : e.pageX + 20
        });
    }
);
topP = $(this).scrollTop();
//$(".yekbox").css("top", $(window).height()-250 + "px");
//$(".yekbox").css("left", $(window).width()-(440*2) + "px");

$("#yekbox_overlay").css("height", $(window).height());
$(window, document).resize(function(){
    topP = $(this).scrollTop();
    $(".yekbox").css("marginTop", topP-250 + "px");
    if ($(window).width() > 900 ) $(".yekbox").css("left", $(window).width()-(440*2) + "px");
    $(".yekbox").css("marginLeft", "auto");
    $(".yekbox").css("marginRight", "auto");
    $("#yekbox_overlay").css("height", $(window).height());

});
$(window).scroll(function () {
    topP = $(this).scrollTop();
    $(".yekbox:visible").css("marginTop", topP-250 + "px");
    $("#yekbox_overlay:visible").css("height", $(window).height());
});
$(window).bind("scroll",function () {
    topP = $(this).scrollTop();
    $(".yekbox:visible").css("marginTop", topP-250 + "px");
    $("#yekbox_overlay:visible").css("height", $(window).height());
});
$("#yekbox_overlay").click(function(){
    $(".yekbox").fadeOut();
    $(this).hide();
    return false;
});

上面的JS和HTML代码没有任何帮助。

一般来说,如果你想让一个块以父块为中心,它应该有

  • 固定宽度
  • margin: auto

如果你想要一个块绝对居中(位置绝对或固定)

  • $('#mypopup').css('left', ($(window).width() - $('#mypopup').width()) / 2)