【问题标题】:I want To show Image Popup [duplicate]我想显示图像弹出窗口[重复]
【发布时间】:2020-04-20 13:04:16
【问题描述】:

我有一个非常静态的登录页面。但是在这个页面中有一个按钮,当有人点击它时。它应该打开一个弹出图像,非常简单的弹出图像。这会发生吗? html的代码是什么? 请指导

【问题讨论】:

标签: html image button popup


【解决方案1】:

Code from W3Schools

var modal = document.getElementById("myModal");

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = "https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded";
  captionText.innerHTML = "StackOverflow";
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}
/* Style the Image Used to Trigger the Modal */
#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {transform:scale(0)}
  to {transform:scale(1)}
}

/* The Close Button */
.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}
<button id="myImg"  style="width:100%;max-width:300px">Click Me</button>


<div id="myModal" class="modal">

 
  <span class="close">&times;</span>

  
  <img class="modal-content" id="img01" src="https://www.debuggr.io/static/3a4bfeeaa7a69e6aa9f4ba6ae4218622/af144/cover.png">

 
  <div id="caption"></div>
</div>

【讨论】:

  • 当答案有效时,我们也可以投票:) 不要亲自投票。给出它们的原因有很多。有些是当人们不喜欢你给代码的缩进数量时给出的,有些是因为不是第一个回答而生气。给 SO 一个机会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2021-11-01
  • 2019-09-10
  • 2021-04-26
  • 1970-01-01
相关资源
最近更新 更多