Skip to content

Instantly share code, notes, and snippets.

@LiuuY
Created January 28, 2017 13:13
Show Gist options
  • Save LiuuY/12c83beb817ebbfa8e381a2a897739cb to your computer and use it in GitHub Desktop.
Save LiuuY/12c83beb817ebbfa8e381a2a897739cb to your computer and use it in GitHub Desktop.
How to get the background color of an element using javascript
function getBgColor(e, pseudo) {
var transparent = 'rgba(0, 0, 0, 0)'
if (!e) {
return transparent
}
var currentBgColor = window.getComputedStyle(e, pseudo).backgroundColor
if (currentBgColor === transparent) {
getBgColor(e.parentElement)
}
return currentBgColor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment