如何在一个页面上创建多个div弹出窗口?

时间:2018-05-28 13:14:06

标签: javascript html css popup

我需要项目帮助。

有没有办法用我的代码在同一页面上创建多个弹出窗口?我只能打开第一个...我知道它必须对脚本做一些事情,但对于我的生活,我不知道我要做什么,并且w3school上没有任何东西可以帮助我。< / p>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}
</style>
</head>
<body style="text-align:center">

<h2>Popup</h2>

<div class="popup" onclick="myFunction()">Popup 1!
  <span class="popuptext" id="myPopup">Popup 1 Content</span>
</div>
&nbsp; &nbsp; &nbsp;
<div class="popup" onclick="myFunction()">Popup 2!
  <span class="popuptext" id="myPopup1">Popup 2 Content!</span>
</div>

<script>
// When the user clicks on div, open the popup
function myFunction() {
    var popup = document.getElementById('myPopup');
    popup.classList.toggle("show");
}
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

您正在引用相同的弹出元素,即对于两个on click事件都具有myPopup id的元素,当它应该取决于单击哪个div时。 通过使用鼠标事件(currentTarget属性),您将能够获得所选(单击)元素,并通过遍历它的子元素,您将能够切换适当的弹出窗口类 plunkr link

function myFunction(event) {
  event.target.children[0].classList.toggle("show");
}

并更改onclick事件myFunction()以传递事件参数

<div class="popup" onclick="myFunction(event)">

答案 1 :(得分:0)

请尝试此

$(function(){
$('.popup').on('click', function(){
    $(this).addClass('show').siblings().removeClass('show');
  });
});
/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

.popup.show .popuptext {
	 visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Popup</h2>

<div class="popup">Popup 1!
  <span class="popuptext">Popup 1 Content</span>
</div>
&nbsp; &nbsp; &nbsp;
<div class="popup">Popup 2!
  <span class="popuptext">Popup 2 Content!</span>
</div>