Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery:函数内部引用和更新外部变量_Jquery_Variables_Constructor - Fatal编程技术网

jquery:函数内部引用和更新外部变量

jquery:函数内部引用和更新外部变量,jquery,variables,constructor,Jquery,Variables,Constructor,我已经尝试了几天: 设置变量“w”的值 每次加载或调整窗口大小时,在构造函数中更新“this.paneWidth=w;” 我面临的问题: JQuery首先运行JS构造函数,并将“w”值返回为未定义的,我不知道如何解决这个问题 我希望能够在每次加载或调整窗口大小时更新构造函数值,但到目前为止还没有成功 请帮忙!谢谢 jQuery.event.add(window, "load", resizeFrame); jQuery.event.add(window, "resize", resizeFram

我已经尝试了几天:

  • 设置变量“w”的值
  • 每次加载或调整窗口大小时,在构造函数中更新“this.paneWidth=w;”
  • 我面临的问题:

  • JQuery首先运行JS构造函数,并将“w”值返回为未定义的,我不知道如何解决这个问题
  • 我希望能够在每次加载或调整窗口大小时更新构造函数值,但到目前为止还没有成功
  • 请帮忙!谢谢

    jQuery.event.add(window, "load", resizeFrame);
    jQuery.event.add(window, "resize", resizeFrame);
    
    var h,
        w,
        sliderWidth;
    
    function resizeFrame() {
        h = $(window).height();
        w = $(window).width(); // assign value to variable
        sliderWidth = (w * 10);
        $("div.slider").css('width',sliderWidth);
        $("div.pane").css('width',w);
    }
    
    /************ JS CONSTRUCTOR ************/
    function Slider(container, nav) {
        this.container = container;
        this.nav = nav;
        this.panes = this.container.find('div.pane');
        this.paneWidth = w; // referencing variable
        this.panesLength = this.panes.length;
        this.current = 0;
        console.log('pane width = ' + w);
    
    }
    

    您可以在构造函数中定义全局变量

    function resizeFrame() {
        h = $(window).height();
        w = $(window).width(); // assign value to variable
        sliderWidth = (w * 10);
        $("div.slider").css('width',sliderWidth);
        $("div.pane").css('width',w);
    }
    
    jQuery.event.add(window, "load", resizeFrame);
    jQuery.event.add(window, "resize", resizeFrame);
    
    /************ JS CONSTRUCTOR ************/
    function Slider(container, nav) {
        h = typeof(h) == 'undefined' ? null : h;
        w = typeof(w) == 'undefined' ? null : w;
        sliderWidth = typeof(sliderWidth) == 'undefined' ? null : sliderWidth;
    
        this.container = container;
        this.nav = nav;
        //this.panes = this.container.find('div.pane');
        this.paneWidth = w; // referencing variable
        //this.panesLength = this.panes.length;
        this.current = 0;
        console.log('pane width = ' + w);
    
    }
    new Slider();