gpt4 book ai didi

javascript - 使用 color-thief 从图像中获取背景颜色

转载 作者:行者123 更新时间:2023-11-29 21:58:13 25 4
gpt4 key购买 nike

我正在编写一个脚本,使用 color-thief.js 从图像中获取主色,然后将该颜色用作背景色。我以前从未使用过这个插件,所以我不知道我到底做错了什么。但是背景颜色没有应用。

$(document).ready(function($) {
$(".box img").load(function(){
var dominantColor = getDominantColor($(this));
$(this).parent().css("background-color", "rgb("+dominantColor+")");
});
});

提前致谢。

最佳答案

您需要启动 ColorThief 对象以引用其内部方法。

var colorThief = new ColorThief();
$(document).ready(function($) {
$(".box img").load(function(){
var dominantColor = colorThief.getColor($(this));
console.log(dominantColor);
$(this).parent().css("background-color", "rgb("+dominantColor+")");
});
});

关于javascript - 使用 color-thief 从图像中获取背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329648/

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