How to set gradient color in javascript

醉折花枝作酒筹
Release: 2021-06-15 14:48:51
Original
7236 people have browsed it

In JavaScript, you can use the style object attribute to set the gradient color. The syntax format is "element object.style.backgroundImage='linear-gradient(angle, color, color)'"; the Style object represents a separate Style declaration.

How to set gradient color in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In fact, the implementation of gradient is ultimately controlled by css (style)

All codes are written directly in JS:

var test = document.getelementById(''test); test .style.backgroundImage='linear-gradient(120deg, #155799, #159957)';
Copy after login

PS: The 'i' of the backgroundImage here must be It should be capitalized, and it must be written like this. It cannot be written the same as the background-image in CSS, which will not be recognized by JS.

Extended information:

After writing it in css, add a class to the element through js to add a new style:

Defined in the style sheet Style

css:

.jianbian{ background-image: linear-gradient(120deg, #155799, #159957); }
Copy after login

Then add class to the element through js

js:

var test = document.getelementById(''test); test.className = 'jianbian' ;
Copy after login

That’s it, but you still need to consider the differences Browser compatibility, how to write gradients.

-webket-/-moz-like

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of How to set gradient color in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!