在窗口调整大小时发出更新变量

时间:2014-06-24 05:46:42

标签: javascript jquery css

我有一个浏览器窗口大小的条件,但是我的变量" 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'
        });
  }
});

}

2 个答案:

答案 0 :(得分:2)

尝试

  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'
       });
   }
}

答案 1 :(得分:0)

试试这个,

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'
            });
         }
      }
}