【问题标题】:responsive height of div: shrink to a breakpoint then start growing againdiv的响应高度:缩小到断点然后再次开始增长
【发布时间】:2017-02-17 09:49:44
【问题描述】:

我正在使用英雄部分来显示一些内容。 它使用padding-bottom 百分比技术和内部绝对定位容器来使内容居中,从而做出响应。

现在要注意了:到达一个断点,比如说 768px,在较小的窗口大小上,我希望盒子再次开始增长。

我在网上找到了一些 js/jQuery 代码,并且能够得到结果,但只有当我在窗口为 时加载页面时它才有效。在这种情况下,它工作得很好。但是如果页面在更大的窗口中加载,768px 以下的大小调整就会丢失。

这是html:

<div class="row row-home-hero" id="hero">
    <div class="cont">
        <h1>Title</h1>
        <h2>Subtitle</h2>
        <div class="cta-hero-home">
            <a href="#" class="cta-hero-link">» CTA1</a>
            <span class="cta-hero-spacer">or</span>
            <a href="#" class="cta-hero-link">» CTA2</a>
        </div>
    </div>
</div>

这是JS。 这是一团糟,因为它是来自不同来源的混合。 我正在使用 Wordpress,所以我必须用 jQuery 替换一些 $。 请原谅我:)

function screenClass() {
    if(jQuery(window).innerWidth() < 768) {
        jQuery('.row-home-hero').addClass('small-hero');
    } else {
        jQuery('.row-home-hero').removeClass('small-hero');
        jQuery('.row-home-hero').css("height", "");
    }
}

// Fire.
screenClass();

// And recheck if window gets resized.
jQuery(window).bind('resize',function(){
    screenClass();
});
if (document.documentElement.clientWidth < 768) {
    var $li = jQuery('.small-hero'),         // Cache your element
    startW = $li.width();  // Store a variable reference
    function setMarginDiff() {
    area = 500000;
    width = jQuery('.small-hero').width();
    jQuery('.small-hero').height(Math.ceil(area/width/1.7));
    }
    setMarginDiff();                 // Do on DOM ready
    jQuery(window).resize(setMarginDiff); // and on resize
}

这就是 CSS

.row-home-hero {
    background-position: center;
    -webkit-background-size: cover;
    background-size: cover;
    background-repeat: no-repeat;
    position: relative;
    background-color: red;

}
.row-home-hero:before {
    display: block;
    content: "";
    width: 100%;
    padding-top: 46%;
}
.row-home-hero .cont {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40%;
    text-align: center;
}
a.cta-hero-link {
    display: block;
    width: 100px;
    max-width: 80%;
    line-height: 40px;
    background: white;
    color: #1b9fdd;
    text-transform: uppercase;
    text-decoration: none;
    margin: 10px auto;
    text-align: center;
    font-weight: 500;
    box-shadow: 0px 0px 7px 1px rgba(0,0,0,.4);
}

@media screen and (max-width: 768px) {
    .row-pre-footer .cont div {
        width: 100%;
        padding: 0 5%;
        float: none;
        margin: 0 auto 30px;

    }
    .progetto-footer, .loghi-footer {
        width: 100%;
        max-width: 320px;
        margin: 0 auto 30px;
        float: none;
    }
    .double-box .tib-tab {
        float: none;
        width: 90%;
        margin: 5% auto;
        padding-bottom: 90%;
    }
    .tib-box h2, .tab-box h2 {
        font-size: calc(28px + (46 - 28) * (100vw - 320px) / (768 - 320));
        margin-bottom: 18vw;
    }
    .double-box-inner p {
        font-size: 22px;
        line-height: 30px;
    }
    .row-home-hero.small-hero {
        height: 500px;
    }
    .row-home-hero:before {
        display: block;
        content: "";
        width: 100%;
        padding-top: 0;
    }
}

And this is a working demo

谢谢!

【问题讨论】:

标签: javascript jquery css responsive-design


【解决方案1】:

我在调整大小事件中移动了if (document.documentElement.clientWidth &lt; 768) { 块。以便在调整窗口大小时调用它。

在原始版本中,它只会在页面加载时调用(并且仅当屏幕小于 768 时)。通过此调整,调整大小时将始终重新检查。

我还将您的所有代码合并到一个较小的函数中。

var breakpoint = 768
var area = 500000
var target = $('.row-home-hero')

$(window).bind('resize', function() {
  if(window.innerWidth < breakpoint) {
        var width = target.width()
        target.addClass('small-hero')
        target.height(Math.ceil(area / width / 1.7))

    } else {
        target.removeClass('small-hero')
        target.css('height', '')
    }
})
.trigger('resize')

【讨论】:

  • 哇,真快。而且很容易。它工作得很好,这里是demo
  • @molokom 完美 :) 很高兴为您提供帮助
  • 嘿,@arcs,你知道如何让它在移动设备上运行吗?我需要在任何调整大小完成之前运行 javascript,因为没有调整大小,窗口是固定的,所以直到用户滚动有一个固定的高度。滚动后,javascript 会覆盖它并分配正确的动态高度。
  • @molokom 是的,我对代码进行了更新。现在也更紧凑了
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 2020-08-28
  • 2013-08-13
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2016-09-15
相关资源
最近更新 更多