How to disable JavaScript in media query

戏子无情 提交于 2019-12-19 04:13:16

问题


I want to dissable javascripts in media query.

@media only screen
 (max-device-width : 568px) {
.sidebar { display: none; } 
  #navmenu {  margin-top:0px; width:auto; padding:10px; margin-left:15%;}
.content {width:auto; height:auto; margin-left:0; margin-top:3%;}
.contentsing{position:relative; width:100%; height:auto; margin-left:0%; margin-top:-150px;}


}

That is mu code, now i want to add something that will disable scripts.


回答1:


You could wrap your script in an if statement that checks how wide the screen is like this:

if(window.innerWidth > 568){
    ...execute script
}

However, that will only execute once, so what if you resize your browser window? You could have an event listener that executes your script whenever you resize the browser.

window.addEventListener('resize', function(){
    if(window.innerWidth > 568){
        ...execute script
    }
});



回答2:


When you use jQuery - this code will help you

$(window).resize(function() {
    if ($(window).width()>992){
        ...execute script
    }
});



回答3:


this would work better....

if ($(window).width()>568){
    ...execute script
}


来源:https://stackoverflow.com/questions/20659338/how-to-disable-javascript-in-media-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!