简体   繁体   English

在特定屏幕宽度下删除JS功能

[英]Remove JS function at certain screen width

I am trying to remove certain functionality when the screen is at certain widths. 当屏幕处于特定宽度时,我正在尝试删除某些功能。 I was wondering if you could tell me why this doesn't work? 我想知道你能否告诉我为什么这行不通?

http://jsbin.com/ALAYOru/1/edit http://jsbin.com/ALAYOru/1/edit

I remove the 'has-sub-menu' class when the screen goes below 700px but it still executes the mouseover event. 当屏幕低于700px时,我删除了“ has-sub-menu”类,但它仍执行mouseover事件。

Thanks! 谢谢!

Or this. 或这个。

function isWindowSmall()
{
  if(window.innerWidth < 700px) return true;
  else return false;
}




$(".some-btn").on('click',function()
{
  if(isWindowSmall()){
  //do something for small windows
}
else{
  //do something else for big windows
}
});

try this. 尝试这个。

window.onresize = function () {
    if(window.innerWidth < 700) {
        // your code here
    }
}

You can add a function to check the screen size and remove the onmouseover event listener. 您可以添加一个功能来检查屏幕尺寸并删除onmouseover事件侦听器。

function check_screen() {
if(window.innerWidth < 700px) 
{
whateveryourelementiscalled.removeEventListner('onmouseover', //whatever your mouseover function was);
}
} 

That is becauase what this is doing is. 那是因为这样做是在做。

$(".has-sub-menu").mouseenter(function(){
   $(this).css('background-color','red');
}).mouseleave(function(){
   $(this).css('background-color','transparent');
});

This goes and finds all of the elements with the class of .has-sub-menu and then it attaches the event listener, so this listener will be applied forever, what you could do is something like this. 这样就找到了.has-sub-menu类的所有元素,然后附加了事件侦听器,因此该侦听器将永远被应用,您可以做的就是这样。

$("body").on({
  mouseenter: function(){
     $(this).css('background-color','red');
  },
  mouseleave: function(){
     $(this).css('background-color','transparent');
  }
}, '.has-sub-menu');

This will check to see if the element has the class every time it is clicked 这将检查每次单击该元素是否具有该类。

Demo: http://jsbin.com/ALAYOru/6/edit 演示: http//jsbin.com/ALAYOru/6/edit

Note: in the demo, you might have to make the output window bigger than 720px and then refresh to see the proper effect. 注意:在演示中,您可能必须使输出窗口大于720px,然后刷新才能看到适当的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM