javascript實現頁面自適應reset可見區域高度

一、該功能只適用於須要頁面自動撐滿或縮小到可見區域,而且只對單個主體改變高度的需求。css

var ResetControl = function (pTask) {
        var _task = pTask || [],
            _isLock = false,            
            bindEvent = function(){
                var me = this;
                $(window).bind('resize.reset', function (event) {
                    fire.call(me);
                    if (_task.length === 0) {
                        $(window).unbind('resize.reset', arguments.callee);
                    }
                });
            }, fire = function () {
                    //當前頁面可見高度
                var _iHeight = Math.min(window.innerHeight, document.documentElement.clientHeight),
                    //頁面總體高度
                    _wHeight = Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.clientHeight, document.body.scrollHeight),
                    //頁面總體實際高度
                    _bHeight = document.body.offsetHeight,
                    _resetHeight = 0;
                if (_iHeight > _bHeight) {
                    //當前頁面可見高度 - 頁面總體實際高度
                    _resetHeight = _iHeight - _bHeight;
                } else {
                    //當前頁面可見高度 - 頁面總體高度
                    _resetHeight = _iHeight - _wHeight;
                }
                for(var i = 0,l = _task.length;i < l;i++){
                    var _h = _task[i][0].offsetHeight;
                    _h = (_h + _resetHeight) < 200 ? 200 : (_h + _resetHeight);
                    _task[i].css("height", _h + "px");
                }
            }
        this.push = function(pItem){
            _task.push(pItem);
        }
        this.init = function(){
            setTimeout(function () {
                fire();
                bindEvent();
            }, 100);
        }
    }
相關文章
相關標籤/搜索