【问题标题】:How come the content isn't centered vertically unless the browser window size is manually changed?除非手动更改浏览器窗口大小,否则内容为什么不垂直居中?
【发布时间】:2015-06-19 12:07:42
【问题描述】:

我的页面有问题,除非移动窗口大小,否则内容不会居中? http://www.robertfikesiv.com/work3.html

我从http://www.minimit.com/demos/fullscreen-backgrounds-with-centered-content复制了代码

jsfiddle 工作正常https://jsfiddle.net/e4ymr9gd/

HTML

<div class="fullscreen background" style="background-image:url('http://www.robertfikesiv.com/images/work/slider/keys.jpg');" data-img-width="1920" data-img-height="1080">
    <div class="content-a">
        <div class="content-b">
            <h1>KEYS TO SUCCESS</h1>
            <h3>Digial Design Intern</h3>
            <a href="#"><div id="c">LEARN MORE</div></a>
        </div>
    </div>
</div>

CSS

.background {
background-repeat:no-repeat;
/* custom background-position */
background-position:50% 50%;
/* ie8- graceful degradation */
background-position:50% 50%\9 !important;
}

/* fullscreen setup */
html, body {
/* give this to all tags from html to .fullscreen */
height:100%;
}
.fullscreen,
.content-a {
width:100%;
min-height:100%;
}
.not-fullscreen,
.not-fullscreen .content-a,
.fullscreen.not-overflow,
.fullscreen.not-overflow .content-a {
height:100%;
overflow:hidden;
}

/* content centering styles */
.content-a {
display:table;
}
.content-b {
display:table-cell;
position:relative;
vertical-align:middle;
text-align:center;
}

jQuery

/* fix vertical when not overflow
call fullscreenFix() if .fullscreen content changes */
function fullscreenFix(){
    var h = $('body').height();
    // set .fullscreen height
    $(".content-b").each(function(i){
        if($(this).innerHeight() <= h){
            $(this).closest(".fullscreen").addClass("not-overflow");
        }
    });
}
$(window).resize(fullscreenFix);
fullscreenFix();

/* resize background images */
function backgroundResize(){
    var windowH = $(window).height();
    $(".background").each(function(i){
        var path = $(this);
        // variables
        var contW = path.width();
        var contH = path.height();
        var imgW = path.attr("data-img-width");
        var imgH = path.attr("data-img-height");
        var ratio = imgW / imgH;
        // overflowing difference
        var diff = parseFloat(path.attr("data-diff"));
        diff = diff ? diff : 0;
        // remaining height to have fullscreen image only on parallax
        var remainingH = 0;
        if(path.hasClass("parallax")){
            var maxH = contH > windowH ? contH : windowH;
            remainingH = windowH - contH;
        }
        // set img values depending on cont
        imgH = contH + remainingH + diff;
        imgW = imgH * ratio;
        // fix when too large
        if(contW > imgW){
            imgW = contW;
            imgH = imgW / ratio;
        }
        //
        path.data("resized-imgW", imgW);
        path.data("resized-imgH", imgH);
        path.css("background-size", imgW + "px " + imgH + "px");
    });
}
$(window).resize(backgroundResize);
$(window).focus(backgroundResize);
backgroundResize();

有没有办法修复这个错误?

【问题讨论】:

    标签: jquery css fullscreen centering


    【解决方案1】:

    仅供参考,除非您需要支持旧版本的 IE,否则这些现在都内置在 CSS 中。您不需要任何 JavaScript。

    居中背景并使其填充元素 (browser support):

    background-position: 50% 50%;
    background-size: cover;
    

    将单个子元素垂直和水平居中 (browser support):

    display: flex;
    justify-content: center;
    align-items: center;
    

    至少,如果您检测到浏览器不支持 CSS,最好只回退到 JS 解决方案。 (对于它的价值,我默认阻止脚本,所以你的最小示例只是一个空白的白色页面 - 因为它是在 JS 启动之前白色背景上的白色文本。如果我不知道更好,我会离开再也没看过。)

    jsbin:http://jsbin.com/jubuvizadi/1/edit?html,css,js,output

    【讨论】:

      【解决方案2】:

      您必须在加载 DOM 后进行初始调用,因此请使用 $(document).ready()

      $(window).resize(function() {
          fullscreenfix();
          backgroundResize();
      }).focus(backgroundResize);
      $(document).ready(function() {
          fullscreenfix();
          backgroundResize();
      });
      

      【讨论】:

      • 不是 this 而是后面的函数?:$(document).ready(function() { fullscreenfix(); backgroundResize(); }); //这里的函数...
      • 不管你按什么顺序做。
      猜你喜欢
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2015-06-10
      • 2013-08-25
      • 2014-04-12
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多