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

1

How do I get the background to stop scrolling when I open my modal window? I already tried some options with javascript but all continue scrolling.

Follow 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="https://pt.stackoverflow.com/"></iframe></div></div></div></div><pclass="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;
}
    
asked by anonymous 17.01.2018 / 19:07

3 answers

0

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

.sem-scroll{
   overflow: hidden;
}

Then use the .toggleClass method to toggle the class in <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 which 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><divid="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="https://pt.stackoverflow.com/"></iframe></div></div></div></div><pclass="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>
    
17.01.2018 / 22:10
1

If you are using the bootstrap, as it seems to be, add to that class

body.modal-open {
    overflow: hidden;
}
    
17.01.2018 / 19:31
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' });
    })

Font

    
17.01.2018 / 19:33