简体   繁体   English

如何缩短这些jQuery函数?

[英]How to shorten these jQuery functions?

I have these 3 jQuery functions that basically do the same thing, but they are a tiny bit different (width of window, and class that is removed/toggled) 我有这3个jQuery函数,它们基本上可以完成相同的操作,但是它们略有不同(窗口的宽度以及要删除/切换的类)

I need the functionality of all these 3 functions, but want to somehow combine them/shorten them into one function. 我需要所有这三个功能的功能,但是想要以某种方式将它们组合/将它们缩短为一个功能。 I've tried to but my code keeps breaking Can anyone help to shorten them? 我尝试过但我的代码不断出错,有人可以帮助缩短它们吗?

Here are the 3 functions 这是3个功能

jQuery(document).ready(function($) {
  $('.exampleimg').click(function() {
    $('.about').hide(600);
    if (($(window).width() > 670) && ($(this).hasClass('exampleimgopen'))) {
      $(this).removeClass('exampleimgopen');
    } else if ($(window).width() > 670) {
      $('.exampleimg').removeClass('exampleimgopen');
      $(this).addClass('exampleimgopen');
    }
  });
});

jQuery(document).ready(function($) {
  $('.exampleimg').click(function() {
    $('.about').hide(600);
    if (($(window).width() < 670) && ($(this).hasClass('exampleimgopen2'))) {
      $(this).removeClass('exampleimgopen2');
    } else if ($(window).width() < 670) {
      $('.exampleimg').removeClass('exampleimgopen2');
      $(this).addClass('exampleimgopen2');
    }
  });
});

jQuery(document).ready(function($) {
  $('.exampleimg').click(function() {
    $('.about').hide(600);
    if (($(window).width() < 540) && ($(this).hasClass('exampleimgopen3'))) {
      $(this).removeClass('exampleimgopen3');
    } else if ($(window).width() < 540) {
      $('.exampleimg').removeClass('exampleimgopen3');
      $(this).addClass('exampleimgopen3');
    }
  });
});

I need the functionality of all these 3 functions, but want to somehow combine them/shorten them into one function. 我需要所有这三个功能的功能,但是想要以某种方式将它们组合/将它们缩短为一个功能。

Generally, a good approach when refactoring similar functions is to create a single factory function that will take your variable data as arguments and return a function that has access to that data via its closure. 通常,重构相似函数时的一种好方法是创建一个工厂函数,该函数将您的变量数据作为参数并返回一个可以通过其闭包访问该数据的函数。

function myFactory(conditionFunc, windowWidth, cssClass) {
  return function() {
    $('.about').hide(600);
    var windowCondition = conditionFunc($(window).width(), windowWidth);
    if (windowCondition && ($(this).hasClass(cssClass))) {
      $(this).removeClass(cssClass);
    } else if (windowCondition) {
      $('.exampleimg').removeClass(cssClass);
      $(this).addClass(cssClass);
    }
  }
}

Then you can call this function 3 times to build your functions: 然后,您可以调用此函数3次以构建函数:

// helper methods that will perform '<' or '>' on window width
var lessThan = function(a, b) { return a < b; };
var greaterThan = function(a, b) { return a > b; };

var func1 = myFactory(greaterThan, 670, 'exampleimgopen');
var func2 = myFactory(lessThan, 670, 'exampleimgopen2');
var func3 = myFactory(lessThan, 540, 'exampleimgopen3');

Which you can then pass each into their corresponding listeners. 然后,您可以将它们传递给各自的相应侦听器。

$('.exampleimg').click(func1);
$('.exampleimg').click(func2);
$('.exampleimg').click(func3);

The advantage of doing things this way is that you only write a single function which then creates different versions of your listener callback functions, based on the data you give to it. 这样做的好处是,您只编写一个函数,然后根据提供给它的数据创建不同版本的侦听器回调函数。

I think is maybe closer to what you wanted, although it's unclear what you wanted to have happen when the width was < 540. Might want to use if .. then ... else instead 我认为也许更接近您想要的东西,尽管不清楚宽度小于540时您想要发生什么if .. then ... else可能想使用

jQuery(document).ready(function($) {
    $('.exampleimg').click(function() {
        var width = $(window).width();
        var classes = [];
        if (width < 540) {
            classes.push('exampleimgopen3');
        }
        if ($(window).width() < 670) {
            classes.push('exampleimgopen2');
        }
        if ($(window).width() >= 670) {
            classes.push('exampleimgopen');
        }
        classes.forEach(function(class) {
            $('.exampleimg').removeClass(class);
            if (!$(this).hasClass(class)) {
                $(this).addClass(class);
            }
        });
    });
});

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

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