关闭弹出窗口并打开新窗口

时间:2020-01-16 09:03:17

标签: javascript html css popup

我写了一个代码。我有按钮,每个按钮都会打开一个弹出窗口。在打开之前,我希望我的按钮关闭上一个弹出窗口并打开所需的一个。我正在上课。因此,我想首先使用getElementsByClassName()检查是否有任何可见的弹出窗口,然后将其切换为隐藏。然后使用getElementById()将右边的按钮切换为可见。 我的问题是我不知道如何从我的.show类中检查可见性,如果首先在我的Function2()中,我将按类进行切换,然后在其后不能按ID进行切换。这是为什么?所以我需要使用If(样式在元素i中可见){then popups[i].classList.toggle("show");}之后,我想使用:

var popup = document.getElementById("myPopup2");
  popup.classList.toggle("show");

,但是它不起作用。还是有更好的方法呢?

function myFunction2() {

var popups = document.getElementsByClassName('popuptext');
  for(var i = 0; i < 5; i++)
  { 
 popups[i].classList.toggle("show");
 console.log(1);
 
 }
 var popup = document.getElementById("myPopup2");
  popup.classList.toggle("show");
  console.log(2);
 }
 



function myFunction() {
  var popup = document.getElementById("myPopup");
  popup.classList.toggle("show");
  
}
function myFunction3() {
 var popup = document.getElementById("myPopup3");
  popup.classList.toggle("show");
   
}
function myFunction4() {
  var popup = document.getElementById("myPopup4");
  popup.classList.toggle("show");
}
   

 .popup {
      position: absolute;
      display: inline-block;
      cursor: pointer;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: auto;
      
    
      }
    /* POPUPS--------START------------------------------------------------------------------------------------------------------- */
      .popup .popuptext {
      visibility: hidden;width: 27%; height: 93%;background-color: #fff;
       color: #004;text-align: left; border-radius: 0px;padding: 10px;
      position: fixed ; top: 48%;left: 20%;transform: translate(-50%, -50%);
       margin-left: -80px;border-left: 75px solid white; overflow:auto;border-right: 10px solid white;border-bottom: 35px solid white;
       z-index: 10;border-top: 35px solid white;
      box-shadow: inset 0 0 0 rgba(255,255,255,0.5), 2px 20px 2px rgba(0, 0, 0, 0.19);
    }
    
     /* POPUPS----------------END------------------------------------------------------------------------------------------ */
     
     /* Toggle this class - hide and show the popup */
    .popup .show {
      visibility: visible;
      -webkit-animation: fadeIn 1s;
      animation: fadeIn 1s;
      animation: move 1s;
    }
    
    /* Add animation (fade in the popup) */
    @-webkit-keyframes fadeIn {
      from {opacity: 0;} 
      to {opacity: 1;}
    }
    
    @keyframes fadeIn {
      from {opacity: 0;}
      to {opacity: 1;}
    }
    
    @keyframes move {
      from {left: 0%;}
      to {top:48% ;}
      }
      
      @-webkit-keyframes move {
      from {left: 0%;}
      to {top:48% ;}
      }
    
      /* ------------------------------------------------popup hide and show end*/
    
    body{
      
      font-family:"Trebuchet MS", Helvetica, sans-serif;
      font-size:15px;
      color: #000;
     bgcolor="#fff"
      text-transform:uppercase;
      //overflow-x:hidden;
    }
    
                  h1{
                  font-size:80px;
                  text-align:right;
                  position:bottom;
                  right:340px;
                  top:300px;
                  font-weight:normal;
    			  }
    button{
       border: none; padding: 12px;
       text-align: center;text-decoration: none;  display: inline-block;
        cursor: auto; float: right;
     
      
    }  
    .button1 {padding: 12px;border-radius: 100%;position: absolute;left: 1445px;top: 459px; background-image: -webkit-linear-gradient(#FF0000 40%,  Tomato 100%);
        background-image: linear-gradient(#FF0000 40%,  Tomato 100%);
       }		
    .button2 {padding: 12px;border-radius: 100%;position: absolute;left: 1212px;top: 785px; background-image: -webkit-linear-gradient(#FF0000 40%,  Tomato 100%);
        background-image: linear-gradient(#FF0000 40%,  Tomato 100%);
       } 	
    .button3 {padding: 12px;position: absolute;left: 1412px;top: 785px; border-radius: 100%; background-image: -webkit-linear-gradient(#FF0000 40%,  Tomato 100%);
        background-image: linear-gradient(#FF0000 40%,  Tomato 100%);
       } 	
.button4 {padding: 12px;border-radius: 100%;position: absolute;left: 140px;top: 78px; background-image: -webkit-linear-gradient(#FF0000 40%,  Tomato 100%);
    background-image: linear-gradient(#FF0000 40%,  Tomato 100%);
   } 	
<div class="popup" onclick="myFunction()"><button class="button button1" id="button1"></button>
<span class="popuptext" id="myPopup">text of popup;</span></div>
<div class="popup" onclick="myFunction2()"><button class="button button2" id="button2"></button>
<div class="popuptext" id="myPopup2">
   <p>
    Lubja tn. 4
	
	</p>
<p>
  12.07.2016 
 
 </p>
  <img class="manImg" alt="Pilt"  src="Lubja/1.jpg">
  <img class="manImg" alt="Pilt"  src="Lubja/2.jpg">
  <img class="manImg" alt="Pilt"  src="Lubja/3.jpg">
  <img class="manImg" alt="Pilt"  src="Lubja/4.jpg">
  
</div>
</div>

<!-- Buttons on map start -->
<div class="popup" onclick="myFunction3()"><button class="button button3" id="button3"></button>
<div class="popuptext" id="myPopup3">
   <p>
    Koor
</p>
  Tühi
  <p>
    
</p>
  <img class="manImg" alt="Pilt"  src="Lubja/1.jpg">
  </div>
</div>
<!-- Buttons on map end -->
<!-- Buttons on map start -->
<div class="popup" onclick="myFunction4()"><button class="button button4" id="button4"></button>
<div class="popuptext" id="myPopup4">
   <p>
    Tühi
</p>
  Tühi
  <p>
    
</p>
  <img class="manImg" alt="Pilt"  src="Lubja/1.jpg">
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

我只更新了您的JS和一些HTML。

使代码可重复使用。可重用的代码是好的代码。当您可以使用一个功能完成所有操作时,请勿为每个按钮创建一个功能。

function showPopup(popupID) {
  var popups = document.querySelectorAll(".popuptext");

  for (var i = 0; i < popups.length; i++) {
    popups[i].classList.remove("show");
  }

  var popup = document.getElementById(popupID);
  popup.classList.add("show");
}
.popup {
  position: absolute;
  display: inline-block;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: auto;
}


/* POPUPS--------START------------------------------------------------------------------------------------------------------- */

.popup .popuptext {
  visibility: hidden;
  width: 27%;
  height: 93%;
  background-color: #fff;
  color: #004;
  text-align: left;
  border-radius: 0px;
  padding: 10px;
  position: fixed;
  top: 48%;
  left: 20%;
  transform: translate(-50%, -50%);
  margin-left: -80px;
  border-left: 75px solid white;
  overflow: auto;
  border-right: 10px solid white;
  border-bottom: 35px solid white;
  z-index: 10;
  border-top: 35px solid white;
  box-shadow: inset 0 0 0 rgba(255, 255, 255, 0.5), 2px 20px 2px rgba(0, 0, 0, 0.19);
}


/* POPUPS----------------END------------------------------------------------------------------------------------------ */


/* Toggle this class - hide and show the popup */

.popup .show {
  visibility: visible;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s;
  animation: move 1s;
}


/* Add animation (fade in the popup) */

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes move {
  from {
    left: 0%;
  }
  to {
    top: 48%;
  }
}

@-webkit-keyframes move {
  from {
    left: 0%;
  }
  to {
    top: 48%;
  }
}


/* ------------------------------------------------popup hide and show end*/

body {
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  font-size: 15px;
  color: #000;
  bgcolor="#fff" text-transform: uppercase;
  //overflow-x:hidden;
}

h1 {
  font-size: 80px;
  text-align: right;
  position: bottom;
  right: 340px;
  top: 300px;
  font-weight: normal;
}

button {
  border: none;
  padding: 12px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  cursor: auto;
  float: right;
}

.button1 {
  padding: 12px;
  border-radius: 100%;
  position: absolute;
  left: 1445px;
  top: 459px;
  background-image: -webkit-linear-gradient(#FF0000 40%, Tomato 100%);
  background-image: linear-gradient(#FF0000 40%, Tomato 100%);
}

.button2 {
  padding: 12px;
  border-radius: 100%;
  position: absolute;
  left: 1212px;
  top: 785px;
  background-image: -webkit-linear-gradient(#FF0000 40%, Tomato 100%);
  background-image: linear-gradient(#FF0000 40%, Tomato 100%);
}

.button3 {
  padding: 12px;
  position: absolute;
  left: 1412px;
  top: 785px;
  border-radius: 100%;
  background-image: -webkit-linear-gradient(#FF0000 40%, Tomato 100%);
  background-image: linear-gradient(#FF0000 40%, Tomato 100%);
}

.button4 {
  padding: 12px;
  border-radius: 100%;
  position: absolute;
  left: 140px;
  top: 78px;
  background-image: -webkit-linear-gradient(#FF0000 40%, Tomato 100%);
  background-image: linear-gradient(#FF0000 40%, Tomato 100%);
}
<div class="popup" onclick="showPopup('myPopup')">
  <button class="button button1" id="button1"></button>
  <span class="popuptext" id="myPopup">text of popup;</span>
</div>
<div class="popup" onclick="showPopup('myPopup2')">
  <button class="button button2" id="button2"></button>
  <div class="popuptext" id="myPopup2">
    <p>
      L 4 Some text here some text
    </p>

    <p>
      12.07.2016 
    </p>

    <img class="manImg" alt="Pilt" src="Lubja/1.jpg">
    <img class="manImg" alt="Pilt" src="Lubja/2.jpg">
    <img class="manImg" alt="Pilt" src="Lubja/3.jpg">
    <img class="manImg" alt="Pilt" src="Lubja/4.jpg">

  </div>
</div>

<!-- Buttons on map start -->
<div class="popup" onclick="showPopup('myPopup3')">
  <button class="button button3" id="button3"></button>
  <div class="popuptext" id="myPopup3">
    <p>
      Koor
    </p>
    Tühi
    <p>

    </p>
    <img class="manImg" alt="Pilt" src="Lubja/1.jpg">
  </div>
</div>
<!-- Buttons on map end -->
<!-- Buttons on map start -->
<div class="popup" onclick="showPopup('myPopup4')">
  <button class="button button4" id="button4"></button>
  <div class="popuptext" id="myPopup4">
    <p>
      Tühi
    </p>
    Tühi
    <p>

    </p>
    <img class="manImg" alt="Pilt" src="Lubja/1.jpg">
  </div>
</div>