用于更改模态背景颜色/不透明度的特定Bootstrap模式

时间:2016-12-15 14:50:40

标签: css twitter-bootstrap modal-dialog bootstrap-modal

我遇到了一个问题,我无法编辑特定模态的样式,但仅适用于使用下面的CSS的所有模式:

.modal-backdrop {background:red;}

但是我能做些什么来改变第二个模态的造型,而不是所有的模态?

Codepen:http://codepen.io/rae0724/pen/woQpqv

2 个答案:

答案 0 :(得分:0)

你可以使用jQuery& Bootstrap's Modal Events

  • show.bs.modal - 模态打开后立即触发。
  • hidden.bs.modal - 模式完全关闭后立即触发。

使用此功能,您会10ms延迟并更改background-color的{​​{1}}。像:

backdrop

查看下面的工作代码段:

// Triggers as soon as 'modal1' Opens
$('.modal1').on('show.bs.modal', function() {
  setTimeout(function() {
    $('.modal-backdrop').css('background', 'red');
  }, 10);
});

// Triggers as soon as 'modal1' is closed
$('.modal1').on('hidden.bs.modal', function() {
  $('.modal-backdrop').css('background', '');
});


// Triggers as soon as 'modal2' Opens
$('.modal2').on('show.bs.modal', function() {
  setTimeout(function() {
    $('.modal-backdrop').css('background', 'blue');
  }, 10);
});

// Triggers as soon as 'modal2' is closed
$('.modal2').on('hidden.bs.modal', function() {
  $('.modal-backdrop').css('background', '');
});
$('.modal1').on('show.bs.modal', function() {
  setTimeout(function() {
    $('.modal-backdrop').css('background', 'red');
  }, 10);
});

$('.modal1').on('hidden.bs.modal', function() {
  $('.modal-backdrop').css('background', '');
});

$('.modal2').on('show.bs.modal', function() {
  setTimeout(function() {
    $('.modal-backdrop').css('background', 'blue');
  }, 10);
});

$('.modal2').on('hidden.bs.modal', function() {
  $('.modal-backdrop').css('background', '');
});
.wrap { padding: 15px; }
h1 { font-size: 28px; }
h4,
modal-title { font-size: 18px; font-weight: bold; }

.no-borders { border: 0px; }
.body-message { font-size: 18px; }
.centered { text-align: center; }
.btn-primary { background-color: #2086c1; border-color: transparent; outline: none; border-radius: 8px; font-size: 15px; padding: 10px 25px; }
.btn-primary:hover { background-color: #2086c1; border-color: transparent; }
.btn-primary:focus { outline: none; }

希望这有帮助!

答案 1 :(得分:0)

鉴于以下解决方案是您的要求的补丁。这是我想出的最好的事情。我刚刚在你的代码中添加了java脚本。我没有改变你的代码单词。

$(".modal1").on('shown.bs.modal', function() {
  $('.modal-backdrop').css('background', 'red');
});

$(".modal1").on('hidden.bs.modal', function() {
  $('.modal-backdrop').css('background', '#000');
});
.wrap {
  padding: 15px;
}
h1 {
  font-size: 28px;
}
h4,
modal-title {
  font-size: 18px;
  font-weight: bold;
}
.no-borders {
  border: 0px;
}
.body-message {
  font-size: 18px;
}
.centered {
  text-align: center;
}
.btn-primary {
  background-color: #2086c1;
  border-color: transparent;
  outline: none;
  border-radius: 8px;
  font-size: 15px;
  padding: 10px 25px;
}
.btn-primary:hover {
  background-color: #2086c1;
  border-color: transparent;
}
.btn-primary:focus {
  outline: none;
}
.model1 .modal-backdrop {
  background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="wrap">
  <h1>Bootstrap Modal Example</h1>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal1">
    Modal 1
  </button>
</div>

<div class="modal fade modal1" tabindex="-1" role="dialog" aria-labelledby="modal1" aria-hidden="true">

  <div class="modal-dialog modal-lg">

    <!-- Modal Content: begins -->
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header no-borders">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="gridSystemModalLabel"></h4>
      </div>

      <!-- Modal Body -->
      <div class="modal-body">
        <p class="body-message centered"><strong>Modal 1 here.</strong>
        </p>
      </div>

    </div>
    <!-- Modal Content: ends -->

  </div>

</div>

<!---------------------->
<div class="wrap">
  <h1>Bootstrap Modal Example</h1>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal2">
    Modal 2
  </button>
</div>

<div class="modal fade modal2" tabindex="-1" role="dialog" aria-labelledby="modal2" aria-hidden="true">

  <div class="modal-dialog modal-lg">

    <!-- Modal Content: begins -->
    <div class="modal-content">

      <!-- Modal Body -->
      <div class="modal-body">
        <div class="body-message">
          <h4>Modal 2 here.</h4>
          <p>How to change this background colour?</p>
        </div>
      </div>

    </div>
    <!-- Modal Content: ends -->

  </div>

</div>