Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery调整大小和文档就绪组合_Javascript_Jquery_Resize - Fatal编程技术网

Javascript jQuery调整大小和文档就绪组合

Javascript jQuery调整大小和文档就绪组合,javascript,jquery,resize,Javascript,Jquery,Resize,我有这个JS函数来删除屏幕大小可靠的类。它起作用了 但是,只有在调整屏幕大小时(我相信这是预期的行为),我也需要让它在加载时工作 require(['jquery'], function(){ jQuery(window).resize(function() { var innerWidth = window.innerWidth; if (innerWidth < 800) { jQuery("#logo-container

我有这个JS函数来删除屏幕大小可靠的类。它起作用了 但是,只有在调整屏幕大小时(我相信这是预期的行为),我也需要让它在加载时工作

require(['jquery'], function(){
    jQuery(window).resize(function() {
        var innerWidth = window.innerWidth;
        if (innerWidth < 800) {
            jQuery("#logo-container").removeClass('pull-left');
        } else if (innerWidth > 800) {
            jQuery("#logo-container").addClass('pull-left');
        }
    });
});
require(['jquery'],function()){
jQuery(窗口).resize(函数(){
var innerWidth=window.innerWidth;
如果(内部宽度<800){
jQuery(“logo容器”).removeClass('pull-left');
}否则,如果(内部宽度>800){
jQuery(“#logo container”).addClass('pull-left');
}
});
});
我用document.ready包装了函数,并在resize事件之前添加了相同的内容。现在有这样的东西:

require(['jquery'], function(){
    jQuery(document).ready(function() {
        var innerWidth = window.innerWidth;
        if (innerWidth < 800) {
            jQuery("#logo-container").removeClass('pull-left');
        } else if (innerWidth > 800) {
            jQuery("#logo-container").addClass('pull-left');
        }
        jQuery(window).resize(function() {
            var innerWidth = window.innerWidth;
            if (innerWidth < 800) {
                jQuery("#logo-container").removeClass('pull-left');
            } else if (innerWidth > 800) {
                jQuery("#logo-container").addClass('pull-left');
            }
        });
    });
});
require(['jquery'],function()){
jQuery(文档).ready(函数(){
var innerWidth=window.innerWidth;
如果(内部宽度<800){
jQuery(“logo容器”).removeClass('pull-left');
}否则,如果(内部宽度>800){
jQuery(“#logo container”).addClass('pull-left');
}
jQuery(窗口).resize(函数(){
var innerWidth=window.innerWidth;
如果(内部宽度<800){
jQuery(“logo容器”).removeClass('pull-left');
}否则,如果(内部宽度>800){
jQuery(“#logo container”).addClass('pull-left');
}
});
});
});
现在,我的函数的结果就是我想要的,但是,我觉得我在重复我的代码

这是正确的方法吗?有更好的替代方法吗?


如果您有任何建议,我们将不胜感激。

请记住,如果您需要复制和粘贴完全相同的代码块,那么最好将其重构为函数调用:

require(['jquery'], function(){
    jQuery(document).ready(function() {
        jQuery(window).resize(function() {
            toggleClass();
        });
        toggleClass();
    });

    function toggleClass() {
        var innerWidth = window.innerWidth;
        if (innerWidth < 800) {
            jQuery("#logo-container").removeClass('pull-left');
        } else if (innerWidth > 800) {
            jQuery("#logo-container").addClass('pull-left');
        }
    }
});
require(['jquery'],function()){
jQuery(文档).ready(函数(){
jQuery(窗口).resize(函数(){
toggleClass();
});
toggleClass();
});
函数toggleClass(){
var innerWidth=window.innerWidth;
如果(内部宽度<800){
jQuery(“logo容器”).removeClass('pull-left');
}否则,如果(内部宽度>800){
jQuery(“#logo container”).addClass('pull-left');
}
}
});

避免重复代码。

制作一个函数并调用文档准备函数和窗口调整函数

在下面的代码中,所有代码都将转到screenResized()函数中的

require(['jquery'],function()){
jQuery(文档).ready(函数(){
OnScreenResized();
});
jQuery(窗口).resize(函数(){
OnScreenResized();
});
函数OnScreenResized(){
var innerWidth=window.innerWidth;
如果(内部宽度<800){
jQuery(“logo容器”).removeClass('pull-left');
}否则,如果(内部宽度>800){
jQuery(“#logo container”).addClass('pull-left');
}
}
});

您可以通过创建新函数并在每个条件下调用它来调整大小,以防止重新编写所有语句并调用一个函数..我明白了!现在我得到答案了!谢谢你的帮助。啊,太好了!谢谢你的帮助和建议!太好了!谢谢你的帮助和建议。
require(['jquery'], function() {
      jQuery(document).ready(function() {
        OnScreenResized();

      });

      jQuery(window).resize(function() {
        OnScreenResized();
      });

      function OnScreenResized() {
        var innerWidth = window.innerWidth;

        if (innerWidth < 800) {
          jQuery("#logo-container").removeClass('pull-left');
        } else if (innerWidth > 800) {
          jQuery("#logo-container").addClass('pull-left');
        }
      }
    });