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
This article shares the installation steps and us...
Table of contents 1. Built-in objects 2. Math Obj...
Table of contents Preface On-site investigation C...
The execution relationship between the href jump ...
Canvas is a new tag in HTML5. You can use js to o...
1. Create a table CREATE TABLE `student` ( `id` i...
XML/HTML CodeCopy content to clipboard <!DOCTY...
This article introduces how to build a high-avail...
Big data continues to heat up, and if you are not...
When configuring web.xml for tomcat, servlet is a...
CSS3 syntax: (1rem = 100px for a 750px design) @m...
Format Encoding 1. Please set the page width with...
mysql 5.6.35 winx64 free installation version con...
01. Command Overview The whatis command searches ...
The most common, most commonly used and most gener...