jquery 设置加载后的高度
我在设置动态加载的元素的高度时遇到了一些麻烦。
我使用 jquery load
函数将外部(动态)页面加载到当前页面上的 div (#cbox) 中。因为这个子页面是动态的,所以我无法事先知道内容的高度是多少。我想在加载内容后获取高度,并将容器 div 的高度设置为匹配,以便我的颜色背景 css 一直向下。我仅在CSS中尝试了许多100%高度div的变体,但是一旦我滚动页面,颜色就会向上滚动(100%似乎只设置浏览器窗口高度的100%,并且b/c内容是动态的我的解决方案是将 div 的高度设置为加载内容的高度,但这仅适用于第二次单击(因为此时页面已加载并可访问。我需要弄清楚什么)如何做是在外部页面加载后更改 div 的高度,但我似乎无法弄清楚)
我希望这对某人来说是可以理解的,我意识到这是
我的 onclick 。代码:
jQuery('#cbox').load('externalpage.php');
jQuery('#cbox').height(jQuery('#content').height());
更新: 如果我想设置该 div 的高度,下面的解决方案有效。但现在我发现我只想将其设置为 div 高度,如果内容高度比窗口高。否则我希望将其设置为 100%。我尝试稍微修改他们的代码(onlick 事件):
jQuery('#cbox').load('<?php the_permalink(); ?>', function()
{
if (jQuery('#cbox').height() < jQuery('#content').height())
{
jQuery('#cbox').height(jQuery('#content').height());
}
else
{
jQuery('#cbox').height('100%');
}
});
但它不起作用......有什么想法吗?
I am having a little trouble setting the height of an element that I am loading in dynamically.
I use the jquery load
function to load an external (dynamic) page into a div (#cbox) on my current page. Because this sub-page is dynamic, I can't know before-hand what the height of the content is. I want to get the height once content is loaded and set the height of the container div to match so that my color background css goes all the way down. I have tried many variations of 100% height divs in css only, but as soon as I scroll the page, the color scrolls up (100% seems to only set 100% of the browser window height, and b/c the content is dynamically loaded it does not work. My solution is to set the height of the div to the height of the loaded content, but this only works on the SECOND click (because at that point the page is loaded and accessible. What I need to figure out how to do is change the height of the div AFTER the external page has already loaded, but I can't seem to figure it out).
I hope this is understandable to someone, I realize it is a bit convoluted.
here is my onclick code:
jQuery('#cbox').load('externalpage.php');
jQuery('#cbox').height(jQuery('#content').height());
UPDATE:
The solutions below work if I want to set the height to that div. But now I find that I only want to set it to that div height IF the content height is TALLER than the window. Otherwise I want it set to 100%. I tried modifying their code slightly to this (onlick event):
jQuery('#cbox').load('<?php the_permalink(); ?>', function()
{
if (jQuery('#cbox').height() < jQuery('#content').height())
{
jQuery('#cbox').height(jQuery('#content').height());
}
else
{
jQuery('#cbox').height('100%');
}
});
but it does not work...any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内容加载后需要调用回调函数。
You need to call callback function after content is loaded.
使用加载回调。
Use the load callback.