引导程序:$('#myModal')。modal('show');使屏幕变黑

时间:2019-01-03 10:35:50

标签: javascript jquery twitter-bootstrap bootstrap-modal

在我的身体标签的结尾,我有一个模态:

<div class="portfolio-modal mfp-hide" id="myModal">
  <div class="portfolio-modal-dialog bg-white">
    <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
    </a>
    <div class="container text-center">
      <div class="row">
        <div class="col-lg-8 mx-auto">
          <hr class="star-dark mb-5">
        </div>
      </div>
    </div>
  </div>
</div>

在body标签的开头,我有一个脚本。在此脚本中,我试图在单击Google Maps标记时显示一种模式。但是,当我单击它时,我的网站崩溃了,屏幕呈暗黑色,没有任何模态显示。

window.google.maps.event.addListener(marker, 'click', function () {
               // $("#myModal").removeClass("in");
               //$("#myModa").remove();
               //$('body').removeClass('modal-open');
               // $('body').css('padding-right', '');
               // $('#myModal').modal('show');
               // $('.myModal').modal('show');

                $('#myModal').modal('show');

            });

有趣的是,通过此处的href访问时,我的模式显示得很好:

<div class="col-md-3">
        <a class="portfolio-item d-block mx-auto" href="#myModal">
          <div class="portfolio-item-caption d-flex position-absolute h-100 w-100">
          </div>
        </a>
      </div>

这必须是Bootstrap Freelancer模板中的某种错误。我读过其他文章,说其他人通过编辑.css文件解决了类似问题。

这是我的bootstrap.css代码

    .modal-open {
  overflow: hidden;
}

.modal-open .modal {
  overflow-x: hidden;
  overflow-y: auto;
}

.modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1050;
  display: none;
  overflow: hidden;
  outline: 0;
}

.modal-dialog {
  position: relative;
  width: auto;
  margin: 0.5rem;
  pointer-events: none;
}

.modal.fade .modal-dialog {
  transition: -webkit-transform 0.3s ease-out;
  transition: transform 0.3s ease-out;
  transition: transform 0.3s ease-out, -webkit-transform 0.3s ease-out;
  -webkit-transform: translate(0, -25%);
  transform: translate(0, -25%);
}

@media screen and (prefers-reduced-motion: reduce) {
  .modal.fade .modal-dialog {
    transition: none;
  }
}

.modal.show .modal-dialog {
  -webkit-transform: translate(0, 0);
  transform: translate(0, 0);
}

.modal-dialog-centered {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  min-height: calc(100% - (0.5rem * 2));
}

.modal-dialog-centered::before {
  display: block;
  height: calc(100vh - (0.5rem * 2));
  content: "";
}

.modal-content {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  width: 100%;
  pointer-events: auto;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 0.3rem;
  outline: 0;
}

.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1040;
  background-color: #000;
}

.modal-backdrop.fade {
  opacity: 0;
}

.modal-backdrop.show {
  opacity: 0.5;
}

.modal-header {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: start;
  align-items: flex-start;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 1rem;
  border-bottom: 1px solid #e9ecef;
  border-top-left-radius: 0.3rem;
  border-top-right-radius: 0.3rem;
}

.modal-header .close {
  padding: 1rem;
  margin: -1rem -1rem -1rem auto;
}

.modal-title {
  margin-bottom: 0;
  line-height: 1.5;
}

    .modal-body {
     position: relative;
     -ms-flex: 1 1 auto;
     flex: 1 1 auto;
     padding: 1rem;
    }

    .modal-footer {
      display: -ms-flexbox;
      display: flex;
     -ms-flex-align: center;
      align-items: center;
     -ms-flex-pack: end;
     justify-content: flex-end;
     padding: 1rem;
     border-top: 1px solid #e9ecef;
    }

     .modal-footer > :not(:first-child) {
     margin-left: .25rem;
    }

      .modal-footer > :not(:last-child) {
      margin-right: .25rem;
     }

      .modal-scrollbar-measure {
      position: absolute;
      top: -9999px;
      width: 50px;
      height: 50px;
      overflow: scroll;
    }

     @media (min-width: 576px) {
        .modal-dialog {
        max-width: 500px;
        margin: 1.75rem auto;
     }
       .modal-dialog-centered {
        min-height: calc(100% - (1.75rem * 2));
     }
       .modal-dialog-centered::before {
       height: calc(100vh - (1.75rem * 2));
     }
      .modal-sm {
      max-width: 300px;
     }

1 个答案:

答案 0 :(得分:2)

尝试这个。背景是主要问题。所以我将其删除了

// $("#myModal").removeClass("in");
               //$("#myModa").remove();
               //$('body').removeClass('modal-open');
               // $('body').css('padding-right', '');
               // $('#myModal').modal('show');
               // $('.myModal').modal('show');

               $('#myModal').modal('show');
               $(".modal-backdrop").remove();
.modal-open {
  overflow: hidden;
}

.modal-open .modal {
  overflow-x: hidden;
  overflow-y: auto;
}

.modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1050;
  display: none;
  overflow: hidden;
  outline: 0;
}

.modal-dialog {
  position: relative;
  width: auto;
  margin: 0.5rem;
  pointer-events: none;
}

.modal.fade .modal-dialog {
  transition: -webkit-transform 0.3s ease-out;
  transition: transform 0.3s ease-out;
  transition: transform 0.3s ease-out, -webkit-transform 0.3s ease-out;
  -webkit-transform: translate(0, -25%);
  transform: translate(0, -25%);
}

@media screen and (prefers-reduced-motion: reduce) {
  .modal.fade .modal-dialog {
    transition: none;
  }
}

.modal.show .modal-dialog {
  -webkit-transform: translate(0, 0);
  transform: translate(0, 0);
}

.modal-dialog-centered {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  min-height: calc(100% - (0.5rem * 2));
}

.modal-dialog-centered::before {
  display: block;
  height: calc(100vh - (0.5rem * 2));
  content: "";
}

.modal-content {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  width: 100%;
  pointer-events: auto;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 0.3rem;
  outline: 0;
}

.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1040;
  background-color: #000;
}

.modal-backdrop.fade {
  opacity: 0;
}

.modal-backdrop.show {
  opacity: 0.5;
}

.modal-header {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: start;
  align-items: flex-start;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 1rem;
  border-bottom: 1px solid #e9ecef;
  border-top-left-radius: 0.3rem;
  border-top-right-radius: 0.3rem;
}

.modal-header .close {
  padding: 1rem;
  margin: -1rem -1rem -1rem auto;
}

.modal-title {
  margin-bottom: 0;
  line-height: 1.5;
}

    .modal-body {
     position: relative;
     -ms-flex: 1 1 auto;
     flex: 1 1 auto;
     padding: 1rem;
    }

    .modal-footer {
      display: -ms-flexbox;
      display: flex;
     -ms-flex-align: center;
      align-items: center;
     -ms-flex-pack: end;
     justify-content: flex-end;
     padding: 1rem;
     border-top: 1px solid #e9ecef;
    }

     .modal-footer > :not(:first-child) {
     margin-left: .25rem;
    }

      .modal-footer > :not(:last-child) {
      margin-right: .25rem;
     }

      .modal-scrollbar-measure {
      position: absolute;
      top: -9999px;
      width: 50px;
      height: 50px;
      overflow: scroll;
    }

     @media (min-width: 576px) {
        .modal-dialog {
        max-width: 500px;
        margin: 1.75rem auto;
     }
       .modal-dialog-centered {
        min-height: calc(100% - (1.75rem * 2));
     }
       .modal-dialog-centered::before {
       height: calc(100vh - (1.75rem * 2));
     }
      .modal-sm {
      max-width: 300px;
     }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="portfolio-modal mfp-hide modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
  <div class="portfolio-modal-dialog bg-white">
   <div class="modal-header">
    <a class="close-button d-none d-md-block portfolio-modal-dismiss" href="#">
      <i class="fa fa-3x fa-times"></i>
    </a>
    </div>
     <div class="modal-body">
    <div class="container text-center">
      <div class="row">
        <div class="col-lg-8 mx-auto">
        helloooo
          <hr class="star-dark mb-5">
        </div>
      </div>
      </div>
    </div>
    </div>
  </div>
</div>