1
I’m having a problem and I don’t know if it’s a bug, because jQuery isn’t capturing the background-color
of the element passed by css()
, I’m using an attribute data-color
to configure the color of the element, in the browsers it puts the color, follows the image below:
When I consult in jQuery using this command: $('.btn').css('backgroundColor')
it returns me blank but when I insert the style
works.
Note: I am using this function to detect the color and check if it is a light or dark element.
const self = $(el) // Elementos
let getBackground = (item) => {
let background = item.css('backgroundColor'),
alpha = parseFloat(background.split(',')[3], 10)
if ((isNaN(alpha) || alpha > .8) && background !== 'transparent') return background
if (item.is('body'))
return false
else
return getBackground(item.parent())
}
let getLuma = (color) => {
let rgba = color.substring(4, color.length - 1).split(','),
r = rgba[0],
g = rgba[1],
b = rgba[2],
luma = 0.2126 * r + 0.7152 * g + 0.0722 * b
return luma
}
// Consulta
let cor = getBackground(self),
inverse = (getLuma(cor) > 183) ? ' inverse' : '',
$ripple = $('<div class="ink' + inverse + '"></div>')
I imagined trying to pick up by the attribute data-color
to compare but I don’t know how to jQuery
because it returns only the name contained using data()
.
Give a
console.log(item)
to see if you’re getting the right element.– fernandoandrade
@fernandoandrade In this case I am using the Framework
VueJS
and the$(el)
is the captured element and even need to go through the DOM using the.each()
.– iLeonardo Carvalho
When I consult inside the function it is blank the results, but when I enter
style
it works and returns me inrgb()
– iLeonardo Carvalho
In the
data-color
you can pass the value asrgb()
or in the form#ffffff
?– fernandoandrade
I did this change test but it didn’t work, I’m using
jQuery@latest # 3.1.1
– iLeonardo Carvalho