gpt4 book ai didi

javascript - 控制多个弹出窗口的位置

转载 作者:行者123 更新时间:2023-12-03 04:41:11 26 4
gpt4 key购买 nike

这段代码中有 3 个弹出窗口。我希望能够控制 3 个弹出窗口中每一个的位置,但是它们都位于完全相同的位置。如何控制每一个的位置?有人可以帮忙吗?非常感谢!!

https://jsfiddle.net/vibajajo64/v8prq87x/1/

<!DOCTYPE html>
<html>
<head>
<style>
.popup {
position: fixed;
top: 0px;
left: 0px;
bottom:0px;
right: 0px;
margin: auto;
width: 200px;
height: 150px;
font-family: verdana;
font-size: 13px;
padding: 10px;
background-color: rgb(240, 240, 240);
border: 2px solid grey;
z-index: 100000000000000000;
}

.blur {
filter: blur(5px);
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
}

.cancel {
display: relative;
cursor: pointer;
margin: 0;
float: right;
height: 10px;
width: 14px;
padding: 0 0 5px 0;
background-color: red;
text-align: center;
font-weight: bold;
font-size: 11px;
color: white;
border-radius: 3px;
z-index: 100000000000000000;
}

.cancel:hover {
background: rgb(255, 50, 50);
}

#overlay1,
#overlay2,
#overlay3 {
position: fixed;
display: none;
left: 0px;
top: 0px;
right: 0px;
bottom: 0px;
background: rgba(255, 255, 255, .8);
z-index: 999;
}

#popup {
position: absolute;
width: 400px;
height: 200px;
background: rgb(255, 255, 255);
border: 5px solid rgb(90, 90, 90);
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin: auto;
}
</style>

</head>

<body>
<div id="overlay1">
<div id="popup">
<h3>POPUP 1</h3> http://www.google.com <a href="javascript:myBlurFunction(0, 'popup1');"> hide</a></div>
</div>
<div id="main_container1">
<a href="javascript:myBlurFunction(1, 'popup1');">OPEN POPUP 1</a><br/>
</div>
<br/>

<div id="overlay2">
<div id="popup">
<h3>POPUP 2</h3> http://www.cnn.com <a href="javascript:myBlurFunction(0, 'popup2');"> hide</a></div>
</div>
<div id="main_container2">
<a href="javascript:myBlurFunction(1, 'popup2');">OPEN POPUP 2</a><br/>
</div>
<br/>

<div id="overlay3">
<div id="popup">
<h3>POPUP 3</h3> http://www.yahoo.com<a href="javascript:myBlurFunction(0, 'popup3');"> hide</a></div>
</div>
<div id="main_container3">
<a href="javascript:myBlurFunction(1, 'popup3');">OPEN POPUP 3</a><br/>
</div>

<script type="text/javascript">
var myBlurFunction = function(state, popup_type) {
if (state == 1) {
if (popup_type == "popup1") {
var containerElement = document.getElementById('main_container1');
var overlayEle = document.getElementById('overlay1');
overlayEle.style.display = 'block';
containerElement.setAttribute('class', 'blur');
} else {
if (popup_type == "popup2") {
var containerElement = document.getElementById('main_container2');
var overlayEle = document.getElementById('overlay2');
overlayEle.style.display = 'block';
containerElement.setAttribute('class', 'blur');
} else {
var containerElement = document.getElementById('main_container3');
var overlayEle = document.getElementById('overlay3');
overlayEle.style.display = 'block';
containerElement.setAttribute('class', 'blur');
}}}

else {
if (popup_type == "popup1") {
var containerElement = document.getElementById('main_container1');
var overlayEle = document.getElementById('overlay1');
overlayEle.style.display = 'none';
containerElement.setAttribute('class', 'null');}

else {
if (popup_type == "popup2") {
var containerElement = document.getElementById('main_container2');
var overlayEle = document.getElementById('overlay2');
overlayEle.style.display = 'none';
containerElement.setAttribute('class', 'null');}

else {
var containerElement = document.getElementById('main_container3');
var overlayEle = document.getElementById('overlay3');
overlayEle.style.display = 'none';
containerElement.setAttribute('class', 'null');
}}
}
};
</script>


<Center>All good men try to do their level best.</Center>
</body>
</html>

最佳答案

弹出窗口是绝对定位的。您可以更改左、右、上、下来更改弹出窗口的位置。这里是一个示例 https://jsfiddle.net/v8prq87x/2/

document.getElementById("popup").style.left="200px";//
//you can use left ,right ,top ,bottom css properties to change location of absolutely positioned popup

更新

请检查更新版本 https://jsfiddle.net/v8prq87x/3/仔细检查,代码仅更改 pop-up1 - 是的,问题是您有 3 个具有相同 id 的元素,因此只有第一个元素被调用。id 是唯一名称,只有一个元素可以具有弹出 id。所以你需要的是一个类。请参阅我更新的代码。我在其中添加了 3 个弹出 div 的弹出类。因此我们可以通过 document.getElementsByClassName("popup")[0] 访问第一个类

关于javascript - 控制多个弹出窗口的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43082480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com