奇怪的问题,关于js+css实现页面内容高度自适应的两种解决方案
2010-09-23 00:09
971 查看
本站blog板块
的css.css样式表中,我用以下CSS的语法成功实现了IE/火狐
两种浏览器的内嵌页面,随着页面内容多少自适应问题:
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
#tag {width:950px;height:auto!important; min-height:388px;
height:auto;overflow:hidden;margin:0 auto;}
#header
{width:950px;height:40px;margin:0 auto;}
#page
{width:950px;height:auto!important; min-height:388px;
height:auto;overflow:hidden;margin:0
auto;background:#ffffff;}
#left{width:295px;height:auto!important;
min-height:388px;
height:auto;overflow:hidden;float:left;background:url(images/1_10.gif);}
#right{width:655px;height:auto!important;
min-height:388px; height:auto;overflow:hidden;margin:0px 0px 0px
0px;float:left;background:#ffffff;}
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
然而,在本站主页面的main.css中,用了类似以上的CSS语法:height:auto!important;min-height:388px;height:auto;overflow:hidden
却无法实现高度自适应。
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
不得已,用JS的方式实现了高度自适应问题,这样以后我要修改页面内容,则只需要修改item_xxx.html内嵌页面,而不需要像以前一样,去修改JS中的document.writeln中的height参数。
确实方便多了,但有个弊端,这样给网站服务器增加了负载(因为JS要计算内嵌iframe页面的高度),而且,网速不够快的用户,很容易被卡到页面,把当前页面显示为一个空白。
不明白,为什么两种方法实现的效果不一样。
下面共享一下本人用JS实现内嵌页面高度自适应功能的代码:
<!--
function
AutoHeight(fm_name,fm_id){
var frm=document.getElementById(fm_id);
var
subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;
if (frm != null && subWeb != null){
frm.style.height
= subWeb.documentElement.scrollHeight+ "px"
;
//如需自适应宽高,去除下行的“//”注释即可
// frm.style.width =
subWeb.documentElement.scrollWidth+"px";
}
}
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
//-->
document.writeln("<iframe onLoad =/"{AutoHeight('FM_Name','FM_Id');}/"
");
document.writeln("src =/"../item_forU.html/"
");
document.writeln("name =/"FM_Name/" id =/"FM_Id/" width =/"950/"
marginwidth =/"0/" marginheight =/"0/" ");
document.writeln("frameborder
=/"0/" scrolling =/"no/"><//iframe>")
(PS:做人要厚道,转载请注明出处:http://www.3netgo.com/blog
)
的css.css样式表中,我用以下CSS的语法成功实现了IE/火狐
两种浏览器的内嵌页面,随着页面内容多少自适应问题:
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
#tag {width:950px;height:auto!important; min-height:388px;
height:auto;overflow:hidden;margin:0 auto;}
#header
{width:950px;height:40px;margin:0 auto;}
#page
{width:950px;height:auto!important; min-height:388px;
height:auto;overflow:hidden;margin:0
auto;background:#ffffff;}
#left{width:295px;height:auto!important;
min-height:388px;
height:auto;overflow:hidden;float:left;background:url(images/1_10.gif);}
#right{width:655px;height:auto!important;
min-height:388px; height:auto;overflow:hidden;margin:0px 0px 0px
0px;float:left;background:#ffffff;}
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
然而,在本站主页面的main.css中,用了类似以上的CSS语法:height:auto!important;min-height:388px;height:auto;overflow:hidden
却无法实现高度自适应。
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
不得已,用JS的方式实现了高度自适应问题,这样以后我要修改页面内容,则只需要修改item_xxx.html内嵌页面,而不需要像以前一样,去修改JS中的document.writeln中的height参数。
确实方便多了,但有个弊端,这样给网站服务器增加了负载(因为JS要计算内嵌iframe页面的高度),而且,网速不够快的用户,很容易被卡到页面,把当前页面显示为一个空白。
不明白,为什么两种方法实现的效果不一样。
下面共享一下本人用JS实现内嵌页面高度自适应功能的代码:
<!--
function
AutoHeight(fm_name,fm_id){
var frm=document.getElementById(fm_id);
var
subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;
if (frm != null && subWeb != null){
frm.style.height
= subWeb.documentElement.scrollHeight+ "px"
;
//如需自适应宽高,去除下行的“//”注释即可
// frm.style.width =
subWeb.documentElement.scrollWidth+"px";
}
}
淘宝网精品女装
淘宝网女士服装
淘宝网女士内衣
淘宝网入口
(健身减肥产品,汽车装饰,男人装,数码玩意...应有尽有)
淘宝网入口官方网站
//-->
document.writeln("<iframe onLoad =/"{AutoHeight('FM_Name','FM_Id');}/"
");
document.writeln("src =/"../item_forU.html/"
");
document.writeln("name =/"FM_Name/" id =/"FM_Id/" width =/"950/"
marginwidth =/"0/" marginheight =/"0/" ");
document.writeln("frameborder
=/"0/" scrolling =/"no/"><//iframe>")
(PS:做人要厚道,转载请注明出处:http://www.3netgo.com/blog
)
相关文章推荐
- iframe自适应内层页面高度,可响应页面中折叠点击事件,以及实现跨域的问题解决方案
- js实现textarea根据内容大小自适应高度
- DIV+CSS 设计的页面,DIV高度自适应问题
- jquery/js实现文本框根据输入内容input,textarea自适应高度
- jquery采用JS实现高度自适应问题
- js控制三列高度自适应DIV+CSS页面
- 关于 基于maven创建的项目jsp页面无法加载css、js、图片的问题
- Jquery与JS两种方法仿twitter/新浪微博 高度自适应无缝滚动实现代码
- 关于iframe的高度自适应问题(js)
- js控制三列高度自适应DIV+CSS页面
- [转]iframe自适应加载页面高度-js实现
- iframe自适应加载页面高度-js实现(转载)
- Android 关于在ScrollView中加上一个ListView,ListView内容显示不完全(总是显示第一项)的问题的两种简单的解决方案
- 关于iframe的高度自适应问题(js)
- div模拟textarea以css控制最大高度和最小高度实现高度自适应实例页面
- 问题:关于贴友的一个书本页面简单布局(html+css)的实现
- 关于iframe自适应高度,解决一个iframe动态改变url,改变页面同时解决高度自适应问题
- js+css实现页面部分内容打印
- js实现textarea高度根据内容自适应
- css---pc、无线如果通过js获取屏幕宽度结合sass实现自适应页面