【问题标题】:This jQuery each() loop keeps returning the last color only (Color Thief)这个 jQuery each() 循环只返回最后一种颜色(颜色小偷)
【发布时间】:2017-01-03 01:35:59
【问题描述】:

使用Color Thief javascript 效果,我编写了一些代码来抓取图像的主色并根据图像调整整个主题的配色方案。

这一切都适用于单个产品页面,其中仅使用 一个 图像。在我的目录页面上,有多个图像需要从中获取主色。每个产品一张图片。我需要将每种单独的颜色与产品一起显示。


您可以在每个产品的面板中看到彩色的border(它是棕色/橙色)。


我正在使用的非常精简的代码如下:

jQuery( document ).ready( function( $ ) {
    var image = new Image;
    var bg;
    $('.post-image-hidden-container').each(function() {
        bg = $(this).text();

        image.onload = function() {
            var colorThief = new ColorThief();
            var dominantColor = colorThief.getColor(image);
            var colorPalette = colorThief.getPalette(image, 7);
            var backgroundColor = 'rgb('+ dominantColor +')';

            /* Calculate the Lightest Color in the Palette */
            var lightestColor = colorPalette.reduce(function(previousValue, currentValue) {
                var currLightNess = (0.2126*currentValue[0] + 0.7152*currentValue[1] + 0.0722*currentValue[2]);
                var prevLightNess = (0.2126*previousValue[0] + 0.7152*previousValue[1] + 0.0722*previousValue[2]);
                return (prevLightNess < currLightNess) ? currentValue : previousValue;
            });

            /* Calculate the Darkest Color in the Palette */
            var darkestColor = colorPalette.reduce(function(previousValue, currentValue) {
                var currLightNess = (0.2126*currentValue[0] + 0.7152*currentValue[1] + 0.0722*currentValue[2]);
                var prevLightNess = (0.2126*previousValue[0] + 0.7152*previousValue[1] + 0.0722*previousValue[2]);
                return (prevLightNess > currLightNess) ? currentValue : previousValue;
            });

            /* Create Shades and Tints of Lightest Color */
            ...

            /* Shades (darker) */
            ...

            /* Tints (lighter) */
            ...

            /* Handle all CSS based on returned colors */
            $('.product-bottom-info-container').each(function() {
                $(this).css({
                    borderTop: '3px solid rgb('+ lightestColor +')'
                });
            });
        }
        image.src = bg;
    });
});

在声明变量bg 之后,我将整个内容包装在each() 循环中。向下到底部,.product-bottom-info-container 是出现彩色边框的元素。我似乎无法让每个产品的边框成为它自己的颜色。它不断给每个边框循环中的最后一种颜色。

一些注意事项:

  • .post-image-hidden-container 是每个产品上方的隐藏 div,包含产品的图片网址。
  • .product-bottom-info-container 是每个产品底部的容器,带有产品标题和彩色边框。

我是否正确使用了each() 函数?我做错了什么?

谢谢


更新 我能够获取每个图像的所有 RGB 值并将它们放入一个数组中:

var thisColor;
var theseColors = [];

$('.shop-page-item-thumb').each(function() {
    $(this).find('img').each(function() {
        thisColor = colorThief.getColor(this);
        theseColors.push(thisColor);
    });
});

现在我已经有了所有可用的 RGB,有没有办法简单地遍历这个数组并将每个值分配给它各自的 .product-bottom-info-container 元素?

theseColors[0] 是第一个 RGB,theseColors[1] 是第二个,依此类推,一直到 theseColors[11]

如果我在循环内运行console.log(thisColor),我会得到以下结果:

[29, 28, 22]
[217, 195, 189]
[14, 14, 8]
[233, 232, 206]
[31, 31, 31]
[82, 97, 111]
[60, 68, 84]
[34, 29, 30]
[17, 30, 37]
[12, 11, 12]
[56, 43, 26]
[209, 150, 108]

我需要的 12 个 RGB 值。所以我们正朝着正确的方向前进。


Mottie 更新 这是其中一种产品的 HTML 结构。 .shop-page-item-thumb 是保存缩略图的容器,但 .shop-page-item-article 是父级(除了实际的 li 列表项之外)。


最终更新(感谢 Mottie!) 这段代码终于奏效了:

$('.shop-page-item-thumb').each(function() {
    var thumb = $(this);
    thumb.find('img').each(function() {
        thisColor = colorThief.getColor(this);
        thumb.parent().find('.product-bottom-info-container').css({
            borderTop: '3px solid rgb('+ thisColor +')'
        })
    });
});

非常喜欢,堆栈溢出

【问题讨论】:

    标签: javascript jquery each color-thief


    【解决方案1】:

    看起来这段代码在每次图像加载后循环遍历每个容器......

    /* Handle all CSS based on returned colors */
    $('.product-bottom-info-container').each(function() {
      $(this).css({
        borderTop: '3px solid rgb('+ lightestColor +')'
      });
    });
    

    试试这个:

    $(this).closest('.product-bottom-info-container').css({
      borderTop: '3px solid rgb('+ lightestColor +')'
    });
    

    更新:哦,抱歉,我没有仔细查看代码...另一个问题是image 定义。每个容器只有一个,而不是一个...而不是在循环之外定义它,而是在.each循环内部找到它,然后附加一个onload...

    $(this).find('img')[0].onload = function() {
    

    不应更改上述添加边框颜色的代码,因为this 将引用onload 函数内部的图像。

    如果您提供一个现成的演示来使用,解决问题会更容易。


    Update2:不是将颜色推送到数组,而是直接将它们应用到边框;我不知道与容器相关的拇指图像,所以我们只是假设拇指与容器的顺序相同。 一些 HTML 会有所帮助。针对给定的 HTML 进行了更新...

    $('.shop-page-item-thumb').each(function() {
        var thumb = $(this);
        thumb.find('img').each(function() {
            thisColor = colorThief.getColor(this);
            // prev() targets the previous element (it should be
            // the 'product-bottom-info-container'; or use
            // thumb.parent().find('.product-bottom-info-container')
            thumb.prev().css({
                borderTop: '3px solid rgb('+ thisColor +')'
            });
        });
    });
    

    【讨论】:

    • 嗨,莫蒂,感谢您的回复。不幸的是仍然无法正常工作。没有收到任何错误,现在根本没有颜色。我还要玩这个。 :)
    • Mottie,在您更新之前,我的方向是正确的。问题 - 我能够将所有 RGB 值放入一个数组中。然后我可以简单地为每个 .product-bottom-info-container 边框颜色使用每个数组值吗?我会用我更新的代码更新我的问题。我也会尝试实施您的建议。谢谢!
    • 我会为您制作一个演示,但 Color Thief 很时髦,并且不允许在脚本中使用远程图像。仅限本地图片。
    • 莫蒂,我无法让它工作。我已经尝试过您的建议,但我什至无法通过这种方式在控制台中获取任何内容。 Color Thief 是一头野兽,文档非常有限。但我们最终会弄清楚的!谢谢
    • 我已经修改了上一次更新...希望你明白,我今晚要退出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2018-04-14
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多