【发布时间】: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;
}
}
谢谢!
【问题讨论】:
-
我不这么认为:这不是关于 div 居中而是关于响应高度。
标签: javascript jquery css responsive-design