2016-07-26 58 views
0

我需要添加多個類以實現響應目的。我想添加navbar-fixed-top,之後它會將col-xs-4添加到包含addxs的每個元素,同樣的反應也適用於removeClass。級聯方式的jquery addClass

我嘗試下面的代碼,但沒有運氣。

<div class="middle"> 
    <nav class="navbar navbar-default"> 
     <div class="container"> 
      <div class="row"> 
       <div class="col-md-3 col-sm-3 addxs logo"> 
        <div class="navbar-header"> 
         <a href="#" class="navbar-brand"> 
          logo 
         </a> 
         <button type="button" class="navbar-toggle collapsed toggle-red toggle-me" data-toggle="collapse" data-target="#bs-navbar-collapse-1" aria-expanded="false"> 
          <span class="sr-only">Toggle navigation</span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
         </button> 
        </div> 
       </div> 

       <div class="col-md-6 col-sm-6 addxs"> 
        something 
       </div> 

       <div class="col-md-3 col-sm-3 addxs"> 
        something 
       </div> 
      </div><!-- /.row --> 
     </div><!-- /.container --> 
    </nav> 
</div><!-- /.middle --> 

的Jquery:

$(function(){ 
    if($(window).width() <= 480){ 

     $(window).scroll(function() { 
      if($(this).scrollTop() > 240) { 
       $('.middle nav.navbar-default').addClass('navbar-fixed-top').parent().next().find('.addxs').addClass('col-xs-4'); 
      }else{ 
      $('.middle nav.navbar-default').removeClass('navbar-fixed-top').parent().next().find('.addxs').removeClass('col-xs-4'); 
     } 
     }); 
    } 
}); 

回答

0

試試這個:

<script type="text/javascript"> 
    $(function(){ 
    if($(window).width() <= 480){ 

     $(window).scroll(function() { 
      if($(this).scrollTop() > 240) { 
       $('.middle nav.navbar-default').addClass('navbar-fixed-top'); 
       $('.middle nav.navbar-default').find('.addxs').addClass('col-xs-4'); 
      }else{ 
       $('.middle nav.navbar-default').removeClass('navbar-fixed-top'); 
       $('.middle nav.navbar-default').find('.addxs').removeClass('col-xs-4'); 
     } 
     }); 
    } 
}); 
</script>