这段时间在工作中遇到一个看似较为棘手的问题。问题描述:查询报表页面分为上下两部分,上部分为条件输入区域,下部分为报表展示区域。客户要求做到默认满屏(但要动态适应不同的窗体大小,也就是浏览器窗体用户会手动改变其大小),但上部分条件输入区域有动态变化高度的现象。

在遇到上述问题,您是否第一反应就是利用window的onresize事件,做尺寸的动态调整。但是条件输入区域某个按钮动态改变了上部分的高度时,我们又应该如何呢。是否有统一的处理方案呢。今儿本人就把我自己的想法和测试提供出来,供大家参考,有疑问或建议欢迎交流和沟通。

一、上代码

闲话少说,上代码。首先本人为了处理与IE的兼容性,对现代浏览器,IE浏览器做了区别对待。然后提供了一个工厂类以供使用。

1.1、 现代浏览器的实现

/**
* 现代浏览器处理方案
*/
function RptAutoHeightForModernBrower(context){
this.context = context;
this.$object = null;
}
var mPt = RptAutoHeightForModernBrower.prototype;
mPt.init = function(){
var object = document.createElement('iframe'), self = this; //object在ie11上onload方法不能执行
//区元素,绝对定位(父级必须是相对定位,否则参考到body了),四个为0,width、height为100%让其宽、高与父级相同,pointer-events:none(不接受鼠标事件)z-index:层级最低。
object.onload = function(){
var context = this;
this.contentDocument.defaultView.addEventListener('resize', function(){
self.context.onResize(context.clientHeight);
});
}
object.setAttribute('style', 'display:block; position:absolute; border:0px; visibility: hidden; left:0px; right: 0px; top: 0px; bottom: 0px; pointer-events: none; z-index: -1; overflow:hidden; width: 100%; height: 100%; opacity:0;');
//object.type = "text/html";
object.src = "about:blank";
this.context.$header.appendChild(object);
this.$object = object;
//先触发一次
this.context.onResize(this.context.$header.clientHeight);
//window.resize事件
window.onresize = function(){
self.context.onResize(self.context.$header.clientHeight);
}
}
mPt.dispose = function(){
this.$object.contentDocument.defaultView.removeEventListener('resize');
this.context.$header.removeChild(this.$object);
}

在此处,为了做到兼容IE11(因为Ie11不支持attacheEvent方法,所以也会被判断为现代浏览器),本人创建的DOM,不是使用的object而是使用的iframe,因为在IE下object的onload事件不能触发,而iframe的可能有;并且iframe的边框一定要去掉,否则影响判断。

1.2、ie浏览器的实现

/**
* ie的处理方案
*/
function RptAutoHeightForIE(context){
this.context = context;
}
var iePt = RptAutoHeightForIE.prototype;
iePt.init = function(){
var self = this;
this.context.$header.attachEvent('onresize', function(){
self.context.onResize(window.event.srcElement.clientHeight);
});
this.context.onResize(this.context.$header.clientHeight);
//window.resize事件
window.onresize = function(){
self.context.onResize(self.context.$header.clientHeight);
}
}
iePt.dispose = function(){
this.context.$header.detachEvent('onresize');
}

IE浏览器的实现相对简单,因为IE环境下的div天身支持onresize事件。

1.3、工厂类

//处理高度自适应的Factory
function RptAutoHeightFactory(opts){
this.opts = opts || {};
this.$wrap = this.opts.wrap || document.getElementsByClassName('rpt-wrap')[0];
this.$header = this.opts.header || this.$wrap.getElementsByClassName('rpt-header')[0];
this.$cont = this.opts.cont || this.$wrap.getElementsByClassName('rpt-cont')[0];
var cxt = {
$header: this.$header,
onResize: this.resize()
};
this.diffVal = 0;
this.realize = document.attachEvent
? new RptAutoHeightForIE(cxt)
: new RptAutoHeightForModernBrower(cxt);
}
var pt = RptAutoHeightFactory.prototype;
pt.init = function(){
var bTop = this.getStyle(this.$header, "border-top-width");
var bBottom = this.getStyle(this.$header, "border-bottom-width");
bTop = parseInt(bTop.replace('px', ''), 10);
bBottom = parseInt(bBottom.replace('px', ''), 10);
this.diffVal += bTop + bBottom; var bTop = this.getStyle(this.$cont, "border-top-width");
var bBottom = this.getStyle(this.$cont, "border-bottom-width");
bTop = parseInt(bTop.replace('px', ''), 10);
bBottom = parseInt(bBottom.replace('px', ''), 10);
this.diffVal += bTop + bBottom; this.realize.init(); }
pt.resize = function(){
var $cont = this.$cont, self = this;
return function(headerHeight){
var dist = self.getMaxHeight() - headerHeight - self.diffVal;
if(dist > 1 ){
$cont.style.height = dist +'px'; //加单位,是为了兼容ie
}
}
}
pt.getHeight = function(dom){
var height = dom.clientHeight;
return height;
}
pt.getStyle = function(dom, key){
if(dom.currentStyle){
return dom.currentStyle[key];
}else if(window.getComputedStyle){
return window.getComputedStyle(dom, null)[key];
}
}
pt.getMaxHeight = function(){
return document.documentElement.clientHeight || document.body.clientHeight;
}

此处本人在获取style的属性值,使用了getComputedStyle和currentStyle实现的,这民是标准的方法。

1.4、这样使用

js代码:

var irow = 2;
function addRow(){
var parent = document.getElementsByClassName('rpt-header')[0];
var p = document.createElement('p');
p.innerHTML = "<p>添加第" + irow++ + "行记录</p>";
parent.appendChild(p);
} var autoHeightFactory = new RptAutoHeightFactory();
autoHeightFactory.init();

html代码:

<div class="rpt-wrap">
<div class="rpt-header">
<button type="button" onclick="addRow()">添加</button>
<p>第一行内容</p>
</div>
<div class="rpt-cont">
</div>
</div>

css代码:

html, body{
margin: 0px;
padding: 0px;
height: 100%;
}
.rpt-wrap{
height: inherit;
overflow: hidden;
}
.rpt-header{
border: 1px solid gray;
position: relative; }
.rpt-cont{
border: 1px solid red;
}

上下div高度动态自适应--另类处理方案的更多相关文章

  1. iframe高度动态自适应

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1294 一.前言碎碎念 ...

  2. 纯CSS实现Div高度根据自适应宽度(百分百调整)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. div高度不能自适应(子级使用float浮动,父级div高度不能自适应)

    1.问题截图: 2.问题描述: 由于地址.公司名长度的不定性,所以每一条地址所在的父级div高度不定,但是需要设置一个最小的高度min-height:48px;但是当内容增加的时候,父级div高度却不 ...

  4. <转载>如何解决子级用float浮动父级div高度不能自适应的问题

    转载:http://www.kwstu.com/ArticleView/divcss_2013101582430202 解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内 ...

  5. 让一个父级div根据子级div高度而自适应高度

    需求是点击上传的时候进行子级div高度不定,相对来说父级div高度也不能固定,把元素都设置成普通标准流,然后样式可以使用margin内边距或者padding外边距来进行调节 放上代码供参考: .opu ...

  6. 纯Css实现Div高度根据自适应宽度(百分比)调整

    在如今响应式布局的要求下,很多能自动调整尺寸的元素能够做到高宽自适应,如img,通过{width:50%;height:auto;}实现图片高度跟随宽度比例调整. 然而,用的最多的标签一哥Div却不能 ...

  7. 子元素div高度不确定时父div高度如何自适应

    粘自:http://www.jb51.net/css/110652.html 在最外层div加以下样式 height:100%; overflow:hidden; 其它方法: Div即父容器不根据内容 ...

  8. 父div高度不能自适应子div高度的解决方案

    <div id="parent"><div id="content"> </div></div> 当conten ...

  9. 元素float以后,div高度无法自适应解决方案

    首先要明白 >> 浮动的子元素会脱离文档流,不再占据父元素的空间,父元素也就没有了高度. 解决方案:1 给父元素加上overflow:hidden;属性就行了. 第一种:(给父级加over ...

随机推荐

  1. Maven导入项目时报错 Could not calculate build plan

    Could not calculate build plan: Plugin org.apache.maven.plugins:maven-war-plugin:2.2 or one of its d ...

  2. linux系统,关于Python多版本共存

    http://www.cnblogs.com/Yiutto/p/5962906.html 给个地址直接看八~

  3. java自动化-实际使用junit的演示

    本文简单介绍一下我写的http接口后端框架 在经过之前多篇博客介绍之后,读者应掌握如下技能 1,自动运行一个或者多个junit框架编写的java代码 2,对数据驱动以及关键字驱动有一定的了解和认识,甚 ...

  4. Android Getting Started

    Building Your First App Creating an Android Project 介绍如何Android开发环境,具体是:怎么使用Eclipse工具 Create a Proje ...

  5. HTML 5将给开发者带来什么?

    在新的时代里,相信网页技术会伴随HTML 5的来临进入大洗牌的局面,HTML 5旨在解决Web中的交互,媒体,本地操作等问题,一些浏览器已经尝试支持HTML 5的一些功能,而开发者们有望最终从那些We ...

  6. fiddler 应用

    一   pc 端抓取 例:本地调试代码,转换域名,请求网络数据 1:设置代理,以smart header 为例 ip为 127.0.0.1 端口与自己的fillder一致,注意将不代理的地址列表做修改 ...

  7. Spring源码Gradle

    Microsoft Windows [版本 10.0.17134.590](c) 2018 Microsoft Corporation.保留所有权利. D:\Workspaces\idea\sprin ...

  8. 正则匹配ts的引用

    ts文件引入代码如下: import { IView, GridView, Sizing as GridViewSizing, Box, IGridViewStyles } from './gridv ...

  9. hibernate框架中注意的几个问题

    使用hibernate框架中,session.createSQLQuery创建sql语句的时候遇到的问题 1.  select e.id,d.id from emp e,dept d where e. ...

  10. 关于阿里云Centos7 Mailx发送邮件失败的处理

    开始通过配置系统自带的mail 发送邮件 在其中一台服务器怎么都不行 /etc/mail.rc配置: set from="邮箱名" set smtp="smtp.163. ...