gpt4 book ai didi

javascript - 应用 css 高度后获取网页上元素的默认高度)

转载 作者:太空宇宙 更新时间:2023-11-03 19:05:15 25 4
gpt4 key购买 nike

如果页面上的元素忽略了应用于它的“高度”css 属性,我该如何获取该元素的高度?

我正在处理的站点是 http://www.wncba.co.uk/results到目前为止我得到的实际脚本是:

jQuery(document).ready(function($) {

document.origContentHeight = $("#auto-resize").outerHeight(true);
refreshContentSize(); //run initially
$(window).resize(function() { //run whenever window size changes
refreshContentSize();
});
});

function refreshContentSize()
{
var startPos = $("#auto-resize").position();
var topHeight = startPos.top;
var footerHeight = $("#footer").outerHeight(true);
var viewportHeight = $(window).height();
var spaceForContent = viewportHeight - footerHeight - topHeight;

if (spaceForContent <= document.origContentHeight)
{
var newHeight = document.origContentHeight;
}
else
{
var newHeight = spaceForContent;
}
$("#auto-resize").css('height', newHeight);
return;
}

[ http://www.wncba.co.uk/results/javascript/fill-page.js ]

我想要做的是让主页内容拉伸(stretch)以填充窗口,这样绿线总是沿着页面一直向下流动,并且“有效的 HTML5”和“设计者”消息永远不会在上面窗口的底部。我不希望页脚粘在底部。如果上面没有足够的内容来填充它,我只是希望它留在那里而不是向上移动页面。如果浏览器窗口大小发生变化,它还必须相应地进行调整。

到目前为止我得到的脚本可以工作,但有一个小问题我想用它来解决。目前,如果页面上的内容动态变化(导致页面变长或变短),脚本将无法检测到这一点。变量 document.origContentHeight 将保持设置为旧高度。

有没有一种方法可以检测元素的高度(例如示例中的#auto-resize)以及它是否已更改而忽略了在 css 中为其设置的高度?然后我将使用它来更新变量 document.origContentHeight 并重新运行脚本。

谢谢。

最佳答案

除了使用插件,我认为没有办法检测元素大小何时发生变化,

$(element).resize(function() //only works when element = window

但是为什么不在页面动态变化时调用refreshContentSize函数呢?

看看这个jsFiddle DEMO ,你会明白我的意思。

或者您可以使用 Jquery-resize-plugin .

关于javascript - 应用 css 高度后获取网页上元素的默认高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10554053/

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