如何在我的bootstrap模式后面设置模糊窗口?

时间:2017-10-05 16:58:54

标签: javascript css background-image

我认为我应该使用JavaScript来截取屏幕截图,然后将其模糊并将其设置为布局背景图像,但是如何? 你能给我一个Code-Snippet吗?



var thecanvas = undefined;
  function make(){
  html2canvas(document.body, {
    onrendered: function(canvas) {
      var ctx = canvas.getContext('2d');
      ctx.filter = 'blur(3px)';
      ctx.drawImage(canvas, 0, 0);
      document.body.appendChild(canvas);
      thecanvas = canvas;
      canvas.id = 'bluredbg';
    }
  });
  }
  function distroy(){
    if (thecanvas){
      var el = thecanvas;
      el.parentNode.removeChild( el );
      thecanvas = undefined;
    }
  }

  $( "#myModal" ).on('show.bs.modal', function(){
      make();
  }).on('hide.bs.modal', function(){
      distroy();
  });

#bluredbg{
  position:fixed;
  z-index:2;
  top:0;
  left:0;
}
.modal-dialog{
  z-index:3;
  width: 200px !important;
  margin: 0 auto !important;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<p>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
<br>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</p>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

我添加了一些代码来展示我对JavaScript canvas blur作为背景的意思。它的工作好我觉得。我不知道浏览器是否更好地支持此解决方案。

1 个答案:

答案 0 :(得分:1)

您必须在模态打开时添加css才能模糊屏幕

body.modal-open .background-container{
    -webkit-filter: blur(4px);
    -moz-filter: blur(4px);
    -o-filter: blur(4px);
    -ms-filter: blur(4px);
     filter: blur(4px);
 }

&#13;
&#13;
body.modal-open .background-container{
    -webkit-filter: blur(4px);
    -moz-filter: blur(4px);
    -o-filter: blur(4px);
    -ms-filter: blur(4px);
     filter: blur(4px);
 }
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
	<div class="row background-container">
        <div class="col-md-12">
        <br><br>
        <h2> https://stackoverflow.com/questions/46591095/how-can-i-set-a-blur-window-behind-my-bootstrap-modal </h2>
        
        <br><br>
   <div class="alert alert-success">
    <strong>Success!</strong> This alert box could indicate a successful or positive action.
  </div>
  <div class="alert alert-info">
    <strong>Info!</strong> This alert box could indicate a neutral informative change or action.
  </div>
  <div class="alert alert-warning">
    <strong>Warning!</strong> This alert box could indicate a warning that might need attention.
  </div>
  <div class="alert alert-danger">
    <strong>Danger!</strong> This alert box could indicate a dangerous or potentially negative action.
  </div>
        
        <br><br>
            <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Your Modal</button>
        </div>
	</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" id="myModalLabel">Bootstrap Modal</h4>
      </div>
      <div class="modal-body">
       
        <h3>WelCome to Stack Overflow </h3>
            <a href="https://stackoverflow.com/questions/46591095/how-can-i-set-a-blur-window-behind-my-bootstrap-modal" target="_blank"> </a>
        </p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;