简体   繁体   English

如何让两个 div 屏幕显示和隐藏在相同的高度

[英]How to make two div screens show and hide at the same exact height

I have two screens I'd like to show.我有两个想要展示的屏幕。 I'd like them to be exactly the same height but the first screen must be the same height as the second and the second screen is sized dynamically.我希望它们的高度完全相同,但第一个屏幕的高度必须与第二个屏幕的高度相同,并且第二个屏幕的大小是动态的。

Is there a way to do this correctly so I can switch between them and both have the same height using CSS or do I have to use JavaScript?有没有办法正确地做到这一点,所以我可以使用 CSS 在它们之间切换并且两者都具有相同的高度,还是必须使用 JavaScript?

I have it sort of working with the example JavaScript below but there's some odd space above the buttons.我可以使用下面的示例 JavaScript,但按钮上方有一些奇怪的空间。

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); basicView.style.height = getComputedStyle(advancedView).height; advancedView.style.display = "none"; basicButton.addEventListener("click", function() { advancedView.style.display = "none"; basicView.style.display = "block"; }) advancedButton.addEventListener("click", function() { advancedView.style.display = "block"; basicView.style.display = "none"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>

Here is an idea with CSS grid where you make both element inside the same track so they have the same size and then you adjust visibility这是 CSS 网格的一个想法,您可以在同一轨道内制作两个元素,以便它们具有相同的大小,然后调整visibility

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); advancedView.style.visibility = "hidden"; basicButton.addEventListener("click", function() { advancedView.style.visibility = "hidden"; basicView.style.visibility = "visible"; }) advancedButton.addEventListener("click", function() { advancedView.style.visibility = "visible"; basicView.style.visibility = "hidden"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #container { display:grid; } #container > *{ grid-row:1; grid-column:1; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>

Another idea using flexbox where you consider negative margin to allow the overlap and rely on the stretch behavior to have the same height:另一个使用 flexbox 的想法,您考虑负边距以允许重叠并依靠拉伸行为具有相同的高度:

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); advancedView.style.visibility = "hidden"; basicButton.addEventListener("click", function() { advancedView.style.visibility = "hidden"; basicView.style.visibility = "visible"; }) advancedButton.addEventListener("click", function() { advancedView.style.visibility = "visible"; basicView.style.visibility = "hidden"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #container { display:flex; } #container > *{ flex-grow:1; flex-basis:0; } #container >:last-child { margin-left:-100%; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>

Answer:回答:

Your main problem is that your paragraphs have margins that are pushing out over the boundary of the parent div.您的主要问题是您的段落的边距超出了父 div 的边界。

This is causing the buttons to be pushed down.这导致按钮被按下。

There are a few things you can do.您可以做几件事。


Add an Overflow Scroll property:添加溢出滚动属性:

#advanced, #basic {
  overflow-y: scroll;
}

This tells the advanced and basic tabs to not exceed the limits they're assigned in the DOM, and to scroll if they do!这告诉advancedbasic选项卡不要超过它们在 DOM 中分配的限制,如果超过则滚动!

Example:例子:

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); function sameSize(from, to) { let el = getComputedStyle(from); ["height", "width"].forEach(p => to.style[p] = el[p]); } sameSize(advancedView, basicView); advancedView.style.display = "none"; basicButton.addEventListener("click", function() { advancedView.style.display = "none"; basicView.style.display = "block"; }) advancedButton.addEventListener("click", function() { advancedView.style.display = "block"; basicView.style.display = "none"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #basic, #advanced { overflow-y: scroll; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>


Add an Overflow Hidden property:添加溢出隐藏属性:

#basic, #advanced { 
 overflow: hidden;
}

This will say to the divs " Anything outside of the rendered box is invisible , capeche? " - which works as long as the content, you know, doesn't exceed the box to begin with.这将对 div 说“渲染框之外的任何东西都是不可见的,capeche? ” - 只要内容不超过一开始的框,它就可以工作。

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); function sameSize(from, to) { let el = getComputedStyle(from); ["height", "width"].forEach(p => to.style[p] = el[p]); } sameSize(advancedView, basicView); advancedView.style.display = "none"; basicButton.addEventListener("click", function() { advancedView.style.display = "none"; basicView.style.display = "block"; }) advancedButton.addEventListener("click", function() { advancedView.style.display = "block"; basicView.style.display = "none"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #basic, #advanced { overflow: hidden; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>


Reduce the Paragraph Tag Margins:减少段落标签边距:

#basic p, #advanced p {
  margin: 0;
}

Basically what's screwing up your layout right now are the margins on the paragraph tags within your separate tabs.基本上,现在搞砸您的布局的是单独选项卡中段落标签的边距。 You can fix this simply by removing them - short and simple.可以简单地通过删除它们来解决这个问题 - 简短而简单。 The problem lies in you having to keep an eye on the content of the divs so that they never spill over their container.问题在于您必须密切关注 div 的内容,以免它们溢出容器。 If this is likely, great!如果这是可能的,太好了! If it isn't it would be best to use one of the other answers above!如果不是,最好使用上面的其他答案之一!

Example:例子:

 var basicView = document.getElementById("basic"); var advancedView = document.getElementById("advanced"); var basicButton = document.getElementById("basicButton"); var advancedButton = document.getElementById("advancedButton"); function sameSize(from, to) { let el = getComputedStyle(from); ["height", "width"].forEach(p => to.style[p] = el[p]); } sameSize(advancedView, basicView); advancedView.style.display = "none"; basicButton.addEventListener("click", function() { advancedView.style.display = "none"; basicView.style.display = "block"; }) advancedButton.addEventListener("click", function() { advancedView.style.display = "block"; basicView.style.display = "none"; })
 #basic { background-color: rgb(255,0,0,.2); } #advanced { background-color: rgb(155,145,0,.2); }.buttonbar { flex-direction:row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #basic p, #advanced p { margin: 0; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>


Grid-Area:网格区域:

Another cool recent addition to the CSS spec is CSS Grid . CSS 规范的另一个很酷的新成员是CSS Grid

You can use display: grid on a parent element, and then assign the layout a number of ways.您可以在父元素上使用display: grid ,然后通过多种方式分配布局。

The easiest way is to use grid-area on each of the children you'd like to layout, and give them a name.最简单的方法是在您想要布局的每个孩子上使用grid-area ,并给他们一个名字。 This allows you to easily swap/adjust the layout by changing grid-template-areas with a string that defines where the children should be located, by name.这使您可以轻松地交换/调整布局,方法是使用一个字符串更改grid-template-areas ,该字符串按名称定义子项应位于的位置。 Ae

.parent {
  display: grid;
  grid-template-areas: "a b c";
}
.childLeft {
 grid-area: a;
}
.childMiddle {
 grid-area: b;
}
.childRight {
 grid-area: c;
}

Within your code using this pattern is great because it actually allows you to deal with any number of tabs as needed with a few changes of CSS and this change to your JavaScript:在您的代码中使用此模式非常棒,因为它实际上允许您根据需要处理任意数量的选项卡,只需对 CSS 进行一些更改以及对 JavaScript 的更改:

document.querySelectorAll( ".buttonbar div" )
    .forEach( element => {
        let tabID = element.id.replace( "Button", "" ), 
    elementTab = document.querySelector("#container #" + tabID),
    otherTabs = document.querySelectorAll( "#container  div:not([id='" + tabID + "'])");

    //on btn click
    element.addEventListener( "click", function() {
      //hide other tabs
      otherTabs.forEach( tab => {
                        tab.style.display = "none";
            });
      //show clicked tab
                elementTab.style.display = "block";
      //change template area
                container.style.gridTemplateAreas = "'" + tabID + "'";
            } );
    } )

 let container = document.querySelector( "#container" ); document.querySelectorAll( ".buttonbar div" ).forEach( element => { let tabID = element.id.replace( "Button", "" ), elementTab = document.querySelector("#container #" + tabID), otherTabs = document.querySelectorAll( "#container div:not([id='" + tabID + "'])"); //on btn click element.addEventListener( "click", function() { //hide other tabs otherTabs.forEach( tab => { tab.style.display = "none"; }); //show clicked tab elementTab.style.display = "block"; //change template area container.style.gridTemplateAreas = "'" + tabID + "'"; } ); } )
 #basic { background-color: rgb(255, 0, 0, .2); } #advanced { background-color: rgb(155, 145, 0, .2); }.buttonbar { flex-direction: row; }.button { display: inline-block; background-color: gainsboro; padding: 4px 8px; cursor: pointer; }.button:hover { background-color: silver; } #container { display: grid; grid-template-areas: "basic"; grid-template-rows: 150px; } #basic { grid-area: basic; } #advanced { grid-area: advanced; display: none; }
 <div id="container"> <div id="basic"> <p>Hello world basic</p> </div> <div id="advanced"> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> <p>Hello world adv</p> </div> </div> <div class="buttonbar"> <div id="basicButton" class="button"> Basic </div> <div id="advancedButton" class="button"> Advanced </div> </div>

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

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