简体   繁体   English

linear-gradient()值作为js变量

[英]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). 我想知道是否有可能通过JS脚本控制CSS值,以便我可以为它们分配变量(在这种情况下,是从clientX和clientY鼠标事件中提取的)。

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? 有没有人有过将脚本和变量连接到CSS值的经验?

Thanks! 谢谢!

Yes. 是。 It is possible. 有可能的。 Here is an example using X e Y to change background color. 这是一个使用X e Y更改背景颜色的示例。

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})' 在您的情况下,只需将backgroundColor更改为background ='linearGradient(value,$ {JSvalues})'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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