gpt4 book ai didi

javascript - jQuery UI .position() 错过了使用 jQuery .height() 设置的目标元素的高度

转载 作者:行者123 更新时间:2023-11-28 06:51:12 27 4
gpt4 key购买 nike

专家您好

这是我的 HTML:

<div class="full-height">
<h1>Lorem</h1>
</div>

我正在使用 jQuery 设置 div.full-height 的高度,如下所示:

$(document).ready(function() {
function setHeight() {
viewportHeight = $(window).height();
$('.full-height').height(viewportHeight);
};
setHeight();

$(window).resize(function() {
setHeight();
});
});

现在我尝试使用 jQuery UI 将 div.full-height 中的 h1 的位置设置为 center center 如下:

$('.full-height > h1').position({
my: 'center center',
at: 'center center',
of: '.full-height'
});

问题是,如果我在 CSS 中设置固定的 height 属性,它只能正确定位。

否则 div.full-height 有全高,但里面的 h2 会粘在顶部,因为 jQuery UI 的 .position()函数不知道 div.full-height 有 100% 的高度。

我怎样才能让它工作?

问候

拉尔夫

最佳答案

专家您好

那很快。 :-)

我只是通过在调整大小函数中包含 jQuery UI 函数解决了这个问题,如下所示:

$(document).ready(function() {
function setHeight() {
viewportHeight = $(window).height();
$('.full-height').height(viewportHeight);
$('.full-height > h1').position({
my: 'center center',
at: 'center center',
of: '.full-height'
});
};
setHeight();

$(window).resize(function() {
setHeight();
});
});

谢谢!

问候

拉尔夫

关于javascript - jQuery UI .position() 错过了使用 jQuery .height() 设置的目标元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758501/

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