0

I am looking for a way to select the css variable --bgcolor of the element being hovered and assign that value to the body css

Here is my current attempt, any suggestions?

var body = document.querySelector('body');
var boxList = document.querySelectorAll('.box');

  var box = Array.prototype.slice.call(boxList);
  box.forEach(function (el) {
    el.addEventListener('mouseover', ()=> {
      var newbg = el.style.background;
      body.style.background = newbg;
    });
  });
body {
  background: var(--bgcolor);
}

.box {
  width: 80px;
  height: 80px;
  margin: 10px;
  background: var(--bgcolor);
  display:inline-block;
}
<body>
  <div class="box" style="--bgcolor: #FF0000"></div>
  <div class="box" style="--bgcolor: #00FF00"></div>
  <div class="box" style="--bgcolor: #0000FF"></div>
</body>

3 Answers 3

2

You can use getComputedStyle(). If want to get the variable then you need to use getPropertyValue() with computedStyle. Otherwise you can directly get background property from computedStlye

var body = document.querySelector('body');
var boxList = document.querySelectorAll('.box');

  var box = Array.prototype.slice.call(boxList);
  box.forEach(function (el) {
    el.addEventListener('mouseover', ()=> {
  
      var newbg = window.getComputedStyle(el).getPropertyValue('--bgcolor')
      
      body.style.background = newbg;
    });
  });
body {
  background: var(--bgcolor);
}

.box {
  width: 80px;
  height: 80px;
  margin: 10px;
  background: var(--bgcolor);
  display:inline-block;
}
<body>
  <div class="box" style="--bgcolor: #FF0000"></div>
  <div class="box" style="--bgcolor: #00FF00"></div>
  <div class="box" style="--bgcolor: #0000FF"></div>
</body>

Sign up to request clarification or add additional context in comments.

Comments

2

You can access CSS variables in your Javascript code through the getComputedStyle and getPropertyValue methods as shown below:

getComputedStyle(el).getPropertyValue('--bgcolor');

So in your case, set newbg to the line above instead of el.style.background.

If you encounter any problems or questions while implementing my solution, please tell me in a comment.

Edit: Looks like someone beat me to the punch!

Comments

1

I would delegate

const body = document.querySelector('body');
const bodyCol = window.getComputedStyle(body).getPropertyValue('--bgcolor')
const hover = e => {
  const tgt = e.target;
  if (tgt.classList.contains("box")) {
    const bg  = window.getComputedStyle(tgt).getPropertyValue('--bgcolor')
    body.style.backgroundColor = e.type === "mouseover" ? bg : bodyCol;
  }  
};

document.getElementById("colors").addEventListener("mouseover",hover)
document.getElementById("colors").addEventListener("mouseout",hover)
body {
  background: var(--bgcolor);
}

.box {
  width: 80px;
  height: 80px;
  margin: 10px;
  background: var(--bgcolor);
  display:inline-block;
}
<body>
<div id="colors">
  <div class="box" style="--bgcolor: #FF0000"></div>
  <div class="box" style="--bgcolor: #00FF00"></div>
  <div class="box" style="--bgcolor: #0000FF"></div>
</div>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.