jQuery center div in window even if scroll is active
How can I get a div exactly in the center of my viewable area, even if I'm on the bottom scroll position, per example 2500px, my browser has a viewable area of 800px, so how to make the div pop up in the midle of the 800 and not on the midle of the 2500px?
I'm trying with this, but so far no luck
Thanks
if(parent == "modais") {
var winH = $(window).height();
var winW = $(window).width();
this.css("top", ( winH - this.outerHeight() ) / 2 - $(window).scrollTop(开发者_运维知识库) + "px");
this.css("left", ( winW - this.outerWidth() ) / 2 - $(window).scrollLeft() + "px");
/*
this.css('top', (winH/4) - (this.outerHeight()/4));
this.css('left', (winW/2) - (this.outerWidth()/2));
*/
}
Set the div's position
style to fixed
, and remove the subtractions from your calculations that use the current scrollLeft
and scrollTop
.
Be prepared to recalculate the position if the user resizes the window!
精彩评论