我按照在线教程(http://uposonghar.com/popup.html)创建了一个jQuery弹出窗口。
由于我对jQuery的了解不多,我不能按照我的要求使它工作。
我的问题:
1.我想禁用网页滚动,而弹出是活跃的。
1.背景褪色的弹出窗口的颜色,而积极的是不工作的完整网页。
CSS:
body {
background: #999;
}
#ac-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,.6);
z-index: 1001;
}
#popup{
width: 555px;
height: 375px;
background: #FFFFFF;
border: 5px solid #000;
border-radius: 25px;
-moz-border-radius: 25px;
-webkit-border-radius: 25px;
box-shadow: #64686e 0px 0px 3px 3px;
-moz-box-shadow: #64686e 0px 0px 3px 3px;
-webkit-box-shadow: #64686e 0px 0px 3px 3px;
position: relative;
top: 150px; left: 375px;
}
字符串
JavaScript:
<script type="text/javascript">
function PopUp(){
document.getElementById('ac-wrapper').style.display="none";
}
</script>
型
超文本标记语言:
<div id="ac-wrapper">
<div id="popup">
<center>
<p>Popup Content Here</p>
<input type="submit" name="submit" value="Submit" onClick="PopUp()" />
</center>
</div>
</div>
<p>Page Content Here</p>
型
9条答案
按热度按时间s4n0splo1#
一个简单的答案是,将
#ac-wrapper
的位置设置为固定,这是您可以使用的,并且不需要停止滚动事件。例如。
字符串
这将使弹出窗口的容器始终可见(左上对齐),但仍允许滚动。
但滚动页面弹出打开是不好的!!!(几乎总是无论如何)
你不想允许滚动的原因是因为如果你的弹出窗口不是全屏的或者是半透明的,用户会看到弹出窗口后面的内容滚动。除此之外,当他们关闭弹出窗口时,他们现在将处于页面上的不同位置。
一个解决方案是,当你在JavaScript中绑定一个
click
事件来显示这个弹出窗口时,也要添加一个类到主体中,基本上有以下规则:型
然后,当通过触发某些操作或单击关闭弹出窗口时,您只需再次删除该类,允许在弹出窗口关闭后滚动。
如果用户在弹出窗口打开时滚动,则文档将不会滚动。当用户关闭弹出窗口时,滚动将再次可用,用户可以继续他们离开的地方:)
5ktev3wc2#
要禁用滚动条,请执行以下操作:
字符串
这将隐藏滚动条
Background-fade-thing:
我创建了自己的弹出对话框小部件,它也有一个背景。我使用了以下CSS:
型
bkhjykvo3#
我也遇到过类似的问题;想要在显示“弹出”div时禁用垂直滚动。
更改
body
的overflow属性确实有效,但也会影响文档的宽度。我选择了jquery来解决这个问题,并为滚动条使用了占位符。这是在没有绑定到
scroll
事件的情况下完成的,因此这不会改变滚动条的位置或导致 Flink :)超文本标记语言:
字符串
CSS:
型
Jquery:
型
希望这对你有帮助。
xtfmy6hx4#
如果简单切换body的'overflow-y'会破坏页面的滚动位置,请尝试使用以下2个函数(jQuery):
字符串
czfnxgou5#
使用下面的代码禁用和启用滚动条。
字符串
des4xlb06#
https://jsfiddle.net/satishdodia/L9vfhdwq/1/
**html:-**打开弹出窗口
弹出
弹出打开滚动停止现在...当我将点击关闭自动滚动运行。
关闭
字符串
rsl1atfo7#
我也遇到了同样的问题,并找到了一种方法来摆脱它,你只需要停止在弹出的元素上传播touchmove。对我来说,这是全屏菜单,出现在屏幕上,你不能滚动,现在你可以。
字符串
cotxawn78#
这个解决方案对我有效。
HTML:
字符串
CSS:
型
JS:
型
eaf3rand9#
我遇到了这个问题,并尝试了几种解决方案,这是解决我的问题的文章(https://css-tricks.com/prevent-page-scrolling-when-a-modal-is-open/),它很简单!
它使用“固定身体”解决方案,这在很多帖子中很常见。这个解决方案的问题是,当弹出窗口关闭时,正文将滚动回顶部。但文章指出:在使用该解决方案时,通过操作CSS top和position属性,我们可以恢复滚动位置。
该解决方案的另一个问题是,您不能将该解决方案应用于多个弹出窗口场景。所以我添加了一个变量来存储弹出窗口的计数,只是为了确保程序不会在错误的时间触发初始化进程或重置进程。
以下是我得到的最终解决方案:
字符串
你也可以在我的Codepen上看到演示:https://codepen.io/tabsteveyang/pen/WNpbvyb的
编辑
关于‘固定体’解决方案的更多信息
该方法主要是将body元素的CSS position属性设置为'fixed',以使其不可滚动。无论它已经滚动了多远,当主体固定时,它将滚动回顶部,这是我不希望看到的行为。(想象一下,用户正在浏览一个很长的内容,几乎滚动到页面的底部,突然弹出一个弹出窗口,使页面滚动回到顶部,这是一个糟糕的用户体验)
文章中的解决方案
基于'fixed body'方法,另外,该解决方案将正文的CSS顶部设置为'-window.scrollY px'的值,以使正文看起来像是在固定时停留在当前滚动位置。此外,该解决方案使用CSS body的顶部作为临时引用,以便我们在希望使body再次可滚动时可以通过属性检索滚动位置。(注意,您必须将位置乘以-1以使其为正)