如何在当前滚动位置垂直居中对话框

huangapple go评论92阅读模式
英文:

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 -->

$(&#39;.show-dialog-btn&#39;).click(function() {

  $(&#39;#dialog-box&#39;).addClass(&#39;display-dialog-box&#39;);
});



$(&#39;.close-dialog-btn&#39;).click(function() {

  $(&#39;#dialog-box&#39;).removeClass(&#39;display-dialog-box&#39;);
});

<!-- 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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!--- Locations 1 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 1&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Locations 2 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 2&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Locations 3 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 3&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Dialog Box ----&gt;
&lt;div id=&quot;dialog-box&quot;&gt;

  &lt;h1&gt;Dialog box&lt;/h1&gt;
  &lt;p&gt;Dialog box text&lt;/p&gt;
  &lt;button class=&quot;close-dialog-btn&quot;&gt;Close box &lt;/button&gt;
&lt;/div&gt;

<!-- 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 -->

$(&#39;.show-dialog-btn&#39;).click(function() {

  $(&#39;#dialog-box&#39;).addClass(&#39;display-dialog-box&#39;);
  $(&#39;body&#39;).addClass(&#39;disable-scroll&#39;);
});



$(&#39;.close-dialog-btn&#39;).click(function() {

  $(&#39;#dialog-box&#39;).removeClass(&#39;display-dialog-box&#39;);
  $(&#39;body&#39;).removeClass(&#39;disable-scroll&#39;);
});

<!-- 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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;!--- Locations 1 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 1&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Locations 2 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 2&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Locations 3 Div ----&gt;
&lt;div class=&quot;locations-container&quot;&gt;
  &lt;h2&gt;Locations 3&lt;/h2&gt;
  &lt;p&gt;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!&lt;/p&gt;

  &lt;button class=&quot;show-dialog-btn&quot;&gt;Show Dialog&lt;/button&gt;
&lt;/div&gt;


&lt;!--- Dialog Box ----&gt;
&lt;div id=&quot;dialog-box&quot;&gt;

  &lt;h1&gt;Dialog box&lt;/h1&gt;
  &lt;p&gt;Dialog box text&lt;/p&gt;
  &lt;button class=&quot;close-dialog-btn&quot;&gt;Close box &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 02:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定