JavaScript window resize event

前端 未结 13 799
余生分开走
余生分开走 2020-11-22 00:58

How can I hook into a browser window resize event?

There\'s a jQuery way of listening for resize events but I would prefer not to bring it into my project for just t

13条回答
  •  悲&欢浪女
    2020-11-22 01:20

    var EM = new events_managment();
    
    EM.addEvent(window, 'resize', function(win,doc, event_){
        console.log('resized');
        //EM.removeEvent(win,doc, event_);
    });
    
    function events_managment(){
        this.events = {};
        this.addEvent = function(node, event_, func){
            if(node.addEventListener){
                if(event_ in this.events){
                    node.addEventListener(event_, function(){
                        func(node, event_);
                        this.events[event_](win_doc, event_);
                    }, true);
                }else{
                    node.addEventListener(event_, function(){
                        func(node, event_);
                    }, true);
                }
                this.events[event_] = func;
            }else if(node.attachEvent){
    
                var ie_event = 'on' + event_;
                if(ie_event in this.events){
                    node.attachEvent(ie_event, function(){
                        func(node, ie_event);
                        this.events[ie_event]();
                    });
                }else{
                    node.attachEvent(ie_event, function(){
                        func(node, ie_event);
                    });
                }
                this.events[ie_event] = func;
            }
        }
        this.removeEvent = function(node, event_){
            if(node.removeEventListener){
                node.removeEventListener(event_, this.events[event_], true);
                this.events[event_] = null;
                delete this.events[event_];
            }else if(node.detachEvent){
                node.detachEvent(event_, this.events[event_]);
                this.events[event_] = null;
                delete this.events[event_];
            }
        }
    }
    

提交回复
热议问题