2013-03-22 145 views
2

我试图找出一种方法来通过jQuery更改一些文本的颜色。根据图像更改文本颜色

从本质上讲,我有一个随机加载图像的股利。在那里我有div包含一些文本。

我想要发生的事情是,当一个特定的图像被加载时,它将文本颜色从白色交换到黑色。

任何人都指向正确的方向吗?

对于澄清:

我的问题是我怎么觉得这已经加载到一个div然后执行基于泰德结果的功能。例如:

我有2个图像,它们是随机加载到通过backstretch.js。我也有一些文字另一DIV覆盖在顶部的图像:

<!-- my images --> 
image-1.jpg 
image-2.jpg 

<!-- Layout --> 
<div id="imageDIV"></div> 
<div id="textDiv"><p>hello</p></div> 

一旦页面加载和图像已经被加载到div中。检查它是哪一个,然后做一些事情。所以

if { 
    image is image-1.jpg then add a class to #textDiv 
} 

else if { 
    image is image-2.jpg add this class instead 
} 

感谢

+0

我假设你想改变文本黑/白反之亦然的主色,使其能在图像的顶部清晰可见?看到这个问题,与你想要实现的内容有关:http://stackoverflow.com/questions/2541481/get-average-color-of-image-via-javascript – sweetamylase 2013-03-22 17:29:43

+1

图像是如何加载的?你可以添加你的代码吗? – superUntitled 2013-03-22 17:30:45

+0

你总是会从白色文字切换到黑色文字? – 2013-03-22 17:33:49

回答

3

可以使用color-thief 来获取图像

myImage = $('#myImage'); 
    dominantColor = getDominantColor(myImage); 
    paletteArray = createPalette(myImage, 10); // 2nd argument sets th`e # of colors in palette 
+0

我会看看这个 - 谢谢 – Victor 2013-03-22 17:43:37

+0

获得主导色彩的另一种非常快速的方法是将图像大小调整为1x1像素。 – ViliusL 2015-02-21 13:36:30