首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模式打开时允许后台div滚动

如何在模式打开时允许后台div滚动
EN

Stack Overflow用户
提问于 2021-02-26 16:51:28
回答 1查看 36关注 0票数 0

阴影视图是一个模式,当它弹出时,我仍然希望我的背景div在阴影视图后面滚动。有没有办法做到这一点?

代码语言:javascript
复制
.main-wrapper {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
 }

我需要将overflow-y设置为auto,以便实现react-infinite scroller框架。

这是模态HTML和css

代码语言:javascript
复制
<div class="modal-backdrop"></div>
<div class="modal"
  tabindex="-1"
  role="dialog"
  (click)="onClose()"
  >
</div>
.modal {
   overflow-x: hidden;
   overflow-y: auto;
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-26 17:04:08

这就是你要找的东西

代码语言:javascript
复制
const showDialog = () => {
  document.getElementById('dialog').classList.add('show');
  const scrollY = document.documentElement.style.getPropertyValue('--scroll-y');
  const body = document.body;
};
const closeDialog = () => {
  const body = document.body;
  const scrollY = body.style.top;
  body.style.position = '';
  body.style.top = '';
  window.scrollTo(0, parseInt(scrollY || '0') * -1);
  document.getElementById('dialog').classList.remove('show');
};
window.addEventListener('scroll', () => {
  document.documentElement.style.setProperty('--scroll-y', `${window.scrollY}px`);
});
代码语言:javascript
复制
body {
  display: flex;
  font-size: 1.5em;
  justify-content: center;
  line-height: 1.5;
  margin: 0;
  padding: 0;
}
#text {
  max-width: 800px;
}
#dialog {
  display: none;
  position: fixed;
  top: 10vh;
  left: 10vw;
  width: 80vw;
  height: 80vw;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 10px;
  text-align: center;
  z-index: 1;
  background-color: #444;
  color: #fff;
}
#dialog.show {
  align-items: center;
  display: flex;
  flex-direction: column;
}
#dialog span {
  margin-bottom: 1em;
}
#show,
#close {
  background: #da1b60;
  border: none;
  color: #fff;
  display: block;
  font-size: 1em;
  padding: 1em;
  width: 200px;
}
#show:hover,
#close:hover {
  background: #ff8a00;
  cursor: pointer;
}
#show {
  position: fixed;
  top: 30px;
  left: 10px;
}
#close {
  position: relative;
}
代码语言:javascript
复制
<button id='show' onClick='showDialog()'>Show Dialog</button>
<div id='text'>
...Your Content here...
</div>
<div id='dialog'>
  <span>Oh wow, modal! We can close this now.</span>
  <button id='close' onClick='closeDialog()'>Close Dialog</button>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66382745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档