javascript 更改内部 html 后自动调整 div 高度
该网站的位置如下:http://pwnage.me/
javascript 位于此处:{site}/scripts/main.js
我正在使用 jQuery 将内容动态加载到主页上的 #container div 中。当您单击主页上的链接(例如“测试项目 1”)时,它会从 ajax.php 加载内容并将新的 html 粘贴到 #container div 中。我尝试了很多不同的解决方案,但我无法获得适合新内容的高度。它最终会延伸到 div 的末尾。但是,如果您访问 http://pwnage.me/projects/test,它会按预期工作。我尝试了与页面加载相同的方法,但它不起作用:
$('#container').html(d);
var h = $('#container').height();
$('#container').html(load_html); // load_html is a predefined var that holds html of a loading image
$('#container').animate({ height: h, opacity: 0 }, 300); // open to new height and fade out loading image
setTimeout("$('#container').html(d);", 300); // d is saved in a global var so this works
setTimeout("$('#container').animate({ opacity: 1 }, 300);", 300);
所以它应该:
- 获取新的高度
- 加载加载图像 html
- 将高度滑出到新值并淡出
- 加载新的 html
- 淡入
它有效正如它应该的那样,除了第一次加载新的 html 时,div 永远不会扩展高度以适应新内容,因此它获取旧的高度值。
Here's where the site is located: http://pwnage.me/
The javascript is located here: {site}/scripts/main.js
I'm using jQuery to dynamically load the content into the #container div on the main page. When you click a link on the main page (eg. "Test Project 1"), it loads the content from ajax.php and sticks the new html in the #container div. I've tried tons of different solutions but I can't get the height to fit the new content. It just ends up extending past the end of the div. However, if you go to http://pwnage.me/projects/test, it works as intended. I tried the same way I use on page load, but it doesn't work:
$('#container').html(d);
var h = $('#container').height();
$('#container').html(load_html); // load_html is a predefined var that holds html of a loading image
$('#container').animate({ height: h, opacity: 0 }, 300); // open to new height and fade out loading image
setTimeout("$('#container').html(d);", 300); // d is saved in a global var so this works
setTimeout("$('#container').animate({ opacity: 1 }, 300);", 300);
So it should:
- Get the new height
- Load the loading image html
- Slide the height out to the new value and fade out
- Load the new html
- Fade in
It works as it should except that when it loads the new html the first time, the div never expands the height to fit the new content, so it gets the old height value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试在动画中执行此操作,而不是尝试指定确切的高度。此外,将加载微调器放在与内容区域分开的 div 中:
然后在获得新内容时执行此操作(即在 AJAX 请求结束时):
我希望这将帮助您走上正轨。
You might try doing this in your animate instead of trying to specify an exact height. In addition, put your loading spinner in a div that's separate from your content area:
Then do this when you have your new content (i.e. at the end of your AJAX request):
I hope this will help you get on track.
您尝试过 $.slideDown() 吗?
容器无法正确调整大小的原因是内容的静态高度设置太小。
Have you tried $.slideDown() ?
The reason the container isn't re-sizing properly is because the content has a static height set on it that is too small.
问题似乎是您已将容器高度固定为 67px。
与在加载 html 之前没有固定容器高度的示例相比。
The problem appears to be that you have fixed the container height at 67px.
Vs the example that didn't have a fixed container height until the html was loaded.