简体   繁体   English

两个div重叠时删除一个类

[英]Remove a class when two divs overlap

I have used flexbox to center my content vertically inside of 'main' tag, however when too much content is added it spills over into the 'header'. 我已经使用flexbox将内容垂直居中在“ main”标签内,但是当添加太多内容时,它会溢出到“ header”中。 Is there a way I can calculate that if the div goes above a certain vertical position on screen (256px - height set as header), it removes a class from the 'main' (currently set to .vertical). 有没有一种方法可以计算出,如果div超出屏幕上某个垂直位置(256像素-设置为标题的高度),它将从“主”中移除一个类(当前设置为.vertical)。

I know that the .removeClass() removes the class, but I dont know where to start with the vertical position calculation. 我知道.removeClass()删除了该类,但是我不知道从哪里开始进行垂直位置计算。

HTML HTML

<header>Nav</header>
<main class="vertical">A lot of text here</main>

CSS CSS

body, html{margin:0; height:100%}

header{width:100%; height:256px; background:red;}
main{width:100%; height: calc(100% - 256px); background:#fff;}

.vertical{
display: flex;
flex-direction: column;
justify-content: center;
}

Fiddle 小提琴

I do hope that makes sense. 我确实希望这是有道理的。 Many thanks Thanks. 非常感谢。

I may misunderstand your goal, but it doesn't seem like you need to calculate the position on the screen. 我可能会误解您的目标,但似乎不需要计算屏幕上的位置。 Since you have a Nav bar, it should always be visible and the content shouldn't overlap. 由于您具有导航栏,因此它应该始终可见并且内容不应重叠。 I made a few changes to your code that allows the content to always sit underneath the header using justify-content: flex-start . 我对您的代码进行了一些更改,以使内容始终可以使用justify-content: flex-start位于标题下方。

body, html {
  margin: 0;
  height: 100%
}

header {
  display: block;
  width: 100%;
  height: 256px;
  background: red;
}

main {
  width: 100%;
  height: 100%;
  background: #fff;
 }

.vertical{
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
}

If you still want to align the text differently, you could nest the content within another tag inside .vertical . 如果仍要以其他方式对齐文本,则可以将内容嵌套在.vertical内的另一个标签中。 Like so: 像这样:

<header>Nav</header>
<main class="vertical">
  <p class="content">all the text...</p>
</main>

And then add vertical styles to the .content section. 然后将垂直样式添加到.content部分。

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

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