【发布时间】:2021-08-26 00:30:58
【问题描述】:
好的,假设我有两个按钮,一个带有两个 div 的容器,下面是我的页脚,如下所示:
<button id="bt1">Show all</button>
<button id="bt2">Show curses</button>
<div class="container">
<div class="one"></div>
<div class="two"></div>
</div>
<footer></footer>
我在我的 div 中显示内容。 Div“一”的高度更高,更多。为了隐藏 div "two" 并且不重叠内容,我使用了 opacity:0。
我有两个按钮,用于显示或隐藏 div“一”或“二”(使用 jQuery)。
但是,如果我只使用 opacity:0 来隐藏 div “one”,而我显示 div “two”,则页面会留下很大的空白空间(由于 div “one” 的高度比 div “two” 高)。就像这里: enter image description here
所以我决定使用 display:none 来隐藏 div "one",以便不留空间,只显示 div "two" 的必要空间。但是我的页脚上升了!!并与 div "two" 重叠内容。
这是图片:enter image description here
我该如何解决这个问题?有任何想法吗?我意识到我的页脚移动是因为没有找到 div "one" 的内容,它上升了。对不起我的英语哈哈
在这里我给你们一个很好的例子来说明我发生了什么,你们可以复制并运行:
HTML:
var cont_all = $('.one');
var bt1 = $('#bt1');
var cont_curses = $('.two');
var bt2 = $('#bt2');
bt2.click(function(){
cont_all.css('display', 'none');
cont_curses.css('opacity', '1');
});
bt1.click(function(){
cont_curses.css('opacity', '0');
cont_all.css('display', 'flex');
});
h2 {
text-align: center;
font-size: 40px;
}
.container {
margin-top: 50px;
position: relative;
height: auto;
padding-bottom: 50px;
}
.one, .two {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
height: auto;
}
.one {
overflow: hidden;
}
.two {
width: 100%;
position: absolute;
top: 0px;
opacity: 0;
}
.chart {
width: 30%;
height: 200px;
margin: 15px;
background: #2096CE;
}
.curse {
width: 30%;
height: 200px;
margin: 15px;
background: #23AD5A;
}
footer {
height: 200px;
background: black;
color: white;
display: flex;
justify-content: center;
text-align: center;
}
p {
text-align: center;
font-size: 35px;
margin: auto 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<h2>Problem with divs</h2>
</header>
<button id="bt1">Show all</button>
<button id="bt2">Show curses</button>
<div class="container">
<div class="one">
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
<div class="chart"></div>
</div>
<div class="two">
<div class="curse"></div>
<div class="curse"></div>
<div class="curse"></div>
<div class="curse"></div>
</div>
</div>
<footer>
<p>I'm the footer</p>
</footer>
嗯,这根本不一样,因为这是一个大项目,但基本上就是这样。
【问题讨论】:
-
我们可以看看你的 CSS 吗?
-
您遇到语法错误,无法关闭
class="two的字符串,应修复该错误,因为格式错误的 HTML 有时会导致难以调试的问题。是否可以创建一个minimal reproducible example 作为 sn-p 来准确说明您遇到的问题?尝试仅从描述中对其进行概念化,并想知道所包含的代码 sn-ps 是否足以重新创建问题是开始这篇文章的一个高标准...... -
是的,你们两个都是对的。我已经更正了。
-
只需将
bottom:0px;position:fixed应用到页脚即可。
标签: javascript html jquery css web-frontend