简体   繁体   English

如何获得拆分层的内容容器最大宽度以匹配全宽层的宽度?

[英]How do I get the content container max-width of a split tier to match the width of a full width tier?

I am attempting to build out a 60/40 split tier on a web page whose container size is calculated based on the maximum container width of the full sized tier above it. 我正在尝试在网页上建立一个60/40拆分层,该页面的容器大小是根据其上方完整大小层的最大容器宽度来计算的。 I was able to pull the max-width value of the container class of the full-width-tier and then use some Javascript to manipulate that number to compute two values (one for the left side of the split tier and one for the right) and then use jQuery to apply the new calculated max-width values to their respective sides of the tier. 我能够提取全宽度层的容器类的最大宽度值,然后使用一些Javascript操纵该数字以计算两个值(一个用于拆分层的左侧,一个用于右侧)然后使用jQuery将新计算出的最大宽度值应用于其相应的层。 The value on the left is 60% of the container max-width and the value on the left is 40% of the container max-width. 左侧的值是容器最大宽度的60%,左侧的值是容器最大宽度的40%。

Here is my code: 这是我的代码:

HTML/JS/jQuery HTML / JS / jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="tier-one" class="full-width-tier">
   <div class="main-text container">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sapien metus, tristique et massa   at, finibus fringilla mi. Vestibulum in semper eros. Nullam in ligula eros. Vivamus arcu risus, convallis sit amet diam eget, consequat hendrerit elit. Donec ac metus id arcu rhoncus sodales. Donec in justo sed mauris iaculis auctor sit amet ut neque. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec accumsan interdum elit, vitae euismod odio mollis vitae. Suspendisse arcu mi, rhoncus non tortor et, mattis efficitur ligula. Sed hendrerit nunc et condimentum fermentum. Nunc vel est non mauris sagittis dignissim. Duis placerat vehicula enim, a tincidunt dui rhoncus sit amet. Aenean sed semper leo. Suspendisse placerat dictum arcu vehicula dapibus. Vivamus et sem venenatis, placerat dolor ut, imperdiet dui. Vivamus sed tortor a massa fringilla tristique. Quisque vulputate ac risus eget tincidunt. Nullam a viverra ex.</p>
   </div>
</div>

<div id="tier-two" class="flex-tier">
    <div class="flex-col-1">
    <div class="main-text container">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras nec pulvinar turpis. Donec nec enim ultricies, ultrices libero ac, tincidunt nibh. In varius neque eu orci consequat, id vestibulum ex congue. Aliquam nulla sem, fermentum eget efficitur nec, gravida at risus. Phasellus molestie sit amet mauris interdum dapibus. Duis vehicula nulla sit amet dignissim tincidunt. Fusce pulvinar, magna sollicitudin sollicitudin pharetra, augue urna varius ex, a ullamcorper elit ante vehicula urna. Pellentesque aliquet posuere neque sed sodales. Nulla auctor magna eget nisi pulvinar sagittis. In a massa at tortor faucibus accumsan at eget nisl. Aliquam accumsan convallis ex et viverra. Pellentesque tempor turpis mi, sit amet tincidunt eros mattis et. Nullam sed ex non risus pulvinar euismod ut quis turpis. Proin ut vestibulum nunc.<p>
    </div>  
  </div>

<!-- Left tier container max-width calculator  -->
<script>
var flex_elem = document.querySelector('.container');
var flex_container_styles = getComputedStyle(flex_elem);
var flex_container_width = flex_container_styles.maxWidth;
var container_width_integer = parseInt(flex_container_width, 10);

var final_flex_60_width = container_width_integer * 0.6;
var final_flex_60_width_px = final_flex_60_width.toString() + "px";
</script>

<script>
function addSplitMaxWidth(media_query) {
  if (media_query.matches)
    $("#tier-two .flex-col-1 .container").css("cssText", "max-width: " + final_flex_60_width_px + " !important;");
}
var media_query = window.matchMedia("(min-width: 768px)")
addSplitMaxWidth(media_query) // Call listener function at run time
media_query.addListener(addSplitMaxWidth) // Attach listener function on state changes
</script>
<!-- End caculator -->

  <div class="flex-col-2">
    <div class="main-text container">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras nec pulvinar turpis. Donec nec enim ultricies, ultrices libero ac, tincidunt nibh. In varius neque eu orci consequat, id vestibulum ex congue. Aliquam nulla sem, fermentum eget efficitur nec, gravida at risus. Phasellus molestie sit amet mauris interdum dapibus. Duis vehicula nulla sit amet dignissim tincidunt. Fusce pulvinar, magna sollicitudin sollicitudin pharetra, augue urna varius ex, a ullamcorper elit ante vehicula urna. Pellentesque aliquet posuere neque sed sodales. Nulla auctor magna eget nisi pulvinar sagittis. In a massa at tortor faucibus accumsan at eget nisl. Aliquam accumsan convallis ex et viverra. Pellentesque tempor turpis mi, sit amet tincidunt eros mattis et. Nullam sed ex non risus pulvinar euismod ut quis turpis. Proin ut vestibulum nunc.</p>
    </div>
  </div>

<!-- Right container max-width calculator  -->
<script>
var flex_elem = document.querySelector('.container');
var flex_container_styles = getComputedStyle(flex_elem);
var flex_container_width = flex_container_styles.maxWidth;
var container_width_integer = parseInt(flex_container_width, 10);

var final_flex_40_width = container_width_integer * 0.4;
var final_flex_40_width_px = final_flex_40_width.toString() + "px";
</script>

<script>
function addSplitMaxWidth(media_query) {
  if (media_query.matches)
    $("#tier-two .flex-col-2 .container").css("cssText", "max-width: " + final_flex_40_width_px + " !important;");
}
var media_query = window.matchMedia("(min-width: 768px)")
addSplitMaxWidth(media_query) // Call listener function at run time
media_query.addListener(addSplitMaxWidth) // Attach listener function on state changes
</script>
<!-- End caculator -->

</div>

CSS 的CSS

body {
    box-sizing: border-box;
}

.full-width-tier {
    position: relative;
    width: 100%;
}

.container {
  max-width: 1170px;
  margin: 0 auto;
}

.main-text {
  padding: 20px;
}

.flex-tier {
    position: relative;
    width: 100%;
        display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
}
.flex-tier [class*="flex-col-"] {
    position: relative;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    overflow: hidden;
}
.flex-tier .main-text {
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -moz-box-align: center;
    -ms-flex-align: center;
    justify-content: center;
    position: relative;
    z-index: 2;
    height: 100%;
}

@media (max-width: 768px) {
  .flex-tier {
        display: block !important;
    }
  .flex-tier [class*="flex-col-"] {
      max-width: 100% !important;
      width: 100% !important;
    }
}

.flex-col-1 .main-text {
  float: right;
}

.flex-col-2 .main-text {
  float: left;
}

#tier-two .flex-col-1 {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 60%;
    flex: 0 0 60%;
    max-width: 60%;
}

#tier-two .flex-col-2 {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 40%;
    flex: 0 0 40%;
    max-width: 40%;
}

Apologies for placing the Javascript and Jquery inside the HTML but the order in which these scripts load in relation to the HTML is very important. 很抱歉将Javascript和Jquery放在HTML内,但是这些脚本相对于HTML的加载顺序非常重要。

Now, while my calculated values appear to be correct, when viewing the page at larger screen sizes, the containers of the two tiers don't appear to line up. 现在,虽然我的计算值似乎是正确的,但是以较大的屏幕尺寸查看页面时,两层的容器似乎没有对齐。 Here is a screenshot: 这是屏幕截图:

alignment-error-screenshot 对齐错误屏幕截图

This is my problem. 这是我的问题。 No matter what I adjust, nothing appears to fix this problem. 无论我进行了什么调整,都无法解决此问题。 I could set the widths manually or change the percentage of the container max-width value I use on each side to line up perfectly but when I do that, the solution only works for the max-width currently in use (in this case 1170px). 我可以手动设置宽度,也可以更改每边使用的容器最大宽度值的百分比以完美对齐,但是当我这样做时,解决方案仅适用于当前使用的最大宽度(在这种情况下为1170px) 。 If the max-width were increased to say 1500px, the manually set values don't line up anymore. 如果最大宽度增加到1500像素,则手动设置的值不再对齐。 This needs to work regardless of the initially set max-width value for the container class. 无论容器类最初设置的最大宽度值是多少,这都需要工作。

Here is a jsfiddle with my code put into place. 这是一个放置了我的代码的jsfiddle Here is a preview of the page as well. 这也是页面预览

Sorry this is a lot but hopefully I've made my dilemma clear. 抱歉,这很多,但希望我已经阐明了我的困境。 Thanks in advance for any and all help! 在此先感谢您提供的所有帮助!

Kind of a long question so I apologize if my answer isn't exactly what you're looking for, but CSS grid is a great way to make 2 columns with one always taking up 40% of the space and the other 60%. 有点长的问题,所以我很抱歉,如果我的答案不完全是您要寻找的东西,但是CSS网格是一种制作2列的好方法,其中一列始终占用40%的空间,而另一列始终占据60%的空间。 Something like this would work: 这样的事情会起作用:

.container {
display: grid;
grid-template-columns: 40% 60%; /*you could also just do 40% auto */
grid-template-rows: 100%;
}

.smallColumn {
grid-column: 1 / 2;
}

.largeColumn {
grid-column: 2 / 3;
}

If you aren't super familiar with how CSS grid works, I'd highly recommend learning it since it's so useful/powerful. 如果您对CSS网格的工作方式不太熟悉,我强烈建议您学习它,因为它是如此有用/功能强大。 Here's a good resource. 这是一个很好的资源。

Let me know if you need further clarification or have other problems. 让我知道您是否需要进一步说明或有其他问题。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM