简体   繁体   English

jQuery如果宽度小于767,则先前的代码不起作用

[英]jQuery if width is less than 767 the previous code should not work

Problem: I want that the code in the if statement would work only if the screen width is greater than 767px, and if it is less than 767px the previous code shouldn't work, how to do it? 问题:我希望if语句中的代码仅在屏幕宽度大于767px时才起作用,而如果小于767px,则先前的代码不起作用,怎么办?

jQuery: jQuery的:

$(document).ready(function() {
    $(window).resize(function() {
        if ($(document).width() > 767) {
    $('.navigation-hide').hide();

   $("nav").mouseenter(function() {    
       $('.navigation-hide').stop().fadeIn(500);       
   });

   $('nav').mouseleave(function() {    
        $('.navigation-hide').stop().fadeOut(500);         
   });
}
        else {
            break;
        }
    });

});

It's not a good idea to use JS in this way. 以这种方式使用JS不是一个好主意。

You can use CSS to show/hide elements depending on screen size using media queries . 您可以使用CSS 使用媒体查询根据屏幕尺寸显示/隐藏元素。

@media (max-width: 768px) {
  .navigation-hide {
      display: none;
  }
  nav:hover .navigation-hide {
      display: block;
  }
}

You can use CSS transitions to achieve the fade effect. 您可以使用CSS过渡来实现淡入淡出效果。

Check out this jsFiddle 看看这个jsFiddle

.navigation-hide {
    position: absolute;
    left: -999em;
    top: 0;
    opacity: 0;
    transition: opacity 0.5s, left 0.5s 0.5s;
}

nav:hover .navigation-hide {
    left: 0;
    opacity: 1;
    transition: opacity 0.5s;
}

It should be achieved using CSS's media queries and effects, see this https://jsfiddle.net/DTcHh/12782/ 应该使用CSS的媒体查询和效果来实现,请参阅https://jsfiddle.net/DTcHh/12782/

however if you insist using jQuery, then check this out 但是,如果您坚持使用jQuery,请检查一下

$(document).ready(callbackReady);
$(window).resize(callbackResize);

var isWidthReady = false;
var callbackReady = function() {
    $("nav").mouseenter(function() {
        if(isWidthReady) {
            $('.navigation-hide').stop().fadeIn(500);     
        }           
    });
    $('nav').mouseleave(function() {
        if(isWidthReady) {
            $('.navigation-hide').stop().fadeOut(500);  
        }
    });
}
var callbackResize = function() {
    if ($(document).width() > 767) {
        $('.navigation-hide').hide();
        isWidthReady = true;
    }
    else {
        isWidthReady = false;
    }
};

Note: do not implement any event handler inside another recurring event (resize() in your case) unless you are creating a new element dynamically or removing and reattaching an event. 注意:除非要动态创建新元素或删除并重新附加事件,否则请勿在另一个重复发生的事件(在您的情况下为resize())内实现任何事件处理程序。

Use an if statement with window.innerWidth! 在window.innerWidth中使用if语句!

    $(document).ready(function() {
if(window.innerWidth >= 767){
 $(window).resize(function() { if ($(document).width() > 767) { $('.navigation-hide').hide(); $("nav").mouseenter(function() { $('.navigation-hide').stop().fadeIn(500); }); $('nav').mouseleave(function() { $('.navigation-hide').stop().fadeOut(500); }); } else { break; } }); }});

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

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