jQuery不到添加和删除类的功能

时间:2013-08-22 10:17:54

标签: jquery

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

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




});

2 个答案:

答案 0 :(得分: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');
}      

答案 1 :(得分: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');
        }
    });
});




});