使用javascript脚本定位CSS弹出窗口
这是一个什么样的小提琴我想我的弹出窗口看起来像。。。这很有效:使用javascript脚本定位CSS弹出窗口,javascript,html,css,popup,Javascript,Html,Css,Popup,这是一个什么样的小提琴我想我的弹出窗口看起来像。。。这很有效: (两个显示都设置为:block;),并且显示效果完美 当我在代码中运行它时,我使用一个JS脚本来弹出窗口 我的HTML: <a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a> line <br>line <br>line <br>line <br>line <br
(两个
显示
都设置为:block;
),并且显示效果完美
当我在代码中运行它时,我使用一个JS脚本来弹出窗口
我的HTML:
<a href="#" onclick="popup('popUpDiv')">Click to Open CSS Pop Up</a>
line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>line <br>
<!--POPUP-->
<script type="text/javascript" src="css-pop.js"></script>
<div id="blanket" style="display:none;"></div>
<div id="popUpDiv" style="display:none;">
<a href="#" onclick="popup('popUpDiv')" class="popclose">Click to Close CSS Pop Up</a>
</div>
<!-- END POPUP -->
下面是JS代码:
function toggle(div_id) {
var el = document.getElementById(div_id);
if ( el.style.display == 'none' ) { el.style.display = 'block';}
else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
if (typeof window.innerWidth != 'undefined') {
viewportheight = window.innerHeight;
} else {
viewportheight = document.documentElement.clientHeight;
}
if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
blanket_height = viewportheight;
} else {
if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
blanket_height = document.body.parentNode.clientHeight;
} else {
blanket_height = document.body.parentNode.scrollHeight;
}
}
var blanket = document.getElementById('blanket');
blanket.style.height = blanket_height + 'px';
var popUpDiv = document.getElementById(popUpDivVar);
popUpDiv_height=blanket_height/2-200;//200 is half popup's height
popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
if (typeof window.innerWidth != 'undefined') {
viewportwidth = window.innerHeight;
} else {
viewportwidth = document.documentElement.clientHeight;
}
if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
window_width = viewportwidth;
} else {
if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
window_width = document.body.parentNode.clientWidth;
} else {
window_width = document.body.parentNode.scrollWidth;
}
}
var popUpDiv = document.getElementById(popUpDivVar);
window_width=window_width/2-200;//200 is half popup's width
popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
blanket_size(windowname);
window_pos(windowname);
toggle('blanket');
toggle(windowname);
}
我对JS代码的理解是#毯子
和#popUpDiv
更改显示:无代码>至<代码>显示:块代码>。。。但是弹出窗口没有像在小提琴上那样放在正确的位置?我不知道为什么。。。JS中一定有什么东西导致了这个。。。因为HTML和CSS正确地定位了弹出窗口。。。有什么想法吗
谢谢哦,我的。。。首先,您应该将其从css中删除:
margin-left: -200px;
margin-top: -200px;
然后替换为:
popUpDiv_height=blanket_height/2-200;//200 is half popup's height
因此:
popUpDiv_height=viewportheight/2-200;//200 is half popup's height
因为这是一页的全高。您只需要视口的高度
popUpDiv_height=viewportheight/2-200;//200 is half popup's height