javascript - 如何增强这个小功能

标签 javascript jquery

我们如何将这两者合并在一起以增强功能并使其更易于编辑。

顶部的一个是在屏幕调整大小时触发功能,另一个是在加载时检测屏幕尺寸。

整个函数:

(function(){
  //detect the width on page load//
  $(document).ready(function(){
    var width = $(window).width();
    if(width <= 770){

        $('#home-sectionB img').insertBefore($('#home-sectionB span'));
        $('.detailsBox').insertBefore($('.imagesGrid'));
        } else {
        $('#home-sectionB span').insertBefore($('#home-sectionB img')); 
        $('.imagesGrid').insertBefore($('.detailsBox'));
    }
  });
  //update the width value when the browser is resized//
  $(window).resize(function(){
    var width = $(window).width();
    if(width <= 770){

        $('#home-sectionB img').insertBefore($('#home-sectionB span'));
        $('.detailsBox').insertBefore($('.imagesGrid'));
        } else {
        $('#home-sectionB span').insertBefore($('#home-sectionB img')); 
        $('.imagesGrid').insertBefore($('.detailsBox')); 
    }
  });
})(jQuery);

顶部:

    (function(){
    //detect the width on page load//
     $(document).ready(function(){
    var width = $(window).width();
    if(width <= 770){

        $('#home-sectionB img').insertBefore($('#home-sectionB span'));
        $('.detailsBox').insertBefore($('.imagesGrid'));
        } else {
        $('#home-sectionB span').insertBefore($('#home-sectionB img')); 
        $('.imagesGrid').insertBefore($('.detailsBox'));
    }
  });

底部:

  //update the width value when the browser is resized//
  $(window).resize(function(){
    var width = $(window).width();
    if(width <= 770){

        $('#home-sectionB img').insertBefore($('#home-sectionB span'));
        $('.detailsBox').insertBefore($('.imagesGrid'));
        } else {
        $('#home-sectionB span').insertBefore($('#home-sectionB img')); 
        $('.imagesGrid').insertBefore($('.detailsBox')); 
    }
  });
})(jQuery);

最佳答案

如果它们完全相同,只需创建一个单独的函数并在这些事件上调用它。

$(function(){
  //detect the width on page load//
  $(document).ready(handleResize);  // Notice you're already in the ready event
                                    // on this line so you can just call it here

  //update the width value when the browser is resized//
  $(window).resize(handleResize);
});

function handleResize(){
    var width = $(window).width();
    if(width <= 770){

        $('#home-sectionB img').insertBefore($('#home-sectionB span'));
        $('.detailsBox').insertBefore($('.imagesGrid'));
        } else {
        $('#home-sectionB span').insertBefore($('#home-sectionB img')); 
        $('.imagesGrid').insertBefore($('.detailsBox'));
    }
}

关于javascript - 如何增强这个小功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629698/

相关文章:

javascript - AngularJS 在 Controller 中共享函数/公共(public)代码

javascript - 如何通过删除 "#"使 Angular URL 变得更清晰?

javascript - 单击图像 jQuery - 函数在附加后不起作用

javascript - 在 fullpage.js 中重新激活自动滚动

Javascript 替换不影响所有数组元素

javascript - Webpack 通天塔 6

javascript - 在具有仪表板和登陆的 React 应用程序中处理 CSS 样式冲突的最佳方法是什么?

javascript - Bootstrap Accordion - 切换所有嵌套元素

javascript - JsTree,限制搜索深度

javascript - Django for 循环列表与 jquery 和 javascript