英文:
How to make certain html tags cause overflow and others don't?
问题
这是一个棘手的问题,我在网上找不到确切的答案。
这是我想要的效果:
想象一下在Html文件中的这个设置:
<div class="main">
<div class="holder holderA"></div>
<div class="holder holderB"></div>
<div class="holder holderC"></div>
<button type="button">按下我</button>
</div>
使用以下样式:
.main {
width: 100vw;
min-height: 100vh;
overflow: auto;
}
.holder {
width: 100vw;
height: 100vh;
}
.holderA {
display: none;
}
button {
position: fixed;
top: 10px;
left: 10px;
}
我试图做的是让按钮显示第一个holder,而当它显示时,只显示第一个holder,如果需要,会有溢出的感觉。
如果你曾经使用过Bootstrap模态框,你会知道当模态框出现时,溢出只会影响到模态框,而背景内容是静态的。这就是我想要的结果,但我不知道如何实现它。
我查看了模态框的CSS文件源代码,但似乎没有什么特别的事情发生...所以有人能解释一下吗?
此外,这是一个示例:
这是在没有打开模态框的Bootstrap页面,当你打开模态框时:
如你所见,溢出被重置,背景中的内容现在是静态的,溢出只由模态框引起,当你滚动时:
滚动的只是模态框,其余内容保持静态!
我如何在纯CSS和/或JavaScript中实现这个效果?
感谢任何答案或评论
英文:
So, this is a tricky question, and I found no certain answer online.
Here is what I want:
Imagine this setup in the Html file:
<div class="main">
<div class="holder holderA"></div>
<div class="holder holderB"></div>
<div class="holder holderC"></div>
<button type="button">press me</button>
</div>
with the following styles:
.main {
width: 100vw;
min-height: 100vh;
overflow: auto;
}
.holder {
width: 100vw;
height: 100vh;
}
.holderA {
display: none;
}
button {
position: fixed;
top: 10px;
left: 10px;
}
what I'm trying to do is somehow have the button display the first holder, and when it does, only the first holder, causing any sense of overflow if required.
If you have ever worked with Bootstrap modals, you know how when the modal is over, the overflow is only caused by the modal and the rest of the content is static on the back? that's the result that I'm looking for, but I have no idea how to implement it.
I looked up the source code for the css file on modals, but really nothing special seems to be going on ... so can anyone explain?
also, here is the example:
this is the page in bootstrap, without the modal open, and when you open the modal:
as you can see, the overflow is reset, and the content in the background is now static, the overflow is only and only caused by the the modal, and when you scroll:
all that is scrolled is the modal, the rest being static!
how can I implement this on vanilla css, and or javascript?
Any answer or comment is appreciated
答案1
得分: 1
这通常被称为创建一个 "模态叠加" 或 "模态对话框" 效果。虽然 Bootstrap 提供了一个预建解决方案,但你也可以使用原始的 CSS 和 JavaScript 来实现它。
HTML:
<div class="main">
<div class="holder holderA"></div>
<div class="holder holderB"></div>
<div class="holder holderC"></div>
<button id="openModalButton" type="button">打开模态框</button>
</div>
<div id="modal" class="modal">
<div class="modal-content">
<!-- 这里放置你的模态框内容 -->
<button id="closeModalButton" type="button">关闭模态框</button>
</div>
</div>
CSS:
.main {
width: 100vw;
min-height: 100vh;
overflow: hidden;
position: relative;
}
.holder {
width: 100vw;
height: 100vh;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5); /* 半透明背景 */
}
.modal-content {
background-color: white;
width: 80%;
max-height: 80%;
overflow: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
position: fixed;
top: 10px;
left: 10px;
}
JavaScript:
const openModalButton = document.getElementById('openModalButton');
const closeModalButton = document.getElementById('closeModalButton');
const modal = document.getElementById('modal');
openModalButton.addEventListener('click', () => {
modal.style.display = 'block';
document.body.style.overflow = 'hidden'; // 防止背景滚动
});
closeModalButton.addEventListener('click', () => {
modal.style.display = 'none';
document.body.style.overflow = 'auto'; // 恢复背景滚动
});
英文:
This is typically referred to as creating a "modal overlay" or "modal dialog" effect. While Bootstrap provides a pre-built solution for this, you can implement it using vanilla CSS and JavaScript as well.
HTML:
<div class="main">
<div class="holder holderA"></div>
<div class="holder holderB"></div>
<div class="holder holderC"></div>
<button id="openModalButton" type="button">Open Modal</button>
</div>
<div id="modal" class="modal">
<div class="modal-content">
<!-- Your modal content goes here -->
<button id="closeModalButton" type="button">Close Modal</button>
</div>
</div>
CSS:
.main {
width: 100vw;
min-height: 100vh;
overflow: hidden;
position: relative;
}
.holder {
width: 100vw;
height: 100vh;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
}
.modal-content {
background-color: white;
width: 80%;
max-height: 80%;
overflow: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
position: fixed;
top: 10px;
left: 10px;
}
Javascript :
const openModalButton = document.getElementById('openModalButton');
const closeModalButton = document.getElementById('closeModalButton');
const modal = document.getElementById('modal');
openModalButton.addEventListener('click', () => {
modal.style.display = 'block';
document.body.style.overflow = 'hidden'; // Prevent background scrolling
});
closeModalButton.addEventListener('click', () => {
modal.style.display = 'none';
document.body.style.overflow = 'auto'; // Restore background scrolling
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论