0

I cant get this to work for all items. This code only works for the first li item. I believe the error is still in the basic jquery. Colorthief is a plugin that grabs the rgb value from image which im trying to apply to each hover.

$(window).load(function () {
    $(".popup-gallery li").each(function (index) {
        var $myImage = $(this).find('img');
        var colorThief = new ColorThief();

        //Grabs 8 swatch color palette from image and sets quality to 5 (0 =slow, 10=default/fast)
        var cp = colorThief.getPalette($myImage[0], 8, 5);

        //Sets background to 3rd color in the palette.
        $(this).find('.hover').css('background-color', 'rgb(' + cp[2][0] + ',' + cp[2][1] + ',' + cp[2][2] + ')');
    });
});
niksos
  • 433
  • 1
  • 7
  • 19

1 Answers1

0

I wanted to add this suggestion as a comment but I dont have the needed privilege. Anyways from what I get from you jQuery .pop-gallery is the classname for the ul element. Since your HTML is not here, if thats the case please try something like

    $(".pop-gallery").children("li").each(function(){

}

Megha
  • 220
  • 2
  • 13
  • Results are still the same :( only working for first item. No difference with $myImage[index] – niksos Jun 09 '15 at 07:44
  • Can you provide your HTML as well? – Megha Jun 09 '15 at 07:56
  • Please check one more thing.In the code above you have used $myImage[0] when it should be $myImage[index] – Megha Jun 09 '15 at 08:04
  • Yep I tried that. The error comes from that line you're correct var cp = colorThief.getPalette($myImage[index], 8, 5); and console is pointing to the colorthief plugin as well TypeError: a is undefined so i believe this might be a lost cause. – niksos Jun 09 '15 at 08:08