Javascript //将margin-top设置为站点高度的负半部分

时间:2013-10-18 08:28:40

标签: javascript jquery html css wordpress

我有一个网站,我希望每个网站的内容都是死点,无论屏幕高度如何。

我想要实现的是这样的:

#content {position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */}

我不知道如何编码:

function fullscreenHeight(){      
      var window_height = $(window).height();
      var negative_margin = window_height / 2 * -1;
      $('.pageclass01').css({height:window_height});
      $('.pageclass01').css({margin-top:negative_margin});
}         
fullscreenHeight();           
$(window).bind('resize',function() {      
    fullscreenHeight();
});  

任何能够帮助我的人吗? :)

4 个答案:

答案 0 :(得分:1)

使用此CSS:

#content {
    position:fixed;
    top:0; bottom:0;
    height:240px;
    margin:auto;
}
/* the following is just in case of a too-small screen, such as a small phone */
@media screen and (max-height:240px) {
    #content {position:static}
}

答案 1 :(得分:0)

$('.pageclass01').css({height:window_height,marginTop: "-"+($(this).height()/2)});// this here being the pageclass01 element and not window

这是您可能需要做的事情

答案 2 :(得分:0)

试试这个,

function fullscreenHeight(){
  var window_height = $(window).height();
  var negative_margin = -(window_height / 2);
  $('.pageclass01').css({'height':window_height,'marginTop':negative_margin+'px'});
}   

答案 3 :(得分:0)

if ($(".page-container").outerHeight()< $(window).outerHeight()){
    $(".page-container").css({
        'top' : ($(window).outerHeight()-$(".page-container").outerHeight())/2
    })
} else {
    $(".page-container").css({
        'top' : 0
    })
}