0

El problema es que aparecen 2 scrollbars, uno del modal y otro del body, quiero quitar el del body cuando el modal este activo.

Este es mi codigo:

<div id="myModal" class="modal">
</div>


.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow:auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */

}

<script type="text/javascript">
  // Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

2 respuestas 2

0

Al momento de mostrar la modal, oculta el scroll del body utilizando:

  document.getElementsByTagName("body")[0].style.overflow = "visible";

Y en tu codigo tendrias algo asi:

// When the user clicks on the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
    document.getElementsByTagName("body")[0].style.overflow = "hidden";

}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
    document.getElementsByTagName("body")[0].style.overflow = "visible";
}
5
  • ese no es el problema, el problema es q aparecen 2 scrollbars, uno del modal y otro del body, quiero quitar el del body cuando el modal este activo
    – deop
    el 6 sep. 2018 a las 17:43
  • Y porque no especificaste eso en tu pregunta?
    – Einer
    el 6 sep. 2018 a las 17:44
  • puedes ayudarme?
    – deop
    el 6 sep. 2018 a las 17:47
  • Acabo de editar la respuesta, dale una mirada.
    – Einer
    el 6 sep. 2018 a las 17:49
  • Muchisimas gracias! me funciono totalmente! puedes decirme donde aprendiste js?
    – deop
    el 6 sep. 2018 a las 18:08
0

Puedes hacerlo directamente en el CSS. Saludos.

<style type="text/css">
body {
    overflow:hidden;
}
</style>

<style type="text/css">
body {
    overflow-y:hidden;
}
</style>

<style type="text/css">
body {
    overflow-x:hidden;
}
</style>

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.