美文网首页
js手动触发窗口resize事件

js手动触发窗口resize事件

作者: 愤怒的阿昆达 | 来源:发表于2020-01-08 16:16 被阅读0次
手动触发resize事件:
        function doResize(){
            setTimeout(function(){
                //手动触发窗口resize事件
                if(document.createEvent) {
                    var event = document.createEvent("HTMLEvents");
                    event.initEvent("resize", true, true);
                    window.dispatchEvent(event);
                } else if(document.createEventObject) {
                    window.fireEvent("onresize");
                }
            },100);
        }
需要调用时:
        $scope.setLiActive = function($event,areaName) {
                doResize();
        }
增加resize监听事件(局部demo):
                window.removeEventListener("resize",resizeReport,false);
                resizeReport();
                window.addEventListener("resize",resizeReport,false);
                function resizeReport() {
                    $(window).resize(function(){
                        console.log('---------------------------------------------');
                        var headerContainer = document.getElementById('zhijia-header') - 30;
                        var winHeight = $(window).height();//浏览器当前窗口可视区域高度
                        var currentActiveNum = $scope.currentActiveNum;
                        var bcs = 0;
                        bcs = Math.ceil(currentActiveNum/2);//几行
                        bcs = bcs > 2 ? 2 : bcs;//bcs每个图表之间的margin
                        var eHeight = (winHeight - (72+62+20+21*bcs)) / (bcs <= 0 ? 1 : bcs) ;
                        for(var i=0;i<currentDatas.length;i++){
                            if(currentDatas[i]['active']){
                                var id = 'zhijia_'+$scope.echartObj[tabIndex0]+'_'+currentDatas[i]['id'];
                                var div = document.getElementById(id);
                                div['style']['height'] = eHeight + 'px';
                                if(currentActiveNum == 1){
                                    div['style']['width'] = headerContainer.clientWidth + 'px';
                                }
                            }
                        }
                    });
                }

相关文章

网友评论

      本文标题:js手动触发窗口resize事件

      本文链接:https://www.haomeiwen.com/subject/possactx.html