Javascript 将弹出窗口设置为中心jquery
现在,我用以下方式设置模态jQuery窗口的位置:Javascript 将弹出窗口设置为中心jquery,javascript,jquery-ui,jquery,Javascript,Jquery Ui,Jquery,现在,我用以下方式设置模态jQuery窗口的位置: var winH = $(window).height(); var winW = $(window).width(); //Set the popup window to center $(id).css('top', winH/2-$(id).height()/2); $(id).css('left', winW/2-$(id).width()/2); 当我向下滚动时,如何使弹出窗口居中?您可以使用另
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);
当我向下滚动时,如何使弹出窗口居中?您可以使用另一种CSS样式,如果您使用的是jQuery UI对话框,则可以尝试
位置:fixed
,默认情况下,该对话框在视口中居中。如果使用不同的jQuery模式窗口,则:
$(id).css({
'position': 'fixed',
'top': parseInt((winH / 2) - ($(id).height() / 2), 10)
'left': parseInt((winW / 2) - ($(id).width() / 2), 10)
});
也许能奏效。我不确定你所说的“向下滚动”作为一个模式对话框和滚动(在对话框之外)是互斥的。你可以这样使用它 //将模式框放置在页面中央
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
return this;
}
//然后调用函数
jQuery(".modal-profile").center();
这将使您的代码有条理,并易于在模态中心的任何项目中使用。你可以用另一个问题来看待这类工作
这个对我有帮助
$.fn.center = function() {
this.css("position", "fixed");
this.css("top", ($(window).height()/2 - this.height()/2) + "px");
this.css("left", ($(window).width()/2 - this.width()/2) + "px");
return this;
}
并按说明使用
$(“.modal window”).center()