2013-08-22 24 views
0

我试图在宽度设置前后添加和删除类,一旦宽度小于1140的addclass和删除类完美工作,但是当与超过1140删除并添加类dosnt工作。如果我能得到任何帮助,我真的会真正地预测它。jQuery的功能与添加和删除类

jQuery(document).ready(function($){ 


    var low = false; 
    $(document).ready(function() { 
    var pageWidth = $(window).width(); 
    if (pageWidth <= 1140) { 
     low = true; 
     $('.studio .thirtyheight').removeClass('thirtyheight').addClass('workfullheight'); 
     $('.studio .sixtyheight').removeClass('sixtyheight').addClass('workfullheighttwo'); 
    } 

    $(window).resize(function() { 
     if ($(window).width() <= 1140) { 
      if (!low) { 
       low = true; 
       $('.studio .thirtyheight').removeClass('thirtyheight').addClass('workfullheight'); 
       $('.studio .sixtyheight').removeClass('sixtyheight').addClass('workfullheighttwo'); 
      } 
     } 
     else if (low) { 
      low = false; 
      $('.studio .workfullheight').addClass('thirtyheight').removeClass('workfullheight'); 
      $('.studio .workfullheighttwo').addClass('sixtyheight').removeClass('workfullheighttwo'); 
     } 
    }); 
}); 




}); 
+0

尝试将其更改为'$ html.addClass( 'thirtyheight')。removeClass('workfullheight “)'。你为什么要用','把它分开呢? – tobspr

回答

0
jQuery(document).ready(function($){ 


var low = false; 
$(document).ready(function() { 
    var pageWidth = $(window).width(); 
    if (pageWidth <= 1140) { 
     low = true; 
     $('.studio .thirtyheight').removeClass('thirtyheight').addClass('workfullheight'); 
     $('.studio .sixtyheight').removeClass('sixtyheight').addClass('workfullheighttwo'); 
    } 

    $(window).resize(function() { 
     if ($(window).width() <= 1140) { 
      if (!low) { 
       low = true; 
       $('.studio .thirtyheight').removeClass('thirtyheight').addClass('workfullheight'); 
       $('.studio .sixtyheight').removeClass('sixtyheight').addClass('workfullheighttwo'); 
      } 
     } 
     else if (low) { 
      low = false; 
      $('.studio .workfullheight').addClass('thirtyheight').removeClass('workfullheight'); 
      $('.studio .workfullheighttwo').addClass('sixtyheight').removeClass('workfullheighttwo'); 
     } 
    }); 
}); 




}); 
1

你忘了别的情况下

使用它,然后它会工作

var $window = $(window), 
     $html = $('.studio .thirtyheight'), 
     $htmltwo = $('.studio .sixtyheight'); 
     if ($window.width() < 1140) { 
      return $html.addClass('workfullheight').removeClass('thirtyheight'),  
      $htmltwo.addClass('workfullheighttwo').removeClass('sixtyheight'); 
     }else{ 
      $html.removeClass('workfullheight').addClass('thirtyheight'), 
      $htmltwo.removeClass('workfullheighttwo').addClass('sixtyheight'); 
}  
+0

调整窗口大小时,即使调整大小代码分类仍然没有被删除任何建议? – Delete

+0

应用调整大小(http://api.jquery.com/resize/)当调整大小事件发生时,然后调用您的代码itys作品 –

+0

我已经看了一遍,这还没有工作计划,我已经更新了我的上面的例子。如果你看到任何东西,请告诉我。 – Delete