我有一个问题。我想将浏览器视口中垂直居中的div对齐。我明白如何做到这一点,并自己写了一些代码。但有一两件事,不工作:垂直居中一页div
// fetch al info I need
var windowHeight = $(window).height();
var pageTop = $(window).scrollTop();
var modalHeight = "98";
// Get the centre of the window
var divTop = (windowHeight - modalHeight)/2;
// Add the scrollTop so the div will align in the middle of my current browser viewport
var divTop = divTop + pageTop;
var divTop = divTop + "px";
$('#modal_placeholder').css('top',divTop);
现在的问题是,它不会得到正确的scrollTop的价值......它总是说,这是0,像你现在在页面的顶部。
你能帮我吗?
Offcourse!这是问题...它保存了页面加载的scrollTop值(当时为0)。我现在已经将它绑定到按钮上并单击它,它就可以工作!谢啦! – Xeon 2010-12-03 17:25:42