简体   繁体   English

创建JavaScript函数来切换精灵

[英]create javascript function to toggle sprite

I am working on creating a JS/jQuery function to toggle the position of an icon sprite. 我正在创建一个JS / jQuery函数来切换图标精灵的位置。 So far I have gotten this code to work: 到目前为止,我已经获得了以下代码:

$('.toggle-completed').mouseup(function(){
var sp = ($(this).css('background-position'));
$(this).css('background-position', 
    sp = sp == '-82px 50%'? '-101px 50%' : '-82px 50%'
);
});

However I want to abstract this so that it can work with any sprite pair. 但是,我想对此进行抽象,使其可以与任何精灵对一起使用。 Something like this: 像这样:

var toggleSprite = function(firstPosition, secondPosition){
var sp = ($(this).css('background-position'));
$(this).css('background-position',
    sp = sp == firstPosition ? secondPosition : firstPosition
);
};

Which would be called as follows: 可以这样称呼:

toggleSprite('-82px 50%', '-101px 50%');

However this code generates an error: 但是,此代码会产生错误:

Uncaught TypeError: Cannot use 'in' operator to search for 
'backgroundPosition' in undefined 

I am thinking that either 1. I can't really use 'this' in a function. 我在想两者之一。1.我不能真正在函数中使用“ this”。 Or perhaps I am not calling the function correctly. 也许我没有正确调用该函数。

Thank you. 谢谢。

Inside of a jQuery callback, this often refers to the DOM element that triggered the event, so can be used in a selector ( $(this) ), but in your example that creates an error, the context is a vanilla js function, so this refers to the current scope, not a DOM object. 在jQuery回调内部, this通常是指触发事件的DOM元素,因此可以在选择器( $(this) )中使用,但是在您创建错误的示例中,上下文是普通js函数,因此this是指当前作用域,而不是DOM对象。

Perhaps extend the jQuery fn object... 也许扩展jQuery fn对象...

jQuery.fn.toggleSprite = function(firstPosition, secondPosition){
var sp = ($(this).css('background-position'));
$(this).css('background-position',
    sp = sp == firstPosition ? secondPosition : firstPosition
);
// also, return $(this) so that chaining is not broken...
return $(this);
};

And call like this... 像这样打电话

$('.some.selector').toggleSprite('-82px 50%', '-101px 50%');

So that the function knows what this refers to. 这样,函数就知道this是指什么。

The problem seems to be in the is the this reference... it does not looks like to be referencing the target element. 问题似乎出在this引用...看起来不像是在引用目标元素。

I think you can write it as a simple plugin like 我认为您可以将其编写为一个简单的插件,例如

$.fn.toggleSprite = function (firstPosition, secondPosition) {
    return $(this).css('background-position',    function(idx, sp){
        return sp = sp == firstPosition ? secondPosition : firstPosition;
    });
};

then call it like 然后像

$(this).toggleSprite('pos1', 'pos2');//or $(el)

or pass the elements as a param to the method 或将元素作为参数传递给方法

var toggleSprite = function (els, firstPosition, secondPosition) {
    $(els).each(function () {
        var sp = $(this).css('background-position');
        $(this).css('background-position', sp = sp == firstPosition ? secondPosition : firstPosition);
    });
};

then 然后

toggleSprite(myels, 'pos1', 'pos2')

A third option is to pass a custom context to the toggle method using Function.call() 第三种选择是使用Function.call()将自定义上下文传递给toggle方法。

toggleSprite('-82px 50%', '-101px 50%').call(myel);

The problem you're having has to do with the fact that the function context this is not bound to a specific DOM element when used in a generic (ie non-jQuery) function. 您遇到的问题与事实的功能方面做this在一般使用时不绑定到特定的DOM元素(即非jQuery的)功能。 One of the more powerful features of the jQuery library is that the function context this is bound to the DOM element that calls the jQuery function, allowing the user to easily target a specific element. 其中一个jQuery库的更强大的功能是函数上下文this势必会调用jQuery的功能,让用户能够轻松地针对特定元素的DOM元素。 In regular JavaScript, this refers to the current scope, which in your case is the function toggleSprite . 在常规JavaScript中, this是指当前作用域,在您的情况下是函数toggleSprite

For your purposes, I think a simple jQuery plugin function would do the trick: 为了您的目的,我认为一个简单的jQuery插件函数就可以解决问题:

$.fn.toggleSprite = function(pos1, pos2) {
  return $(this).css('background-position', function(_, sp){
    return sp == pos1 ? pos2 : pos1;
  });
};

The benefit of this method is that you can then call this function on any jQuery object as you normally would: 此方法的好处是您可以像往常一样在任何jQuery对象上调用此函数:

$('.sprite').toggleSprite('-82px 50%', '-101px 50%');

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

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