屏幕+叠加中间的中心图像

时间:2017-04-24 07:57:53

标签: javascript jquery html css

当您点击图像时,它会显示在灯箱中。现在叠加中的图像放在屏幕顶部,但最好的解决方案是放在用户屏幕的中间。

代码如下。

// Create a lightbox
(function() {
  var $lightbox = $("<div class='lightbox'></div>");
  var $img = $("<img>");
  var $caption = $("<p class='caption'></p>");

  // Add image and caption to lightbox

  $lightbox
    .append($img)
    .append($caption);

  // Add lighbox to document

  $('body').append($lightbox);

  $('.lightbox-gallery img').click(function(e) {
    e.preventDefault();

    // Get image link and description
    var src = $(this).attr("src");
    var cap = $(this).attr("alt");
    var txt = $(this).parent().find('.js-text').text()

    // Add data to lighbox

    $img.attr('src', src);
    $caption.text(txt);

    // Show lightbox

    $lightbox.fadeIn('fast');

    $lightbox.click(function() {
      $lightbox.fadeOut('fast');
    });
  });

}());
body{
	background: linear-gradient(to bottom right, #b81d1d, #5F554C, #E4DFD8);
	font-family: "Open Sans", sans-serif;
}
.text {
  display: none;
}
.container{
	max-width: 800px;
	margin: 5% auto;
	padding: 20px;
	background-color: #fff;
	overflow: hidden;
	box-sizing: border-box;
  box-shadow: 0 15px 20px -15px rgba(0, 0, 0, 0.3), 0 35px 50px -25px rgba(0, 0, 0, 0.3), 0 85px 60px -25px rgba(0, 0, 0, 0.1);
}

.text-center{
	text-align: center;
	margin-bottom: 1em;
}

.lightbox-gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.lightbox-gallery div > img {
  max-width: 100%;
  display: block;
}

.lightbox-gallery div {
  margin: 10px;
  flex-basis: 180px;
}

@media only screen and (max-width: 480px) {
  .lightbox-gallery {
    flex-direction: column;
    align-items: center;
  }

  .lightbox > div {
    margin-bottom: 10px;
  }
}


/*Lighbox CSS*/


.lightbox{
	display: none;
	width: 100%;
	height: 100%;
	background-color: rgba(0,0,0,.7);
	position: fixed;
	top: 0;
	left: 0;
	z-index: 20;
	padding-top: 30px;
	box-sizing: border-box;
}

.lightbox img{
	display: block;
  margin: auto;
}

.lightbox .caption{
	margin: 15px auto;
	width: 50%;
	text-align: center;
	font-size: 1em;
	line-height: 1.5;
	font-weight: 700;
	color: #eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
		<h2 class="text-center">Lightbox Gallery</h2>
		<div class="lightbox-gallery">
				<div>
          <img src="http://placehold.it/300/f1b702/fff&text=image1" alt="">
          <div class="js-text text">Test 1</div>
      </div>
				<div><img src="http://placehold.it/300/d2f1b2/222&text=image2" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime accusamus officiis dignissimos doloribus consectetur harum eos sapiente optio aut minima."><div class="js-text text">Test 2</div></div>
				<div><img src="http://placehold.it/300/eee/000&text=image3" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates veritatis similique, amet, maiores soluta recusandae cupiditate, sed perspiciatis fugit minima, sunt dolores cum earum deserunt illo ipsum!"><div class="js-text text">Test 3</div></div>
				<div><img src="http://placehold.it/300/222/fff&text=image4" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque laudantium dignissimos tenetur eos unde quidem repellat officiis nemo laboriosam necessitatibus deleniti commodi quis aliquid est atque tempora aut, nihil!"><div class="js-text text">Test</div></div>
				<div><img src="http://placehold.it/300/b47f99/000&text=image5" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto minus consequatur soluta quaerat itaque, laboriosam quis a facilis, cumque, deleniti quas aperiam voluptate dolore. Enim nostrum sit eaque, porro eligendi illo placeat?"><div class="js-text text">Test</div></div>
				<div><img src="http://placehold.it/300/e1d400/000&text=image6" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi suscipit quam, id aliquam totam aperiam quas rem debitis voluptatem pariatur, illo accusamus facilis eius ipsa! Reprehenderit libero, quas iste repudiandae distinctio, quos dignissimos."><div class="js-text text">Test</div></div>
		</div>
	</div>

这是codepen:

Codepen网址:https://codepen.io/jonathandion/pen/EmPbvb

2 个答案:

答案 0 :(得分:2)

以下是flexbox解决方案:

codepen

&#13;
&#13;
router.get('/', customers.list);
&#13;
// Create a lightbox
(function() {
  var $lightbox = $("<div class='lightbox'></div>");
  var $img = $("<img>");
  var $caption = $("<p class='caption'></p>");

  // Add image and caption to lightbox

  $lightbox
    .append($img)
    .append($caption);

  // Add lighbox to document

  $('body').append($lightbox);

  $('.lightbox-gallery img').click(function(e) {
    e.preventDefault();

    // Get image link and description
    var src = $(this).attr("src");
    var cap = $(this).attr("alt");
    var txt = $(this).parent().find('.js-text').text()

    // Add data to lighbox

    $img.attr('src', src);
    $caption.text(txt);

    // Show lightbox

    $lightbox.fadeIn('fast').css('display', 'flex');

    $lightbox.click(function() {
      $lightbox.fadeOut('fast');
    });
  });

}());
&#13;
body {
  background: linear-gradient(to bottom right, #b81d1d, #5F554C, #E4DFD8);
  font-family: "Open Sans", sans-serif;
}

.text {
  display: none;
}

.container {
  max-width: 800px;
  margin: 5% auto;
  padding: 20px;
  background-color: #fff;
  overflow: hidden;
  box-sizing: border-box;
  box-shadow: 0 15px 20px -15px rgba(0, 0, 0, 0.3), 0 35px 50px -25px rgba(0, 0, 0, 0.3), 0 85px 60px -25px rgba(0, 0, 0, 0.1);
}

.text-center {
  text-align: center;
  margin-bottom: 1em;
}

.lightbox-gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.lightbox-gallery div>img {
  max-width: 100%;
  display: block;
}

.lightbox-gallery div {
  margin: 10px;
  flex-basis: 180px;
}

@media only screen and (max-width: 480px) {
  .lightbox-gallery {
    flex-direction: column;
    align-items: center;
  }
  .lightbox>div {
    margin-bottom: 10px;
  }
}


/*Lighbox CSS*/

.lightbox {
  display: none;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .7);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20;
  padding-top: 30px;
  box-sizing: border-box;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.lightbox img {
  display: block;
}

.lightbox .caption {
  margin: 15px auto;
  width: 50%;
  text-align: center;
  font-size: 1em;
  line-height: 1.5;
  font-weight: 700;
  color: #eee;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <h2 class="text-center">Lightbox Gallery</h2> <div class="lightbox-gallery"> <div> <img src="http://placehold.it/300/f1b702/fff&text=image1" alt=""> <div class="js-text text">Test 1</div> </div> <div><img src="http://placehold.it/300/d2f1b2/222&text=image2" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime accusamus officiis dignissimos doloribus consectetur harum eos sapiente optio aut minima."> <div class="js-text text">Test 2</div> </div> <div><img src="http://placehold.it/300/eee/000&text=image3" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates veritatis similique, amet, maiores soluta recusandae cupiditate, sed perspiciatis fugit minima, sunt dolores cum earum deserunt illo ipsum!"> <div class="js-text text">Test 3</div> </div> <div><img src="http://placehold.it/300/222/fff&text=image4" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque laudantium dignissimos tenetur eos unde quidem repellat officiis nemo laboriosam necessitatibus deleniti commodi quis aliquid est atque tempora aut, nihil!"> <div class="js-text text">Test</div> </div> <div><img src="http://placehold.it/300/b47f99/000&text=image5" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto minus consequatur soluta quaerat itaque, laboriosam quis a facilis, cumque, deleniti quas aperiam voluptate dolore. Enim nostrum sit eaque, porro eligendi illo placeat?"> <div class="js-text text">Test</div> </div> <div><img src="http://placehold.it/300/e1d400/000&text=image6" alt="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi suscipit quam, id aliquam totam aperiam quas rem debitis voluptatem pariatur, illo accusamus facilis eius ipsa! Reprehenderit libero, quas iste repudiandae distinctio, quos dignissimos."> <div class="js-text text">Test</div> </div> </div> </div>添加到padding-bottom: 55px;课程,并将.lightbox添加到height:100%此课程。 希望这对你有用。

  

这是完整的工作示例。

&#13;
&#13;
.lightbox img
&#13;
// Create a lightbox
(function() {
  var $lightbox = $("<div class='lightbox'></div>");
  var $img = $("<img>");
  var $caption = $("<p class='caption'></p>");

  // Add image and caption to lightbox

  $lightbox
    .append($img)
    .append($caption);

  // Add lighbox to document

  $('body').append($lightbox);

  $('.lightbox-gallery img').click(function(e) {
    e.preventDefault();

    // Get image link and description
    var src = $(this).attr("src");
    var cap = $(this).attr("alt");
    var txt = $(this).parent().find('.js-text').text()

    // Add data to lighbox

    $img.attr('src', src);
    $caption.text(txt);

    // Show lightbox

    $lightbox.fadeIn('fast');

    $lightbox.click(function() {
      $lightbox.fadeOut('fast');
    });
  });

}(jQuery));
&#13;
body{
	background: linear-gradient(to bottom right, #b81d1d, #5F554C, #E4DFD8);
	font-family: "Open Sans", sans-serif;
}
.text {
  display: none;
}
.container{
	max-width: 800px;
	margin: 5% auto;
	padding: 20px;
	background-color: #fff;
	overflow: hidden;
	box-sizing: border-box;
  box-shadow: 0 15px 20px -15px rgba(0, 0, 0, 0.3), 0 35px 50px -25px rgba(0, 0, 0, 0.3), 0 85px 60px -25px rgba(0, 0, 0, 0.1);
}

.text-center{
	text-align: center;
	margin-bottom: 1em;
}

.lightbox-gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.lightbox-gallery div > img {
  max-width: 100%;
  display: block;
}

.lightbox-gallery div {
  margin: 10px;
  flex-basis: 180px;
}

@media only screen and (max-width: 480px) {
  .lightbox-gallery {
    flex-direction: column;
    align-items: center;
  }

  .lightbox > div {
    margin-bottom: 10px;
  }
}


/*Lighbox CSS*/


.lightbox{
	 background-color: rgba(0, 0, 0, 0.7);
    bottom: 0;
    box-sizing: border-box;
    display: none;
    height: 100%;
    left: 0;
    padding-bottom: 55px;
    padding-top: 30px;
    position: fixed;
    right: 0;
    top: 0;
    width: 100%;
    z-index: 20;
}

.lightbox img{
	display: block;
  margin: auto;
  height:100%;
}

.lightbox .caption{
	margin: 15px auto;
	width: 50%;
	text-align: center;
	font-size: 1em;
	line-height: 1.5;
	font-weight: 700;
	color: #eee;
}
&#13;
&#13;
&#13;