what does bootstrap use to make backdrop passive

╄→гoц情女王★ 提交于 2019-12-11 08:56:24

问题


I am trying to create a Bootstrap-like modal using only CSS and jQuery, without Bootstrap. But after launching the modal, the backdrop is still active. What can I do to make it passive so that while scrolling, only the modal content scrolls, and if one clicks outside the modal window, it gets closed? Please share your suggestions. Below is my code snippet.

$(document).ready(function() {
	$(".modal").hide();
	let flag = 0;//hidden
	$("#launch-button").click(function(){
		$(".modal").slideDown(200, function() {
			$(".bg-body").click(function(){
			$(".modal").slideUp(200);
			flag = 0;//hidden
			$(".bg-body").css("opacity","initial");
	});
		});
		flag = 1;//visible
		$(".bg-body").css("opacity","0.3");
	});
	$("#save-btn").addClass("close-button")
	$(".close-button").click(function(){
		$(".modal").slideUp(200);
		flag = 0;//hidden
		$(".bg-body").css("opacity","initial");
	});
	
});
* {
	box-sizing: border-box;
	font-family: Lato, sans-serif;
}

.overlay{
	/*background-color: #000;
	opacity: 0.1;*/
	/*display: flex;
	justify-content: center;
	align-items: center;*/
}

.modal{
	width: 34%;
	position:fixed;
	left: 33vw;
	border: 1px solid #b0b0b0;
	border-radius: 5px;
	padding:2vh 2vw;
}

.modal-header{
	display: flex;
	align-items: center;
}

#close-icon{
	margin-left: auto;
}

.modal-body{
	margin: 0;
	padding-top: 2vh;
	padding-bottom: 2vh;
}

.modal-footer{
	display:flex;
	align-items: center;
	justify-content: flex-end;
}
#close-button{
	margin:1vh 0.8vw;
}

button{
	font-size: 1rem;
	background-color: transparent;
	border: 1px solid #000;
	border-radius: 5px;
	padding: 1vh 0.8vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta  name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
	<link rel="stylesheet" type="text/css" href="index.css">
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
	<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
	<title>modal</title>
</head>
<body>
	<div class="bg-body">
		<p>
			Click here to launch a sample modal made without using Bootstrap. 
		</p>
		<div id="launch">
			<button id="launch-button">
				Launch Sample Modal
			</button>
		</div>
	</div>
	<div class="overlay">
		<div class="modal">
			<div class="modal-header">
				<h3 class="">Modal Title</h3>
				<button class="close-button" id="close-icon"><i class="fas fa-times"></i></button>
			</div>
			<hr>
			<div class="modal-body">
				Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
				tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
				quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
				consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
				cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
				proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
			</div>
			<hr>
			<div class="modal-footer">
				<button class="close-button" id="close-button">Close</button>
				<button id="save-btn">Save changes</button>
			</div>
		</div>
	</div>



	<script src="jquery-3.3.1.min.js"></script>
	<script src="index.js"></script>
</body>
</html>

回答1:


It usually makes the overlay cover the screen:

$(document).ready(function() {
  var $overlay = $('.overlay'),
    $modal = $(".modal"); // put these in vars as you use them multiple times so it will be better performance

  $("#launch-button").on('click', function() {
    // show overlay and modal
    $overlay.show();
    $modal.slideDown(200);
  });

  // hide overlay and modal when overlay or close buttons clicked
  $overlay.on('click', hideModal);
  $('.close-button').on('click', hideModal);

  $modal.on('click', function(e) {
    // stop click events on modal bubbling up to overlay (stops the overlay click event firing when modal is clicked on)
    e.stopPropagation();
  });

  function hideModal() {
    // hide modal and overlay
    $modal.slideUp(200, function() {
      $overlay.hide();
    });
  }
});
* {
  box-sizing: border-box;
  font-family: Lato, sans-serif;
}

.overlay {
  /* make overlay cover whole body */
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  overflow: auto;
}

.modal {
  /* start off hidden with css so it doesn't flash when loading */
  display: none;
  width: 34%;
  border: 1px solid #b0b0b0;
  border-radius: 5px;
  padding: 2vh 2vw;
  background: white;
  /* centre modal may need a media query to remove transform on smaller screen heights */
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%, -50%);
}

.modal-header {
  display: flex;
  align-items: center;
}

#close-icon {
  margin-left: auto;
}

.modal-body {
  margin: 0;
  padding-top: 2vh;
  padding-bottom: 2vh;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

#close-button {
  margin: 1vh 0.8vw;
}

button {
  font-size: 1rem;
  background-color: transparent;
  border: 1px solid #000;
  border-radius: 5px;
  padding: 1vh 0.8vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  <link rel="stylesheet" type="text/css" href="index.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
  <title>modal</title>
</head>

<body>
  <div class="bg-body">
    <p>
      Click here to launch a sample modal made without using Bootstrap.
    </p>
    <div id="launch">
      <button id="launch-button">
				Launch Sample Modal
			</button>
    </div>
  </div>
  <div class="overlay">
    <div class="modal">
      <div class="modal-header">
        <h3 class="">Modal Title</h3>
        <button class="close-button" id="close-icon"><i class="fas fa-times"></i></button>
      </div>
      <hr>
      <div class="modal-body">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
        in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </div>
      <hr>
      <div class="modal-footer">
        <button class="close-button" id="close-button">Close</button>
        <button id="save-btn">Save changes</button>
      </div>
    </div>
  </div>



  <script src="jquery-3.3.1.min.js"></script>
  <script src="index.js"></script>
</body>

</html>


来源:https://stackoverflow.com/questions/52383021/what-does-bootstrap-use-to-make-backdrop-passive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!