Manan Joshi
Manan Joshi

Reputation: 15

How do we change linear-gradient of background image using javascript DOM?

document.querySelector('.image').addEventListener('mousemove',(e)=>{

    document.querySelector('.image').style.backgroundImage = 'linear-gradient(to top,rgba(${e.offsetX},${e.offsetY},22,0.5),rgba(31, 22, 26, 0.8)), url("https://pics.freeartbackgrounds.com/fullhd/Morning_Sea_and_Boat_Background-318.jpg")';

});

I wrote this EventListener function and want to change the gradient of my background image with mousemove event. This is not working.

Upvotes: 0

Views: 1186

Answers (2)

aden lall
aden lall

Reputation: 1

You can try :

  var element = document.querySelector('.class');//The class of your element
    var change = document.querySelector ('.stepPre p');

element.addEventListener('click', function FUNCTIONNAME(){
    change.innerHTML = 'here you change the p of .stepPre';
    change.style.backgroundImage = 'linear-gradient(-120deg, #75d9e0,#5ddfff)';
  })

Upvotes: 0

abdulrahman alaa
abdulrahman alaa

Reputation: 557

I believe you missed the `` that's all

window.onload = ()=> {
  document.querySelector('.image').addEventListener('mousemove',(e)=>{
    const style = `linear-gradient(to top right,rgba(${e.offsetX},${e.offsetY},22,0.5),rgba(31, 22, 26, 0.8))`;
    console.log(style);
    document.querySelector('.image').style.backgroundImage = style;
  });
}
<html>
  <head></head>
  <body>
    <image  width="200" height="200" class="image" src="" />
  </body>
</html>

Upvotes: 1

Related Questions