简体   繁体   English

如何从resize函数中访问变量值

[英]how to access variable value from within a resize function

I have an else..if statement that assigns a value to a variable based on the window.width , what I would like to do is then use this value inside a window.resize but at the moment get 'undefined' for currentSize the value. 我有一个else..if语句,它根据window.width为变量赋值,然后我想要做的是在window.resize使用这个值但是目前为currentSize得到'undefined'值。 What should I be doing in order to get the value? 我应该怎么做才能获得价值?

JS JS

//Code ran onload
if(windowWidth >= 924) {
    incrementValue = 3;
    currentSize = 'desktop';
}
else if(windowWidth > 615 && windowWidth < 924) {
    incrementValue = 2;
    currentSize = 'tablet';
}
else {
    incrementValue = 1;
    currentSize = 'mobile';
}

$(window).resize(function() {
    windowWidth = $(window).width();

    if(windowWidth >= 924) {
        incrementValue = 3;
        var newSize = 'desktop';
    }
    else if(windowWidth > 615 && windowWidth < 924) {
        incrementValue = 2;
        var newSize = 'tablet';
    }
    else {
        incrementValue = 1;
        var newSize = 'mobile';
    }

    console.log(currentSize); //'undefined'
    if (currentSize !== newSize) {
        var currentSize = newSize;

        incrementTotal = 0;
        slideIndex = 0;

        //Reset slider to start
        $carouselSlides.css( 'left', 0 );
    }
});

Don't redefine currentSize with var like this 不要像这样用var重新定义currentSize

if (currentSize !== newSize) {
    var currentSize = newSize;

Remove the var 删除var

if (currentSize !== newSize) {
    currentSize = newSize;

Here is an example which allows you to reproduce or eliminate your error depending on the presence of var : 下面是一个示例,它允许您根据var的存在重现或消除您的错误:

<script>
//global scope
var currentSize = 'desktop', incrementTotal = 0, incrementValue = 0, slideIndex = 0, $carouselSlides = $('#carouselSlides');

function windowResize(windowWidth) {
    var data = [];
    if (windowWidth >= 924) {
        data = ['desktop', 3];
    } else if (windowWidth > 615 && windowWidth < 924) {
        data = ['tablet', 2];
    } else {
        data = ['mobile', 1];
    }
    return data.length ? data : false;
}


$(window).resize(function() {
    var windowWidth = $(this).width();
    var data = windowResize(windowWidth);
    // data[0] = desktop, tablet OR mobile
    // data[1] = 1, 2 OR 3
    incrementValue = data[1];

    if (currentSize !== data[0]) {
        currentSize = data[0];

        incrementTotal = 0;
        slideIndex = 0;

        //Reset slider to start
        $carouselSlides.css('left', 0);
    }    

    // set the style for the current screen size   
});
</script>

Your newSize is out of scope: 你的newSize超出了范围:

$(window).resize(function() {
   windowWidth = $(window).width();
var newSize =0;
   if(windowWidth >= 924){
      incrementValue = 3;
        newSize = 'desktop';
    } else if(windowWidth > 615 && windowWidth < 924) {
      incrementValue = 2;
      newSize = 'tablet';
    } else {
      incrementValue = 1;
      newSize = 'mobile';
    }

    if (currentSize !== newSize) {
      var currentSize = newSize;

      incrementTotal = 0;
      slideIndex = 0;

      //Reset slider to start
      $carouselSlides.css( 'left', 0 );
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM