任何人都可以请一个狡猾的if语句提供帮助,请尝试一下,因为我可能看不到有什么问题,该代码可以在if语句之外使用:

var sizeBefore;
var sizeAfterResize;
var sizeBreakPoint;
var resizeTimer;
$(function() {
    sizeBefore = $(window).width();
    sizeBreakPoint = 500;
    doneResizing();
});

function doneResizing(){
       sizeAfterResize = $(window).width();
       if ((sizeBefore < sizeBreakPoint) && (sizeAfterResize >= sizeBreakPoint)) {
                alert('Before:' + sizeBefore);
                alert('After:' + sizeAfterResize);
                sizeBefore = sizeAfterResize;
                // THIS RUNS WHEN OUTSIDE IF STATEMENT
       }
}


$(window).resize(function() {
    //doneResizing();
     clearTimeout(resizeTimer);
     resizeTimer = setTimeout(doneResizing, 100);
});


编辑:仅当窗口从小于sizeBreakPoint调整为大于sizeBreakPoint的大小时,此声明才应在文档准备就绪或调整大小完成时为真-希望这可以弄清楚

编辑:解决方案

var sizeBefore;
var sizeAfterResize;
var sizeBreakPoint;
var resizeTimer;
$(function() {
    sizeBefore = $(window).width();
    sizeBreakPoint = 500;
    doneResizing();
});

function doneResizing(){
       sizeAfterResize = $(window).width();
       if ((sizeBefore < sizeBreakPoint) && (sizeAfterResize >= sizeBreakPoint)) {
                alert('Before:' + sizeBefore);
                alert('After:' + sizeAfterResize);
       }
       sizeBefore = sizeAfterResize;
}


$(window).resize(function() {
     clearTimeout(resizeTimer);
     resizeTimer = setTimeout(doneResizing, 100);
});

最佳答案

好的,可以查看并测试您是否可以说:

if ((sizeBefore < sizeBreakPoint) && (sizeAfterResize >= sizeBreakPoint))


仅当窗口的起始尺寸小于500时,并且窗口大小超过500时,才重新调整尺寸。这是正确的,因为您从不更改sizeBefore,然后再加载页面。

您能告诉我您真正想要验证的内容吗?或者,如果这是您真正想要的。

**编辑:建议的解决方案**

尝试做这样的事情:

$(function() {
   sizeBreakPointMin = 480;
   sizeBreakPointMax = 520;
   doneResizing();
});

function doneResizing(){
   sizeAfterResize = $(window).width();
   //this way you have a window where this is false and it's as you want to allow the window as in a fine size
   //you can garantie that the window is always bigger then 480 and smaller then 520
   if ((sizeAfterResize < sizeBreakPointMin) || (sizeAfterResize >= sizeBreakPointMax)) {
            alert('Before:' + sizeBefore);
            alert('After:' + sizeAfterResize);
            // THIS RUNS WHEN OUTSIDE IF STATEMENT
   }
}


从您的代码:

 if ((sizeBefore < sizeBreakPoint) && (sizeAfterResize >= sizeBreakPoint)) {
            ...
            sizeBefore = sizeAfterResize;
 }


这两行并不能很好地配合,因为第一次是这样,您只是想保证sizeAfterResize >= sizeBreakPoint使得下次检查sizeBefore是否总是大于sizeBreakPoint使得if语句不可能请按原样输入2次(如果您不更改if之外的sizeBefore

希望这可以帮助。

关于javascript - 响应式JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15160983/

10-12 06:52