jQuery function running when it shouldn't

前端 未结 1 1719
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 04:49

I have a function which only needs to run when the width is less then a specific value. I have done this with the if ($(window).width() < n) { } but the functio

相关标签:
1条回答
  • 2021-01-27 05:16

    Instead of just if (windowsize < 1000) you also need to know the current status.

    if (is_large && windowsize < 1000)
    {
        is_large = false;
        // rest of code to handle transition to small size
    }
    else if (!is_large)
    {
        is_large = true;
        // code to handle transition to large size
    }
    

    You also need to handle the case of setting is_large on page load before the initial call to checkWidth().

    0 讨论(0)
提交回复
热议问题