我正在编写一个脚本,使用 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/