简体   繁体   中英

linear-gradient() values as js variables

I was wondering if it would be possible to control CSS values via JS script so that I could assign variables to them (in this case extracted from clientX and clientY mouse events).

That would mean that color values would be changing as the mouse moves across the page. Has anyone had experiences of connecting scripts and variables to CSS values?

Thanks!

Yes. It is possible. Here is an example using X e Y to change background color.

function runEvent(e) {
    let valY = e.clientY, valX = e.clientX;

    document.body.style.backgroundColor = `rgb(${e.offsetX}, ${e.offsetY}, 0)`;
}

window.addEventListener('mousemove', runEvent);

In your case, just change backgroundColor to background = 'linearGradient(value, ${JSvalues})'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM