SVG has been widely used in recent years due to its various advantages. Unfortunately, WeChat Mini Programs do not support the use of SVG in the form of XML so far, which greatly reduces the flexibility of SVG. Most people choose to give up using SVG icon solutions in WeChat Mini Programs.
First of all, there is no problem with the first point. WeChat applet supports the introduction of SVG in the form of Image.src. The next part is the key part of this article, how to make SVG in Image form change color. In my recent study of CSS, I found that there is a property that can cast a shadow on the non-transparent part of the DOM. This property is drop-shadow, and its value is roughly similar to box-shadow. With this attribute, we can cast a shadow with a modifiable color for the SVG image, and then we can hide the original part to realize a modifiable color SVG icon. Next, let's practice it. First, construct the DOM structure: <view class="svg_icon"> <image class="svg_icon-inner" src="/path/icon.svg"/> </view> Next, add the CSS: .svg_icon { display: inline-flex; width: 1em; height: 1em; overflow: hidden; } .svg_icon-inner { width: 1em; height: 1em; transform: translateY(-1em); filter: drop-shadow(0 1em 0 currentColor); } Let me explain why the DOM structure and CSS are set up like this: first, svg_icon is the container of the entire icon, responsible for setting the icon size (1em = parent container font size) and hiding the excess part (that is, the original part of the icon), while svg_icon-inner is responsible for rendering SVG and casting colored shadows. By setting svg_icon-inner to the same width and height as the parent container and setting an offset in the opposite direction of the projection, the need to change the SVG color can be achieved (setting the projection color to currentColor can make the icon color change with the font color of the parent container). This solution has a disadvantage. If there is a transform animation in the page and it is triggered, the icon will flicker. I am not sure about the specific reason. I hope someone can give me some advice. This concludes this article on how to happily use SVG icons in WeChat mini-programs. For more information on using SVG icons in WeChat mini-programs, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Sharing of experience on repairing MySQL innodb exceptions
>>: Methods for optimizing Oracle database with large memory pages in Linux
When exporting data to operations, it is inevitab...
1. Function : Allows the parent component to inse...
Table of contents Preface sql_mode explained The ...
This article example shares the specific implemen...
Disadvantages of single-node database Large-scale...
Preface: As a junior programmer, I dream of build...
Stored Procedures 1. Create a stored procedure an...
This article uses examples to illustrate the opti...
The multi-site feature of WordPress allows you to...
I would like to quote an article by Zhang Xinxu a...
Table of contents Preface Error Object throw try…...
Table of contents Opening scene Direct rendering ...
This article records the installation and configu...
This article example shares the specific code of ...
In the previous article, I introduced the functio...