How to prevent background scrolling when opening a modal window with javascript?

Asked

Viewed 847 times

1

How do I get the background to stop rolling when I open my modal window? I’ve tried some options with javascript but all continue to scroll.

Follows the code:

HTML:

<div id="openModal" class="modalDialog">

  <div class="body">

    <div class="teste3">

      <div class="teste2">

        <a href="#close" title="Close" class="close">X</a>

        <iframe class="teste" src="/"></iframe>

      </div>
    </div>
  </div>
</div>

    <p class="parah"><span class="ph"><a href="#openModal2"><img   alt="Illustration" src="../Images/video.png" /></a></span> <strong>Vídeo 3.2</strong> Texto para testar o segundo video desse livro.</p>

CSS:

.modalDialog {
        position: fixed;
        font-family: Arial, Helvetica, sans-serif;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
background: rgba(0,0,0,0.8);
        z-index: 99999;
        opacity:0;
        -webkit-transition: opacity 400ms ease-in;
        -moz-transition: opacity 400ms ease-in;
        transition: opacity 400ms ease-in;
        pointer-events: none;




    }

    .modalDialog:target {
        opacity:1;
        pointer-events: auto;
            position: fixed; 

    }

    .modalDialog > div {
        width: 640px;
        position: relative;
        margin: 10% auto;


    }

    .close {
        background: crimson;;
        color: #FFFFFF;
        line-height: 30px;
        position: absolute;
        right: -12px;
        text-align: center;
        top: -10px;
        width: 30px;
        text-decoration: none;
        font-weight: bold;
        -webkit-border-radius: 15px;
        -moz-border-radius: 15px;
        border-radius: 15px;
        -moz-box-shadow: 1px 1px 3px #000;
        -webkit-box-shadow: 1px 1px 3px #000;
        box-shadow: 1px 1px 3px #000;
        cursor: pointer;/**/
            z-index: 1;

    }

    .close:hover { background: red; }



.teste {
      position: absolute;
      top: 0;
      left: 0;
      width: 100% !important;
      height: 100% !important;
    }
    .teste2 {
      position: relative;
      width: 100%;
      max-width: 100%;
      padding-top: 56.5%;
    }

   .teste3 {
       width: 640px;
       margin: auto;
  }




.body {
  padding:0.75em 10.5%;/*0.75em 10.5%;*/
  margin-top: 2em;
  margin-bottom: 5em;
text-align: center;
  clear: both;
}
  • Try this friend <!-- Begin snippet: js Hide: false console: true Babel: false --> <!-- language: lang-css --> overflow: Hidden; <!-- end snippet -->

  • So I tried and it didn’t work either. Actually I need the scroll to stop rolling when I click the modal open button and then scroll back when I close the modal.

  • You are using Bootstrap?

3 answers

1

If you are using bootstrap, as it seems, add to this class

body.modal-open {
    overflow: hidden;
}

1

With jQuery:

$('#myModal').on('show.bs.modal', function (e) {
      $('body').css({ 'overflow' : 'hidden' });
    })

    $('#myModal').on('hide.bs.modal', function (e) {
      ('body').css({ 'overflow' : 'initial' });
    })

Source

  • They removed the modal-open again?

  • I don’t know friend. I answered using the most objective way, with Istener for events and style to force the definition of body overflow

0


You can add a class to your CSS that will disable the scroll page:

.sem-scroll{
   overflow: hidden;
}

Then use the method .toggleClass to toggle the class on <body>:

$("span.ph a, a.close").on("click", function(){
   $("body").toggleClass("sem-scroll");
});

Where span.ph a is the element that opens the modal and a.close what closes.

See example (run in full screen):

$("span.ph a, a.close").on("click", function(){
   $("body").toggleClass("sem-scroll");
});
.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;




}

.modalDialog:target {
opacity:1;
pointer-events: auto;
position: fixed; 

}

.modalDialog > div {
width: 640px;
position: relative;
margin: 10% auto;


}

.close {
background: crimson;;
color: #FFFFFF;
line-height: 30px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 30px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
cursor: pointer;/**/
z-index: 1;

}

.close:hover { background: red; }



.teste {
position: absolute;
top: 0;
left: 0;
width: 100% !important;
height: 100% !important;
}
.teste2 {
position: relative;
width: 100%;
max-width: 100%;
padding-top: 56.5%;
}

.teste3 {
width: 640px;
margin: auto;
}




.body {
padding:0.75em 10.5%;/*0.75em 10.5%;*/
margin-top: 2em;
margin-bottom: 5em;
text-align: center;
clear: both;
}


.sem-scroll{
   overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="openModal" class="modalDialog">

  <div class="body">

    <div class="teste3">

      <div class="teste2">

        <a href="#close" title="Close" class="close">X</a>

        <iframe class="teste" src="/"></iframe>

      </div>
    </div>
  </div>
</div>

<p class="parah"><span class="ph"><a href="#openModal"><img alt="Illustration" src="../Images/video.png" /></a></span> <strong>Vídeo 3.2</strong> Texto para testar o segundo video desse livro.</p>

<div style="display: block; height: 1500px;">
    exemplo para fazer scroll
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.