gpt4 book ai didi

javascript - 这个 jQuery each() 循环只返回最后一种颜色(Color Thief)

转载 作者:行者123 更新时间:2023-11-29 15:23:26 25 4
gpt4 key购买 nike

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

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


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

enter image description here


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

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,其中包含产品的图片 url。
  • .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 列表项)。

enter image description here


最终更新(感谢 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 +')'
})
});
});

非常喜欢,Stack Overflow! <3

最佳答案

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

/* 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 函数内的图像。

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


更新2:不是将颜色推送到数组,而是直接将它们应用到边框;我不知道缩略图与容器的关系,所以我们假设缩略图与容器的顺序相同。 一些 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 +')'
});
});
});

关于javascript - 这个 jQuery each() 循环只返回最后一种颜色(Color Thief),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41435292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com