2013-09-29 29 views
0

我有一个弹出在我的页面中间弹出一个js弹出。问题是,当用户向下滚动到页面底部并点击按钮触发弹出时,弹出窗口将出现在顶部而不是屏幕中间(因为用户已经向下滚动,所以他/她不在页面的中间)。使JavaScript弹出中间的屏幕,而不是页面中间

我想要的是,无论用户在哪里查看,弹出窗口都会位于用户的屏幕中间。

function centerPopup(){ 
var windowWidth = document.documentElement.clientWidth; 
var windowHeight = document.documentElement.clientHeight; 
var popupHeight = $(".popupContent").height(); 
var popupWidth = $(".popupContent").width(); 
$(".popupContent").css({ 
    "position": "absolute", 
    "top": windowHeight/2-popupHeight/2, 
    "left": windowWidth/2-popupWidth/2 
}); 
//this is needed for ie6 
$(".backgroundPopup").css({ "height": windowHeight }); 
} 

或者是有可能有一个弹出触发弹出下面的按钮?

+0

只是禁用/启用scrollig,当弹出窗口显示 – Sarath

+1

使用'位置:fixed'代替。 – JJJ

+0

我明白了,谢谢兄弟。放大时使用触摸屏设备的用户会感到讨厌吗?你有一些jQuery的示例代码,直接弹出按钮下方? –

回答

1

我没有测试它,但有可能它会工作

function centerPopup(){ 

var windowWidth = $(window).width(); 
var windowHeight = $(window).height(); 
var popupHeight = $(".popupContent").outerHeight(); 
var popupWidth = $(".popupContent").outerWidth(); 
var windowScrollTop = $(window).scrollTop(); 
var windowScrollLeft = $(window).scrollLeft(); 

$(".popupContent").css({ 
    "position": "absolute", 
    "top": Math.max(0,((windowHeight -popupHeight) /2)+windowScrollTop) + "px", 
    "left": Math.max(0,((windowWidth -popupWidth) /2)+windowScrollLeft) + "px" 
}); 

} 
相关问题