首页 > 解决方案 > 我正在尝试打开一个具有某种效果的模态窗口

问题描述

openModal()函数在单击函数时被调用。

我在 Google 上检查了过渡属性不适用于 CSS 中的显示属性。另一种方法是用不透明度来做。但我无法从 JavaScript 调用动画以获取 CSS 属性。

    var whm, whs, whd, whc;
function openModal(a) {
        whm="myModal"+a;
        whs="mySlides"+a;
        whd="demo"+a;
        whc="caption"+a;

  document.getElementById(whm).style.display = "block";
  document.getElementById(whm).style.opacity = "1";
 // document.getElementById(whm).classList.add('didLoad');
 //document.getElementById(whm).className += " didLoad";

  document.getElementById(whm).style.webkitTransition = "background 2s";
}
.modal {
  display: none;
  opacity: 0;
  -webkit-transition: opacity 2s;
  transition: opacity 2s;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;

整个代码

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Verdana, sans-serif;
  margin: 0;
}

* {
  box-sizing: border-box;
}

.row > .column {
  padding: 0 8px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.column {
  float: left;
  width: 25%;
}

/* The Modal (background) */
.modal {
  display: none;
  opacity: 0;
  -webkit-transition: opacity 2s;
  transition: opacity 2s;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: black;
}

.didLoad
{
	background-color: black;
	transition: background-color 2s;
 
	-webkit-transition: background-color 2s;
  
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  width: 90%;
  max-width: 1200px;
}

/* The Close Button */
.close {
  color: white;
  position: absolute;
  top: 10px;
  right: 25px;
  font-size: 35px;
  font-weight: bold;
}

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

.mySlides {
  display: none;
}

.cursor {
  cursor: pointer;
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

img {
  margin-bottom: -4px;
}

.caption-container {
  text-align: center;
  background-color: black;
  padding: 2px 16px;
  color: white;
}

.demo {
  opacity: 0.6;
}

.active,
.demo:hover {
  opacity: 1;
}

img.hover-shadow {
  transition: 0.3s;
}

.hover-shadow:hover {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
</style>
<body>

<h2 style="text-align:center">Lightbox</h2>

<div class="row">
  <div class="column">
    <img src="http://placehold.it/1900x1400" style="width:100%" onclick="openModal(1);currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placehold.it/1900x1400" style="width:100%" onclick="openModal(2);currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placehold.it/1900x1400" style="width:100%" onclick="openModal(3);currentSlide(1)" class="hover-shadow cursor">
  </div>
  <div class="column">
    <img src="http://placehold.it/1900x1400" style="width:100%" onclick="openModal(4);currentSlide(1)" class="hover-shadow cursor">
  </div>
</div>

<div id="myModal1" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides1">
      <div class="numbertext">1 / 4</div>
      <img src="http://placehold.it/720x301" style="width:100%">
    </div>

    <div class="mySlides1">
      <div class="numbertext">2 / 4</div>
      <img src="http://placehold.it/720x302" style="width:100%">
    </div>

    <div class="mySlides1">
      <div class="numbertext">3 / 4</div>
      <img src="http://placehold.it/720x303" style="width:100%">
    </div>
    
    <div class="mySlides1">
      <div class="numbertext">4 / 4</div>
      <img src="http://placehold.it/720x304" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption1"></p>
    </div>


    <div class="column">
      <img class="demo1 cursor" src="http://placehold.it/1900x1401" style="width:100%; height:100px;" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo1 cursor" src="http://placehold.it/1900x1402" style="width:100%" onclick="currentSlide(2)" alt="Snow">
    </div>
    <div class="column">
      <img class="demo1 cursor" src="http://placehold.it/1900x1403" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo1 cursor" src="http://placehold.it/1900x1404" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>

<div id="myModal2" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides2">
      <div class="numbertext">1 / 4</div>
      <img src="http://placehold.it/720x201" style="width:100%">
    </div>

    <div class="mySlides2">
      <div class="numbertext">2 / 4</div>
      <img src="http://placehold.it/720x202" style="width:100%">
    </div>

    <div class="mySlides2">
      <div class="numbertext">3 / 4</div>
      <img src="http://placehold.it/720x203" style="width:100%">
    </div>
    
    <div class="mySlides2">
      <div class="numbertext">4 / 4</div>
      <img src="http://placehold.it/720x204" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption2"></p>
    </div>


    <div class="column">
      <img class="demo2 cursor" src="http://placehold.it/1900x1301" style="width:100%; height:100px;" onclick="currentSlide(1)" alt="bhai">
    </div>
    <div class="column">
      <img class="demo2 cursor" src="http://placehold.it/1900x1302" style="width:100%" onclick="currentSlide(2)" alt="bhai bhai">
    </div>
    <div class="column">
      <img class="demo2 cursor" src="http://placehold.it/1900x1303" style="width:100%" onclick="currentSlide(3)" alt="bhai bhai bhai">
    </div>
    <div class="column">
      <img class="demo2 cursor" src="http://placehold.it/1900x1304" style="width:100%" onclick="currentSlide(4)" alt="fuck off">
    </div>
  </div>
</div>

<div id="myModal3" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides3">
      <div class="numbertext">1 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>

    <div class="mySlides3">
      <div class="numbertext">2 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>

    <div class="mySlides3">
      <div class="numbertext">3 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>
    
    <div class="mySlides3">
      <div class="numbertext">4 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption3"></p>
    </div>


    <div class="column">
      <img class="demo3 cursor" src="http://placehold.it/1900x1400" style="width:100%; height:100px;" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo3 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(2)" alt="Snow">
    </div>
    <div class="column">
      <img class="demo3 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo3 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>

<div id="myModal4" class="modal">
  <span class="close cursor" onclick="closeModal()">&times;</span>
  <div class="modal-content">

    <div class="mySlides4">
      <div class="numbertext">1 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>

    <div class="mySlides4">
      <div class="numbertext">2 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>

    <div class="mySlides4">
      <div class="numbertext">3 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>
    
    <div class="mySlides4">
      <div class="numbertext">4 / 4</div>
      <img src="http://placehold.it/720x300" style="width:100%">
    </div>
    
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

    <div class="caption-container">
      <p id="caption3"></p>
    </div>


    <div class="column">
      <img class="demo4 cursor" src="http://placehold.it/1900x1400" style="width:100%; height:100px;" onclick="currentSlide(1)" alt="Nature and sunrise">
    </div>
    <div class="column">
      <img class="demo4 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(2)" alt="Snow">
    </div>
    <div class="column">
      <img class="demo4 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
    </div>
    <div class="column">
      <img class="demo4 cursor" src="http://placehold.it/1900x1400" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
    </div>
  </div>
</div>


<script>
	var whm, whs, whd, whc;
function openModal(a) {
		whm="myModal"+a;
	    whs="mySlides"+a;
		whd="demo"+a;
		whc="caption"+a;
  
  document.getElementById(whm).style.display = "block";
  document.getElementById(whm).style.opacity = "1";
 // document.getElementById(whm).classList.add('didLoad');
 //document.getElementById(whm).className += " didLoad";
 
  document.getElementById(whm).style.webkitTransition = "background 2s";

  

}

function closeModal() {
  document.getElementById(whm).style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName(whs);
  var dots = document.getElementsByClassName(whd);
  var captionText = document.getElementById(whc);
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
  captionText.innerHTML = dots[slideIndex-1].alt;
}
</script>
    
</body>
</html>

标签: javascripthtmlcss

解决方案


过渡不适display用于新class动画和 CSS 动画,您可以做一些不错的事情(我只为“show”设置动画):

var show = document.getElementById('show');
var hide = document.getElementById('hide');

show.addEventListener("click", function() {
  document.getElementById('hello').classList.add("active");
});

hide.addEventListener("click", function() {
  document.getElementById('hello').classList.remove("active");
});
#content {
  position: relative;
  text-align: center;
}

#hello {
  position: absolute;
  padding: 10px;
  background-color: yellow;
  text-align: center;
  left: 0;
  right: 0;
  display: none;
  opacity: 0;
}

#hello.active {
  display: block;
  opacity: 1;
  animation: fadeIn .4s;
}

@keyframes fadeIn {
  0% { opacity: 0; }
  100% { opacity: 1; }
}
<div id="content">
  <div id="hello">
    <p>Hello world !</p>
    <button id="hide">Hide</button>
  </div>
  <button id="show">Show</button>
</div>


推荐阅读