简体   繁体   English

带有页面滚动条的jQuery模式滚动

[英]jQuery modal scrolling with page scrollbars

When a jQuery modal has loaded... how do you make it so the page scrollbar scroll the modal and not the page? 加载了jQuery模态后,如何实现它,以便页面滚动条滚动模态而不是页面?

See this example on 123reg... https://www.123-reg.co.uk/order/domain?domain=somedomain.co.uk&is=1 and click '.co.uk taken' then scroll. 请参阅123reg ...上的示例... https://www.123-reg.co.uk/order/domain?domain=somedomain.co.uk&is=1 ,然后单击“ .co.uk采取”,然后滚动。

If you then close the modal and click '.uk.com Available' the modal is shorter and the page scrollbars are disabled until closing the modal. 如果您随后关闭模式并单击“ .uk.com可用”,则模式会更短,并且页面滚动条将被禁用,直到关闭模式。

Could anyone give an example how this works? 谁能举例说明这是如何工作的?

Thanks in advance! 提前致谢! :) :)

Use position:absolute instead of position:fixed in your main modal container class 在主要的模态容器类中使用position:absolute而不是position:fixed

.modal_name {       
    position: absolute;        
}

Just add this style to your stylesheet and it should work: 只要将此样式添加到样式表中,它就可以工作:

.modal-open {
   overflow: inherit; 
}

This is happening because of "modal-scrollable" class which is applied at the parent div of the modal-popup . 发生这种情况是因为在模态弹出窗口的父div上应用了“模态可滚动”类。 This "modal-scrollable" class has style "overflow:auto and overflow:y-scroll" . 这个“模态可滚动”类的样式为“ overflow:auto和overflow:y-scroll”。 If you remove these styles the page scroll bar will be no more present and you won't be able to scroll down the modal-popup then . 如果您删除这些样式,则页面滚动条将不再存在,然后您将无法向下滚动modal-popup。 The modal-poup is bigger than the body height, so overflow style works here . 模态水袋大于身高,因此这里采用溢流式设计。 For the second popup , it's not bigger than the body height , so scroll doesn't comes over there . 对于第二个弹出窗口,它不大于身体高度,因此滚动不会在那上面。 'overflow' style only works there where the element is reaching beyond the specified limit ( like div has max-height:450px and when the content in the div make it to reach beyond 450 px then it will show scroll . The condition is overflow:auto or overflow:y-scroll is added with it) . 'overflow'样式仅在元素超出指定限制的地方起作用(例如div的max-height:450px,并且当div中的内容达到450 px以上时,它将显示scroll。条件是溢出:自动或溢出:y-scroll已添加)。

You can handle these things via jquery too .Just to make you better understand I have created two function ShowModal and HideModal :- 您也可以通过jquery处理这些事情。只是为了让您更好地了解我创建了两个函数ShowModal和HideModal:-

 <div class="modal" id="popupModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header ">                  
                <button type="button" class="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <p>Something to be written<p>          
        </div>
    </div>
</div>

Script 脚本

function ShowModal()
{
$("#popupModal").modal('show'); //this will show the page scroll bar for modal popup only . This is handled by bootstrap itself
$('body').css({overflow:'hidden'); // this will hide the body scroll bar 
}

function HideModal(){
$("#popupModal").modal('hide'); 
$('body').css({overflow:'auto'}); //this will show the scroll bar 
}

Hope my answer help you !! 希望我的回答对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM