How to use SVG icons in WeChat applets

How to use SVG icons in WeChat applets

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.
So, is there really no way to happily use SVG icons in WeChat applets? Let's first analyze what requirements we have for using SVG icons:

  • Can be introduced for use
  • Ability to adjust colors

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:
  • How to introduce SVG vector icons into WeChat applet
  • Today, Mini Programs officially support SVG
  • The store rating component in the WeChat applet and the rating display component implemented with svg in vue
  • Detailed explanation of how to use SVG vector icons in WeChat applet

<<:  Sharing of experience on repairing MySQL innodb exceptions

>>:  Methods for optimizing Oracle database with large memory pages in Linux

Recommend

Mysql implements three functions for field splicing

When exporting data to operations, it is inevitab...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

Detailed explanation of the use of MySQL sql_mode

Table of contents Preface sql_mode explained The ...

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

Implementation of Docker deployment of MySQL cluster

Disadvantages of single-node database Large-scale...

How to quickly build a static website on Alibaba Cloud

Preface: As a junior programmer, I dream of build...

MySQL stored procedure in, out and inout parameter examples and summary

Stored Procedures 1. Create a stored procedure an...

N ways to align the last row of lists in CSS flex layout to the left (summary)

I would like to quote an article by Zhang Xinxu a...

JavaScript Basics: Error Capture Mechanism

Table of contents Preface Error Object throw try…...

Detailed graphic explanation of how to clear the keep-alive cache

Table of contents Opening scene Direct rendering ...

MySQL 8.0.12 installation configuration method and password change

This article records the installation and configu...

Vue+spring boot realizes the verification code function

This article example shares the specific code of ...