Javascript 在调整窗口大小时更新变量的问题

Javascript 在调整窗口大小时更新变量的问题,javascript,jquery,css,Javascript,Jquery,Css,我对浏览器窗口的大小有一个条件,但是我的变量“windowsize”没有得到更新,只考虑了文档加载时的值。我做错了什么,我需要一个全局变量吗 以下是我所拥有的: jQuery(function(){ jcf.customForms.replaceAll(); initCarousel(); initCycleCarousel(); initSlideShow(); initOpenClose(); initAccordion(); jQue

我对浏览器窗口的大小有一个条件,但是我的变量“windowsize”没有得到更新,只考虑了文档加载时的值。我做错了什么,我需要一个全局变量吗

以下是我所拥有的:

jQuery(function(){
    jcf.customForms.replaceAll();
    initCarousel();
    initCycleCarousel();
    initSlideShow();
    initOpenClose();
    initAccordion();
    jQuery('input, textarea').placeholder();

    .....
    ...

});

.....
...
// open-close init
function initOpenClose() {

var $window = $(window);
var windowsize = $window.width();

$(window).resize(function() {
  windowsize = $window.width(); 
});

  if (windowsize > 1200) {
    //if the window is greater than 1200px wide then..
        jQuery('#nav > ul > li').openClose({
            activeClass: 'active',
            opener: '> a',
            slider: '.drop-container',
            animSpeed: 200,
            event: 'over',
            effect: 'slideAlt'
        });
  }
  if (windowsize < 1200) {
    //if the window is greater than 1200px wide then..
        jQuery('#nav > ul > li').openClose({
            activeClass: 'active',
            opener: '> a',
            slider: '.drop-container',
            animSpeed: 400,
            event: 'click',
            effect: 'slideAlt'
        });
  }
});
jQuery(函数(){
jcf.customForms.replaceAll();
initCarousel();
initCycleCarousel();
initSlideShow();
initOpenClose();
手风琴();
jQuery('input,textarea')。占位符();
.....
...
});
.....
...
//开闭初始化
函数initOpenClose(){
变量$window=$(window);
var windowsize=$window.width();
$(窗口)。调整大小(函数(){
windowsize=$window.width();
});
如果(窗口大小>1200){
//如果窗口宽度大于1200px,则。。
jQuery('#nav>ul>li').openClose({
activeClass:'活动',
开场白:'>a',
滑块:'.drop容器',
速度:200,,
事件:“结束”,
效果:'slideAlt'
});
}
如果(窗口大小<1200){
//如果窗口宽度大于1200px,则。。
jQuery('#nav>ul>li').openClose({
activeClass:'活动',
开场白:'>a',
滑块:'.drop容器',
速度:400,,
事件:“单击”,
效果:'slideAlt'
});
}
});
}

尝试使用

  jQuery(function(){
      updateContainer(); // Initial load call to check it did not require resize
  });
  $(window).resize(function () {
        updateContainer(); // If Resize happens call to check
   });

 function updateContainer() {
    var windowsize = $(window).width();
    if (windowsize > 1200) {
        //if the window is greater than 1200px wide then..
        jQuery('#nav > ul > li').openClose({
            activeClass: 'active',
            opener: '> a',
            slider: '.drop-container',
            animSpeed: 200,
            event: 'over',
            effect: 'slideAlt'
        });
    }
  else
  {  
      jQuery('#nav > ul > li').openClose({
        activeClass: 'active',
        opener: '> a',
        slider: '.drop-container',
        animSpeed: 400,
        event: 'click',
        effect: 'slideAlt'
       });
   }
}
试着这样,

function initOpenClose() 
{
     var $window = $(window);
     var windowsize = $window.width();

     updateWindow();

     $(window).resize(function() {

          windowsize = $window.width(); 
          updateWindow();

     });

     function updateWindow()
     {
         if (windowsize > 1200) 
         {
                   //if the window is greater than 1200px wide then..
             jQuery('#nav > ul > li').openClose({
                activeClass: 'active',
                opener: '> a',
                slider: '.drop-container',
                animSpeed: 200,
                event: 'over',
                effect: 'slideAlt'
             });
        }
        else if (windowsize < 1200) 
        {
            //if the window is greater than 1200px wide then..
           jQuery('#nav > ul > li').openClose({
                activeClass: 'active',
                opener: '> a',
                slider: '.drop-container',
                animSpeed: 400,
                event: 'click',
                effect: 'slideAlt'
            });
         }
      }
}
函数initOpenClose()
{
变量$window=$(window);
var windowsize=$window.width();
updateWindow();
$(窗口)。调整大小(函数(){
windowsize=$window.width();
updateWindow();
});
函数updateWindow()
{
如果(窗口大小>1200)
{
//如果窗口宽度大于1200px,则。。
jQuery('#nav>ul>li').openClose({
activeClass:'活动',
开场白:'>a',
滑块:'.drop容器',
速度:200,,
事件:“结束”,
效果:'slideAlt'
});
}
否则如果(窗口大小<1200)
{
//如果窗口宽度大于1200px,则。。
jQuery('#nav>ul>li').openClose({
activeClass:'活动',
开场白:'>a',
滑块:'.drop容器',
速度:400,,
事件:“单击”,
效果:'slideAlt'
});
}
}
}

在resize handler中尝试您的代码。如果我这样做,代码似乎不会运行。@user2300867如果您使用此代码
if(WindowsSize>1200),函数调用如何或何时发生{
outside resize当窗口小于1200时,我有一个响应菜单,它的工作方式不同,但是如果用户要调整窗口大小,js应该相应地给出正确的菜单。@user2300867检查更新的代码,这将调用onload和resize toothis不起作用bc我需要代码在浏览器窗口没有调整大小时也能工作,只要在文档加载时。它确实可以工作,但只有在我调整窗口大小之前,如果我从不调整窗口大小,我需要代码也可以运行。”$(document).ready“意味着在文档加载后运行。因为docReady中包含“updateContainer”,它将在加载和调整大小时运行。我正在更新一个由更有经验的开发人员开发的应用程序,但它不使用$(文档)准备好了,但是jQuery(function(){initOpenClose();然后是function initOpenClose(){我的代码…非常感谢您的帮助。只有一件事…更新的版本可以工作,但它只在我第一次重新调整大小时工作。您知道为什么会发生这种情况吗?