question How to modify CSS pseudo-class style with js? But js does not have a pseudo-class selector, so what should we do? There are many methods on the Internet, such as switching element classes, dynamically inserting new styles in style, etc. Then here is another method, which is to set the CSS variable (var) and change this variable through JS to achieve it. Example: Change the hover background color of a div <!-- css --> <style type="text/css"> :root { --divHoverColor: red; } div { width: 100px; height: 100px; background: bisque; } div:hover { background: var(--divHoverColor); } </style> <!-- html --> <div onClick="onDivClick('green')"/> <!-- js --> <script type="text/javascript"> function onDivClick(value){ document.documentElement.style.setProperty('--divHoverColor', value); } </script> So, let’s get to know CSS variables. 1. Basic usage Local variables div { --masterColor: red; background: var(--masterColor); } Global variables /* For HTML, :root refers to the <html> element*/ :root { --masterColor: red; } div { background: var(--masterColor); } 2. Syntax var( <custom-property-name> [, <declaration-value> ]? ) <custom-property-name> : custom property name <declaration-value> : declaration value (fallback value) Example: div { /* --masterColor is undefined, so the background color is red It can define multiple declaration values separated by commas, var(--masterColor, red, green) */ background: var(--masterColor, red); } Variables can be referenced using var() Example: div { --masterColor: red; --bgColor:var(--masterColor) } Note: Variables cannot be used in attribute names. Error example: div { --bg: background; var(--bg): red; } 3. Browser Support Can I use Using @support detection @supports ( (--masterColor: red)) { /* supported */ } @supports ( not (--masterColor: red)) { /* not supported */ } Using JS detection const isSupported = window.CSS && window.CSS.supports && window.CSS.supports('--masterColor', 'red'); if (isSupported) { /* supported */ } else { /* not supported */ } refer to MDN The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: HTML&CSS&JS compatibility tree (IE, Firefox, Chrome)
>>: MySQL advanced learning index advantages and disadvantages and rules of use
Google China has released a translation tool that ...
This article is mainly to take you to quickly und...
This article records the installation and configu...
Preface Every developer who comes into contact wi...
Preface In MySQL, InnoDB belongs to the storage e...
Table of contents (I) Using Workbench to operate ...
I followed the tutorial on W3school. I think the t...
<br />I have summarized the annotation writi...
Recently, I want to build a hadoop test cluster i...
Table of contents 1. Shared CommonModule 2. Share...
Preface In order to reflect the difference betwee...
MySQL supports many types of tables (i.e. storage...
1. <div></div> and <span></s...
Since I installed the official version of IE8.0, ...
1. Server setup The remote repository is actually...