当前位置:网站首页>Actual combat | magical conic gradient
Actual combat | magical conic gradient
2022-06-29 16:36:00 【User 1097444】
| Introduction CSS Future standard cone gradient , Introduce its various uses , Use it to complete incredible wonderful graphics .
thank LeaVerou A great god , Let's use this wonderful property ahead of time .
conic-gradient What is it ? Speaking of conic-gradient , I have to mention its other two brothers :
linear-gradient: Linear gradient
radial-gradient: Radial Gradient
It is said that these two should be understood and used by many people .CSS3 New linear gradients and radial gradients for CSS The world has changed a lot .
and conic-gradient , Represents a conical gradient , Another kind of gradient , to CSS The world brings more possibilities .
Enter the text below , All the examples in this article , Please download it in a later version Chrome Preview under the kernel .
API
Look at its simplest API:
And the similarities and differences between linear gradient and conical gradient
So what's the difference between it and the other two gradients ?
linear-gradientThe direction of a linear gradient is a straight line , It can be any angleradial-gradientRadial gradients spread out from the center of a circle in the shape of an ellipse
And in terms of direction , The direction of the taper is like this :
Focus on :
As you can see from the diagram , The direction and starting point of the taper . The starting point is the center of the graph , Then the gradient effect is achieved around the center in a clockwise direction .
Use conic-gradient Realize the color dial
I know from the above conic-gradient The simplest use , We use it to implement the simplest color dial .
conic-gradient It's not just a gradient from one color to another , Like the other two gradients , Can achieve multi-color transition gradient .
thus , Think of the rainbow , We can list them in turn Red orange yellow green blue violet Seven colors :
conic-gradient: (red, orange, yellow, green, teal, blue, purple)
It says , In the process of taper , Color from the first set red Start , Gradient to orange , Until then yellow , Until the last set purple . And every interval is equally divided .
Let's add border-radius: 50% , Suppose our CSS as follows :
Look at the effect :
wow, It's got a preliminary shape . however , I always feel that there is no nature . All in all , I feel sick all over
Um. ? What's the problem ? First, the color is not rich enough, not bright enough , Second, the connection between the beginning and the end is not natural enough . Let me adjust it a little bit .
We know , The way to express color , except rgb() Beyond color representation , also hsl() notation .
hsl()It's defined as hue - saturation - Lightness (Hue-saturation-lightness)
- Hue (H) It's the basic property of color , It's the name of the color , Like red 、 Yellow, etc .
- saturation (S) It refers to the purity of color , The higher the color, the purer , If it's low, it's gray , take 0-100% The numerical .
- Lightness (V), brightness (L), take 0-100%.
here , We get a brighter and more complete color system by changing the hue .
That is to adopt such a transition hsl(0%, 100%, 50%) —> hsl(100%, 100%, 50%), Only the hue changes in the middle , Generate 20 It's a transitional state . With the help of SCSS ,CSS The grammar is as follows :
The result is as follows , This time it worked very well :
CodePen Demo — conic-gradinet colors(https://codepen.io/Chokcoco/pen/LLLWEy)
Use with percentage
Of course , We can specify the scale of each segment of the cone gradient more specifically , Percent fit , It's easy to implement pie chart .
Suppose we have the following CSS:
Upper figure , We have designated 0~30%,30%~70%,70%~100% The colors of the three intervals are deeppink( Crimson ),yellowgreen( Yellow and green ) as well as teal( green ) , You can get the following pie chart :
Of course , It's just the first way to write percentage , There is another way to write it :
It means :
- 0-30% Use the interval of
deeppink - 0-70% Use the interval of
yellowgreen - 0-100% Use the interval of
teal
and , The colors defined first are superimposed on the colors defined later .
CodePen Demo — use proportion in conic-gradient(
https://codepen.io/Chokcoco/pen/awwGQy)
coordination background-size Use
The percentage is used to control the interval , And then use it together background-size All kinds of mapping can be realized .
We first implement a basic conic gradient graph as follows :
design sketch :
Plus background-size: 50px 50px;, That is to say :
obtain :
CodePen Demo — conic-gradient wallpaper(https://codepen.io/Chokcoco/pen/dRRKqB)
Repeat the taper gradient repaeting-conic-gradient
Like linear and radial gradients , Conic gradients are also repeated conic gradients repaet-conic-gradient Of .
Let's assume that the fragment we want to repeat is 0~30° A segment of , its CSS The code is conic-gradient(deeppink 0 15deg, yellowgreen 0 30deg) .
that , Used repaeting-conic-gradient after , Automatically fills the entire area ,CSS The code is as follows :
CodePen Demo — repeating-conic-gradient(https://codepen.io/Chokcoco/pen/ZyyMBG)
Taper animation
I'm done with some basic usage , Let's see what kind of flowers you can play with with taper gradients .
With the help of SCSS A powerful , We can make some very cool background panels .
Use SCSS , We randomly generate a multi-color cone gradient pattern :
Suppose our HTML The structure is as follows :
CSS/SCSS The code is as follows :
Explain briefly the above SCSS Code ,
randomNum()Used to generate random numbers ,randomNum(255)It's equivalent to random generation 1~255 The random number ;randomConicGradient()Used to generate the entireconic-gradient()Parameters in , That's the color of each section ;vwandvhIt's relatively new CSS Company , For a page , The height of its window is 100vh, The width is 100vw .
OK, Refresh the page , The result is as follows :
Oh my god , It's cool ,bling bling The feeling of glittering ! And it's randomly generated colors , So every time you refresh, you get a new experience !!
It's not over yet. , Next, add the rotation animation to it , Push, push, push , It's going to spin like this :
because GIF The limitation of graph size , Just look at GIF I can't feel the dizziness of science fiction in full screen , I suggest you poke in to have a look :
CodePen Demo — conic-gradient Animation(https://codepen.io/Chokcoco/pen/GEvZra)
Brain hole time
I'm still not very satisfied here . Think of the previous mix-blend-mode attribute .
Want to know
mix-blend-modeThis attribute , You can poke me : Incredible color mixing mode mix-blend-mode(http://www.cnblogs.com/coco1s/p/6829372.html)
If multiple taper gradients are superimposed , And in practice mix-blend-mode What's going to happen ? What a terrible idea …
Finally, this is a very sci-fi effect :
The image above uses 2 A translucent cone gradient , Rotate in opposite directions , And use it at the bottom mix-blend-mode: overlay Superimposed a white black radial gradient layer . You can see the code and the effect :
CodePen Demo — conic-gradient Animation(https://codepen.io/Chokcoco/pen/gRRdQq)
Used in projects conic-gradient
The above example is cool to cool , But it is not practical in the project . So can cone gradients be used in business ? The answer is yes .
Look at the picture below , Sesame credit sub background gradient color bar , Don't use JS, pure CSS With the help of conic-gradient How to draw .
Suppose our structure is as follows :
To highlight conic-gradient The practicality of , Simply combine the two into one , I simulated . Look at the effect , Be accomplished , So conic-gradient There's still room for use :
CodePen Demo — Use conic-gradient Realize the dial credit sub example (https://codepen.io/Chokcoco/pen/pwWNJJ)
Taper gradient conic-gradient polyfill Gasket Library
See here , Presumably readers are eager to try such a magical property .
however , By convention , such “ High-tech ” It's usually not very compatible .conic-gradient What about compatibility ?
Very tragic ,CSS The official description of it is :
- Modules in the revision phase (Modules in the revising phase)
The module in the correction phase is not stable as the module in the improvement phase . Usually their syntax needs to be examined in detail , Maybe there will be a big change , And it's not guaranteed to be compatible with the previous . Alternative syntax is usually tested and implemented .
luckily , I also mentioned at the beginning of the article , thank LeaVerou A great god , Let's use this wonderful property ahead of time .
LeaVerou Provides a library of gaskets , According to the above grammar of this article , Add this gasket Library , You can use it happily .
You need to add the following JS , The gasket library will follow CSS grammar , Generate the corresponding taper gradient pattern , And into BASE64 Code :
Because the function of the gasket library is to put our CSS Grammar is transformed into BASE64 Code replacement
background-image: url()The content in , therefore , You don't need to add these two libraries after you go online .
Okay , This concludes the article , I hope it helps you :)
Any questions or Suggestions , Communicate more , Original article , Writing co., LTD. , Pretty good , If there is anything wrong with the text , All hope to inform .
Last , more CSS Related articles , Can be found in my blog and GITHUB See above :
- Chokcoco —Blog;(http://www.cnblogs.com/coco1s/)
- Chokcoco — Github(https://github.com/chokcoco/iCSS)
reference
CSS conic-gradient() polyfill(http://leaverou.github.io/conic-gradient/)
Scan the QR code below the code ,
Follow more front-end dry goods articles at any time !
▼
WeChat :IMWebTech
边栏推荐
- Comprehensive analysis of Seata distributed transaction at and XA
- 自己实现一个ThreadLocal
- GNN notes: message propagation model
- After studying this series of notes about software testing, it is a "bonus" to enter the factory
- BOE: with the arrival of the peak season in the second half of the year, the promotion and the release of new products, the demand is expected to improve
- 同样是做测试,为什么别人年薪30W+?
- Take another picture of cloud redis' improvement path
- 【Proteus仿真】8位数码管动态扫描显示变化数据
- UWB precise positioning scheme, centimeter level high-precision technology application, intelligent pairing induction technology
- MySQL foundation - multi table query
猜你喜欢

Advanced MySQL - storage engine

After studying this series of notes about software testing, it is a "bonus" to enter the factory

元数据管理Apache Atlas编译集成部署及测试

MATLAB给数据加噪声/扰动

哪个版本的JVM最快?

Tool chain empowers hundreds of companies, horizon opens the "Matthew effect" of mass production of intelligent driving

星环科技数据安全管理平台 Defensor重磅发布

Sophon AutoCV:助力AI工业化生产,实现视觉智能感知

图文带你彻底弄懂MySQL事务原子性之UndoLog

Cv5200 ad hoc network remote WiFi module, UAV wireless image transmission application, HD low delay scheme
随机推荐
Greedy Apple plans to raise the price of iphone14, which will provide opportunities for Chinese mobile phones
Key sprite fighting monsters - window binding protection skills and click skills
What is the strength of a software testing engineer who can get a salary increase twice a year?
Implement a ThreadLocal by yourself
【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
leetcode:139. Word splitting [DFS + memory]
毕业生迷茫,中年人焦虑,职场路怎么越走越宽?
[proteus simulation] 8-bit nixie tube dynamic scanning display change data
又拍云 Redis 的改进之路
贪婪的苹果计划提高iPhone14的价格,这将为中国手机提供机会
After 3 years of testing experience, do you know the state transition method for use case design?
Perhaps in two years, ASML will be free to supply EUV lithography machines to China
Nanjing University: Discussion on the training scheme of digital talents in the new era
Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)
数学知识:求组合数 II—求组合数
STM32 key chattering elimination -- Thinking of entry state machine
如何配置 logback?30分钟让你彻底学会代码熬夜敲
水球图-利用动态波纹展示百分比
Flutter技术与实战(2)
实战 | 神奇的 conic-gradient 圆锥渐变