2016-07-09 20 views
0

ウィンドウのサイズ変更でJavascriptを無効/再有効にできる必要がありますか? 現在、デスクトップのウィンドウのサイズが変更されると、ナビゲーションバーがスティックされ、コンテンツではなく表示されます。ウィンドウのサイズ変更でJavascriptを無効/有効にする方法はありますか?

<script> 
if ($(window).width() <= 1200) { 
}else{ 


$('nav').addClass('original').clone().insertAfter('nav').addClass('cloned').css('position','fixed').css('top','0').css('margin- top','0').css('z-index','500').removeClass('original').hide(); 

scrollIntervalID = setInterval(stickIt, 10); 


function stickIt() { 

var orgElementPos = $('.original').offset(); 
orgElementTop = orgElementPos.top;    

if ($(window).scrollTop() >= (orgElementTop)) { 

as original element.  
orgElement = $('.original'); 
coordsOrgElement = orgElement.offset(); 
leftOrgElement = coordsOrgElement.left; 
widthOrgElement = orgElement.css('width'); 
    th',widthOrgElement).show(); 
$('.original').css('visibility','hidden'); 
} else { 
$('.cloned').hide(); 
$('.original').css('visibility','visible'); 
} 
} 
</script> 
+0

ウィンドウ幅チェックすることによって、それを行うことができます(関数(){...});' –

答えて

1

は、JavaScriptイベント「をリサイズする」にイベントハンドラをバインドすることができます。

$(window).resize(function() { 

    if($(window).width() <= 1200) { 
     //You code here 
    }else { 
     //You code here 
    } 

}); 

ブラウザウィンドウのサイズが変更されるたびにあなたのコードが実行されます。

1
$(window).resize(function() {   
     if($(window).width() <= 1200) { 
      //small screen code 
     }else { 
      //large screen code   } 

    }); 

    //trigger window resize event on load 
    $(window).trigger('resize'); 
1

あなたはあなたが$(ウィンドウ) `.resize使用することができます

var winWidth = $(window).width(); // you can get the window width from this 

//Then check with the condtion(compare with your need ex :) 



    if(winWidth <= 600) 
    { 
    //your work here 
    alert("window resize less than or equal to 600"); 
    } 
    else 
    { 
    //your work here 
    alert("window resize greater than to 600"); 
    } 
関連する問題