最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - make page stop scrolling when lightbox is active - Stack Overflow

programmeradmin0浏览0评论

I have the following lightbox which includes a form. Everything works fine. My only problem is how to make the html page stop scrolling when the lightbox is active.

<a href = "javascript:void(0)" onclick="
  document.getElementById('light').style.display='block';
  document.getElementById('fade').style.display='block'">
<img src="img/add.jpg"/></a></p>
<div id="light" class="white_content">

<input name="degree_1" type="text" size="73"
   value="<?php echo $user_data_profile_education['degree_1'];?>"/>
</br></br>
Grade</br>
<input name="grade_1" type="text" size="73"
   value="<?php echo $user_data_profile_education['grade_1'];?>"/>

<a href = "javascript:void(0)" onclick="
  document.getElementById('light').style.display='none';
  document.getElementById('fade').style.display='none'"> 
</br><img src="img/done_editing.jpg"/> </a></div>
<div id="fade" class="black_overlay"></div>    

and this is my css:

.black_overlay{
    display: none;
    position: absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 220%;
    background-color: grey;
    z-index:1001;
    -moz-opacity: 0.8;
    opacity:.80;
    filter: alpha(opacity=80);
}


.white_content {
    display: none;
    position: absolute;
    top: 45%;
    left: 30%;
    width: 32%;
    height: 51%;
    padding: 30px;
    padding-left:50px;
    border: 5px solid green;
    background-color: white;
    z-index:1002;
    overflow: auto;
}

I have the following lightbox which includes a form. Everything works fine. My only problem is how to make the html page stop scrolling when the lightbox is active.

<a href = "javascript:void(0)" onclick="
  document.getElementById('light').style.display='block';
  document.getElementById('fade').style.display='block'">
<img src="img/add.jpg"/></a></p>
<div id="light" class="white_content">

<input name="degree_1" type="text" size="73"
   value="<?php echo $user_data_profile_education['degree_1'];?>"/>
</br></br>
Grade</br>
<input name="grade_1" type="text" size="73"
   value="<?php echo $user_data_profile_education['grade_1'];?>"/>

<a href = "javascript:void(0)" onclick="
  document.getElementById('light').style.display='none';
  document.getElementById('fade').style.display='none'"> 
</br><img src="img/done_editing.jpg"/> </a></div>
<div id="fade" class="black_overlay"></div>    

and this is my css:

.black_overlay{
    display: none;
    position: absolute;
    top: 0%;
    left: 0%;
    width: 100%;
    height: 220%;
    background-color: grey;
    z-index:1001;
    -moz-opacity: 0.8;
    opacity:.80;
    filter: alpha(opacity=80);
}


.white_content {
    display: none;
    position: absolute;
    top: 45%;
    left: 30%;
    width: 32%;
    height: 51%;
    padding: 30px;
    padding-left:50px;
    border: 5px solid green;
    background-color: white;
    z-index:1002;
    overflow: auto;
}
Share Improve this question edited Mar 31, 2013 at 16:46 Jan Turoň 32.9k23 gold badges137 silver badges177 bronze badges asked Mar 31, 2013 at 16:39 3352833528 3826 gold badges12 silver badges30 bronze badges 2
  • see stackoverflow.com/questions/9280258/… – Tamil Selvan C Commented Mar 31, 2013 at 16:45
  • 1 If you want the lightbox content to be stationary even with the scroll of pages, you can set the lightbox css to position: fixed; – Arunkumar Srisailapathi Commented Mar 31, 2013 at 16:54
Add a comment  | 

4 Answers 4

Reset to default 19

Add overflow: hidden to your body when you show the lightbox and overflow: auto when you hide it.

Use onLoad/onClose to change the css of the div that holds the page content

Setting .lb_overlay height is a nice touch, add the same "margin" from the top of the modal to the bottom so scrolling to the bottom doesn't just sit right at the bottom of the window

var modal = $("#modal"),
    wrapper = $("#wrapper")

$("#modalTrigger").click(function(){
    modal.lightbox_me({
        centered: true,
        onLoad : function(){
            wrapper.css({
                "position" : "fixed",
                "width" : "100%"
            })
            $(".lb_overlay").css("height" , parseInt($(window).height()) + parseInt(modal.css("top") ) + "px" )
        },
        onClose : function(){
            wrapper.css("position" , "relative")
        }
    })
})

If somebody still needs it, you can use lightbox events:

let lg = document.getElementById('lightgallery');

lg.addEventListener('onAfterOpen', function(e) {
    $("html, body").css("overflow","hidden");
}, false);

lightGallery(lg);

lg.addEventListener('onCloseAfter', function(e) {
    $("html, body").css("overflow","auto");
}, false);

lightGallery(lg);

Just add this script to disable scrollign in lighbox

<script>
        lightbox.option({
    disableScrolling:true
    })
</script>
发布评论

评论列表(0)

  1. 暂无评论