英文:
How to vertically center dialog box at the its current scroll position
问题
当点击任何一个"显示对话框"按钮时,我应该如何使对话框在窗口的当前滚动位置垂直居中?
示例:当我点击位置3的底部时,我试图使对话框在当前视窗屏幕上垂直居中。这对所有按钮都应该成立。
$('.show-dialog-btn').click(function() {
$('#dialog-box').addClass('display-dialog-box');
});
$('.close-dialog-btn').click(function() {
$('#dialog-box').removeClass('display-dialog-box');
});
.locations-container {
padding: 10px;
}
.locations-container:not(:last-of-type) {
margin-bottom: 100px;
}
.locations-container:nth-child(3) {
background: #eee;
}
#dialog-box {
top: 50%;
left: 50%;
z-index: 99;
display: none;
width: 220px;
color: #4a4a4a;
position: absolute;
border-radius: 10px;
background: #fff;
padding: 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: 1px 5px 4px rgba(0, 0, 0, 0.1);
}
#dialog-box.display-dialog-box {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--- 位置1 Div ---->
<div class="locations-container">
<h2>位置1</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">显示对话框</button>
</div>
<!--- 位置2 Div ---->
<div class="locations-container">
<h2>位置2</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">显示对话框</button>
</div>
<!--- 位置3 Div ---->
<div class="locations-container">
<h2>位置3</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">显示对话框</button>
</div>
<!--- 对话框 ---->
<div id="dialog-box">
<h1>对话框</h1>
<p>对话框文本</p>
<button class="close-dialog-btn">关闭对话框</button>
</div>
英文:
How would I be able to vertically center the dialog box at its current scroll position of the window when either one of the "show dialog" buttons is clicked?
Example: When I click the bottom on locations 3. I'm trying to get the dialog box to be vertically centered at that current viewing window screen. And this should be the case for all buttons.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('.show-dialog-btn').click(function() {
$('#dialog-box').addClass('display-dialog-box');
});
$('.close-dialog-btn').click(function() {
$('#dialog-box').removeClass('display-dialog-box');
});
<!-- language: lang-css -->
.locations-container {
padding: 10px
}
.locations-container:not(last-of-type) {
margin-bottom: 100px
}
.locations-container:nth-child(3) {
background: #eee
}
#dialog-box {
top: 50%;
left: 50%;
z-index: 99;
display: none;
width: 220px;
color: #4a4a4a;
position: absolute;
border-radius: 10px;
background: #fff;
padding: 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: 1px 5px 4px rgba(0, 0, 0, 0.1)
}
#dialog-box.display-dialog-box {
display: block
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--- Locations 1 Div ---->
<div class="locations-container">
<h2>Locations 1</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Locations 2 Div ---->
<div class="locations-container">
<h2>Locations 2</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Locations 3 Div ---->
<div class="locations-container">
<h2>Locations 3</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Dialog Box ---->
<div id="dialog-box">
<h1>Dialog box</h1>
<p>Dialog box text</p>
<button class="close-dialog-btn">Close box </button>
</div>
<!-- end snippet -->
答案1
得分: 1
只需在#dialog-box
中将position: absolute
替换为fixed
:
#dialog-box {
top: 50%;
left: 50%;
z-index: 99;
display: none;
width: 220px;
color: #4a4a4a;
position: fixed; /* 将此行的position属性改为fixed */
border-radius: 10px;
background: #fff;
padding: 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: 1px 5px 4px rgba(0, 0, 0, 0.1);
}
英文:
Just replace position: absolute
with fixed
in #dialog-box
:
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
$('.show-dialog-btn').click(function() {
$('#dialog-box').addClass('display-dialog-box');
$('body').addClass('disable-scroll');
});
$('.close-dialog-btn').click(function() {
$('#dialog-box').removeClass('display-dialog-box');
$('body').removeClass('disable-scroll');
});
<!-- language: lang-css -->
.locations-container {
padding: 10px
}
.locations-container:not(last-of-type) {
margin-bottom: 100px
}
.locations-container:nth-child(3) {
background: #eee
}
#dialog-box {
top: 50%;
left: 50%;
z-index: 99;
display: none;
width: 220px;
color: #4a4a4a;
position: fixed;
border-radius: 10px;
background: #fff;
padding: 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: 1px 5px 4px rgba(0, 0, 0, 0.1)
}
#dialog-box.display-dialog-box {
display: block
}
body.disable-scroll {
overflow: hidden !important;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--- Locations 1 Div ---->
<div class="locations-container">
<h2>Locations 1</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Locations 2 Div ---->
<div class="locations-container">
<h2>Locations 2</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Locations 3 Div ---->
<div class="locations-container">
<h2>Locations 3</h2>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Delectus aliquid quas, ut fugiat, ipsum, veniam aut fuga impedit sunt laboriosam praesentium ullam quisquam eos rem voluptas corporis vel hic natus!</p>
<button class="show-dialog-btn">Show Dialog</button>
</div>
<!--- Dialog Box ---->
<div id="dialog-box">
<h1>Dialog box</h1>
<p>Dialog box text</p>
<button class="close-dialog-btn">Close box </button>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论