jQuery simplemodal 禁用滚动
我在页面上滚动内容超过 2000 像素。
如果用户单击 div
,则会在简单模式窗口中弹出滚动内容。现在我的客户希望在模态窗口打开时使原始页面不可滚动。 (当然,模式应该仍然可以滚动。)
这可能吗?
编辑:我已经尝试过你的建议。基本上它可以工作,但问题有点复杂:
$(".foReadMoreLink a").click(function(){
if ($('#modalBox').length == 0)
$('body').append('<div style="display:none" id="modalBox"></div>')
$('body').css({'overflow':'hidden'});
$.post('jquery/loadarticle.php',{id:$(this).attr('id')},function(data){
$('#modalBox').html(data).modal({overlayClose:'true'});
})
return false;
});
我在链接上使用 return false,以便机器人和没有 JavaScript 的用户(是的,即 2%)可以打开文章。使用上面的代码,它可以按预期工作,但是在关闭模式后,我必须返回滚动条,但此代码将不起作用:
$(".foReadMoreLink a").click(function(){
if ($('#modalBox').length == 0)
$('body').append('<div style="display:none" id="modalBox"></div>')
$('body').css({'overflow':'hidden'});
$.post('jquery/loadarticle.php',{id:$(this).attr('id')},function(data){
$('#modalBox').html(data).modal({onClose:function(){$('body').css({'overflow':'auto'})},overlayClose:'true'});
})
return false;
});
I have more than 2000 pixels scrolling content on a page.
If the user clicks a div
a scrolling content pops up in a simplemodal window. Now my client wants to make the original page non-scrollable while the modal window is up. (Of course the modal should be still scrollable.)
Is it even possible?
Edit: I have tried your suggestions. Basically it works, but the problem is a little bit complicated:
$(".foReadMoreLink a").click(function(){
if ($('#modalBox').length == 0)
$('body').append('<div style="display:none" id="modalBox"></div>')
$('body').css({'overflow':'hidden'});
$.post('jquery/loadarticle.php',{id:$(this).attr('id')},function(data){
$('#modalBox').html(data).modal({overlayClose:'true'});
})
return false;
});
I use return false on the links so bots and users without JavaScript (yes, that's 2%) can open the articles. With the code above it works as expected, but after closing the modal I have to have back the scrollbar but this code won't work:
$(".foReadMoreLink a").click(function(){
if ($('#modalBox').length == 0)
$('body').append('<div style="display:none" id="modalBox"></div>')
$('body').css({'overflow':'hidden'});
$.post('jquery/loadarticle.php',{id:$(this).attr('id')},function(data){
$('#modalBox').html(data).modal({onClose:function(){$('body').css({'overflow':'auto'})},overlayClose:'true'});
})
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的脚本中打开模式:
并在关闭时:(
需要 HTML 和正文,因为滚动条附加到浏览器的不同部分,具体取决于您使用的浏览器)
In your script to open your modal:
And on close:
(HTML and body are required as the scroll bars are attached to different parts of the browser depending on which you are using)
打开和关闭滚动条将导致内容移动,并且覆盖层将不再覆盖整个窗口。以下是修复方法。
Turning the scrollbars on and off will cause the content to shift and the overlay will no longer cover the whole window. Here's how to fix it.
使用
在模式对话框打开时将其应用到页面,并在对话框销毁时将其删除。这将隐藏您的滚动条。
Use
Apply it to the page when modal dialog is opened and remove it when the dialog is destroyed. This will hide your scrollbar.
我发现
overflow:hidden
不太好,因为如果页面滚动到一半,它会将内容隐藏在半透明覆盖层后面。我想出了以下相当复杂的解决方案。它禁用了我发现的所有可能的可检测方式的滚动。如果页面仍然以某种方式滚动,则将滚动位置直接放回到旧位置。
I found
overflow:hidden
not so nice since it hides the content behind the semi-transparant overlay if the page is scrolled halfway.I came up with the following, rather elaborate solution. It disables scrolling in all possible detectable ways I found. And puts the scrollposition straight back to the old position if the page was still scrolled somehow.
这个选项就像一个魅力:
This option works like a charm:
就我而言,在标记
param
href = "#"
中。所以解决方案是:
In my case in tag
<a>
paramhref = "#"
.So solution be: