草庐IT

javascript - 在 angularJS 中交换颜色的可逆函数

coder 2025-01-20 原文

我有一个返回颜色数组的函数:

default: function () {

    // Get our colours array
    var colours = [service.kits.kit.colour1, service.kits.kit.colour2, service.kits.kit.colour3];

    // If our third colour is blank
    if (!colours[2]) {

        // Is our first or second colour white
        var isWhite = colours[0] === 'ffffff' || colours[1] === 'ffffff';

        // Set our thrid colour
        colours[2] = isWhite ? 'a6a6a6' : 'ffffff';
    }

    // Return our colours
    return colours;
},

然后我这样使用:

// Checks to see what colour was selected for Colour 1 and 2 and sets the 3rd colour
var setSelectedColours = function () {

    // Get our default colours
    var colours = service.colours.default();

    // Apply to our selected colours
    service.colours.selected = angular.copy(colours);
    service.colours.selectedLeisure = angular.copy(colours);
};

我有一个用于交换颜色的指令,如下所示:

.directive('colourSwapper', function () {

    return {
        restrict: 'A',
        controller: 'ColourSwapperController',
        link: function (scope, element, attrs, controller) {

            // Get to see if we are working with the leisure colours or not
            var leisurewear = attrs.hasOwnProperty('leisurewear');

            // Create our function
            element.bind('click', function (e) {

                // Apply the scope
                scope.$apply(function () {

                    // Use the algorithm for leisurwear items
                    controller.swapColours(leisurewear);
                });

                // Prevent any other actions
                e.preventDefault();
            });
        }
    };
})

到目前为止非常简单。 交换颜色的功能保存在我的 Controller 中,如下所示:

.controller('ColourSwapperController', ['ConfiguratorService', function (shared) {
    var self = this;

    // Assign our private properties
    var defaultColours = shared.colours.default(),
        tempColour,
        leisureColour = '000e31',
        leisureColours = [leisureColour, '001444'];

    // Used to swap the colours for a garment
    self.swapColours = function (leisurewear) {

        // Get our colours
        var colours = leisurewear ? shared.colours.selectedLeisure : shared.colours.selected;

        // If we are leisurewear and colour 1 and 2 are not navy and we don't have a temp colour
        if (leisurewear && leisureColours.indexOf(defaultColours[0]) === -1 && leisureColours.indexOf(defaultColours[1]) === -1 && !tempColour) {

            // Assign our first colour to our temp colour
            tempColour = colours[0];

            // Make our first colour our 
            colours[0] = leisureColour;

            // If we are playingwear or we have a tempColour
        } else {

            // If we have a tempColour
            if (tempColour) {

                // Set our first colour to our tempColour
                colours[0] = tempColour;

                // Reset our tempColour
                tempColour = null;
            }

            // If our last colour is the hightlight colour and we have not swapped, and our first colour is not the same as our second colour
            if (colours[2] === defaultColours[2] && colours[0] === defaultColours[0] && defaultColours[0] !== defaultColours[1]) {

                // Get colour 1
                var c1 = colours.shift();

                // Move it to the middle
                colours.splice(1, 0, c1);

                // If our last colour is our highlight colour and we have already swapped
            } else if (colours[2] === defaultColours[2]) {

                // Get our highlight colour
                var h1 = colours.pop();

                // Move it to the middle
                colours.splice(1, 0, h1);

                // If our highlight colour is in the middle and our first colour is actually our second colour, and we our first colour is not the same as our second colour
            } else if (colours[1] === defaultColours[2] && colours[0] === defaultColours[1] && defaultColours[0] !== defaultColours[1]) {

                // Get colour 1 and 2
                var c1 = colours.pop();
                var c2 = colours.shift();

                // Swap our colours
                colours.splice(0, 0, c1);
                colours.push(c2);

                // If our hightlight colour is in the middle and our first colour is in the original place, move our hightlight colour back to the last position
            } else if (colours[1] === defaultColours[2]) {

                // If we have a tempColour
                if (tempColour) {

                    // Set our first colour to our tempColour
                    colours[0] = tempColour;

                    // Reset our tempColour
                    tempColour = null;
                }

                // Get colour 2
                var c2 = colours.pop();

                // Insert c2 into the middel
                colours.splice(1, 0, c2);
            }
        }
    };
}])

现在,这有点复杂。有3种颜色,可以这样排列,颜色3只能在位置1和2,而颜色1和2可以在任何位置。

如果颜色用于休闲装,则应用相同的规则在位置 0 的每个步骤之间插入另一种颜色(除非该颜色已经被选择并且位于位置 1 或 2) .

我的函数运行良好,但它是线性的,如果按下一个按钮,它将始终向一个方向移动。我“希望”它朝两个方向移动,所以有一个后退和前进按钮可以反转功能。

有谁知道如何对我当前的功能执行此操作?

最佳答案

创建一个存储颜色序列的数组

var colorsOrderSet=[];

当你点击按钮时

var colorChangeNumber = 0;

当它点击时,它应该将信息存储在一个数组中

colorsOrderSet[colorChangeNumber] = colours;

然后将它加一,这样它就会存储这个数字

colorChangeNumber++;

返回函数 将点击

ColorChangeNumber--;
colorsOrderSet[colorChangeNumber] = colours;

关于javascript - 在 angularJS 中交换颜色的可逆函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125379/

有关javascript - 在 angularJS 中交换颜色的可逆函数的更多相关文章

  1. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  2. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  3. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  4. ruby 诅咒颜色 - 2

    如何使用Ruby的默认Curses库获取颜色?所以像这样:puts"\e[0m\e[30;47mtest\e[0m"效果很好。在浅灰色背景上呈现漂亮的黑色。但是这个:#!/usr/bin/envrubyrequire'curses'Curses.noecho#donotshowtypedkeysCurses.init_screenCurses.stdscr.keypad(true)#enablearrowkeys(forpageup/down)Curses.stdscr.nodelay=1Curses.clearCurses.setpos(0,0)Curses.addstr"Hello

  5. ruby - Rails 3 的 RGB 颜色选择器 - 2

    状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基

  6. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  7. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  8. ruby-on-rails - 将字符串转换为 ruby​​-on-rails 中的函数 - 2

    我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。

  9. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  10. ruby - 在 ruby​​ 中使用 .try 函数和 .map 函数 - 2

    我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法

随机推荐