gpt4 book ai didi

javascript - jQuery 对齐 - 相对于浏览器窗口?

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:28 29 4
gpt4 key购买 nike

在各种网站的帮助下,我使用 javascript 创建了一个非常简单的弹出框,其中包含我的联系信息。我对它的工作方式很满意,除了出现的弹出窗口是绝对定位的,我想让它相对于浏览器窗口出现(即我希望弹出窗口出现在浏览器窗口的中心,不管单击信息图标时您在页面上的位置)。

我熟悉 HTML,但不熟悉 javascript。我知道相对定位在 javascript 中的工作方式非常不同,但我不知道如何解决这个问题。任何建议将不胜感激。

网页在这里:http://www.thirstlabmedia.com/

脚本如下:

<script type="text/javascript">
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 = window.innerHeight / 2 - 200;
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;
popUpDiv.style.left = window_width + 'px';
}
function popup( windowname ) {
blanket_size( windowname );
window_pos( windowname );
toggle( 'blanket' );
toggle( windowname );
}
</script>

(我很抱歉把它全部放在一行上;该网站是通过 Cargo Collective 创建的,它不接受脚本,除非它全部放在一行上)。

最佳答案

使用 CSS 位置:固定:

#my-element {
position : fixed;
top : 50%;
left : 50%;
margin : -100px 0 0 -250px;
width : 500px;
height : 200px;
z-index : 1000;
}

这是一个演示:http://jsfiddle.net/huRcV/1/

这会在 viewport 中居中放置一个 500x200px 的元素。负边距用于使元素相对于其尺寸居中。如果用户滚动页面,该元素将在 viewport 中居中。

位置的文档:https://developer.mozilla.org/en/CSS/position

fixed

Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and doesn't move when scrolled. When printing, position it at that fixed position on every page.

可以使用 JavaScript 执行此操作,但最好使用 CSS 版本。如果您确实想使用 jQuery,这里有一个简单的示例:

var $myElement = $('#my-element');
$(window).on('scroll resize', function () {
$myElement.css({
top : ($(this).scrollTop() + ($(this).height() / 2)),
});
});

这是一个演示:http://jsfiddle.net/huRcV/ (请注意,对于此演示,position : fixed 已更改为 position : absolute)

关于javascript - jQuery 对齐 - 相对于浏览器窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9047909/

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