


How to use pure css to achieve the water drop animation effect of buttons in Material Design
The content of this article is about how to use pure CSS to realize the water drop animation effect of buttons in Material Design. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
You should often see this kind of special effect, it’s very cool, isn’t it
This is the most common in Google Material Design special effects, there are many ready-made js libraries on the market to simulate this special effect. But it is often necessary to introduce a lot of js and css. In fact, in existing projects, you may just want to add such a button to enhance the user experience. These js libraries seem a bit too large. At the same time, because they are implemented in js, many Also pay attention to loading issues.
So, is there a way to use css to achieve this special effect?
Idea
In fact, it is an animation, a perfect circle grows from small to large, using css3# The animation in ## is easy to implement
Sample code
@keyframes ripple{ from { transform: scale(0); opacity: 1; } to { transform: scale(1); opacity: 0; } }Usually
js is used to implement it in a very simple way, which is to add A
class, and then remove the
class
Sample code
var btn = document.getElementById('btn'); btn.addeventlistener('click',function(){ addClass(btn,'animate') },false) btn.addeventlistener('transitionend',function(){//监听css3动画结束 removeClass(btn,'animate') },false)So how to do it through css What about triggering animation? CSS implementation
The pseudo-classes that interact with the mouse in css mainly include
hover
mouse After
- ##:active
Mouse press
- :focus
Mouse focus
- :checked
Mouse selected
In many cases, the effects on our pages are achieved through
To achieve this, putting the mouse on it will trigger an effect and leave the animation. However, if you put the mouse on it and leave immediately, the animation will end immediately. Let’s try it first.
Structure
This is the page structure and style we have written
<style> .btn{ display: block; width: 300px; outline: 0; overflow: hidden; position: relative; transition: .3s; cursor: pointer; user-select: none; height: 100px; text-align: center; line-height: 100px; font-size: 50px; background: tomato; color: #fff; border-radius: 10px; } </style> <a>button</a>
It is very simple, it is an ordinary button style
Next we add the perfect circle we need in the button.
We use pseudo elements to achieve this
.btn:after{ content: ''; position: absolute; width: 100%; padding-top: 100%; background: transparent; border-radius: 50%; left: 50%; top: 50%; transform: translate(-50%,-50%) }
We remove the overflow: hidden above and shrink the circle a little to see the effect
Then, we write a zoom animation
@keyframes ripple{ from { transform:translate(-50%,-50%) scale(0); /**由于我们默认写了变换属性,所以这里要补上translate(-50%,-50%),不然就会被替换了**/ background: rgba(0,0,0,.25); } to { transform:translate(-50%,-50%) scale(1); background: transparent; } }
hover small interactive experience
Try passing the mouse?
.btn:hover:after{ animation: ripple 1s; }
The effect is good, but if the mouse leaves too fast, the newly expanded circle will shrink back immediately, which is a bit inconsistent
But this is not the effect we want. What we want is to click once to trigger once, instead of just putting it on and it will never trigger again.
active try
In daily work,
active is also used more often, usually for the click effect, so let’s try it? <pre class="brush:php;toolbar:false">.btn:active:after{
animation: ripple 1s;
}</pre>
The effect is also unsatisfactory, a bit similar to
, you must keep pressing the mouse to complete the Trigger, for example, in the above example, the running implementation of the animation is 1s, then you must click on that button and continue 1s
to see the complete animation effect, otherwise, it is like the above As soon as the mouse leaves, the animation retracts immediately focus experience
If you need to focus any element, you can specify a
tabindex attribute <pre class="brush:php;toolbar:false"><a>button</a></pre>
<pre class="brush:php;toolbar:false">.btn:focus:after{
animation: ripple 1s;
}</pre>
can also be triggered, but after it is triggered, it can only be triggered again after losing focus. The actual operation performance is, click over After one time, is there no other way to click on the outer space?
Of course there are still some, the last one is definitely the solution, haha
checked
checked cannot be triggered directly, it is a form element Triggered after selection. To do this, we need to transform the page structure <pre class="brush:php;toolbar:false"><label>
<input><span>button</span>
</label></pre>
We changed it to
and included the input[type=checkbox]
tag , mainly to trigger the input
selection when the button is clicked. Add a little style
.btn>span:after{ /**换一下选择器**/ } .btn>input[type=checkbox]{ display: none } .btn>input[type=checkbox]:checked+span:after{ animation: ripple 1s; }
这样也能触发动画,但问题是,当再次点击的时候就成了非选中状态了,怎么触发动画呢?
其实可以用:not
来实现
.btn>input[type=checkbox]:not(:checked)+span:after{ animation: ripple 1s; }
乍一看好像挺聪明的,仔细一想,正反两个都写了动画,不就跟:checked
没关系了?还不如直接
.btn>input[type=checkbox]+span:after{ animation: ripple 1s; }
无限轮回中...
这个问题困扰了我好久,不过皇天不负有心人,后来试着在两种状态下触发不同的动画是可以分别触发的,如下
.btn>input[type=checkbox]:checked+span:after{ animation: ripple1 1s; } .btn>input[type=checkbox]:not(:checked)+span:after{ animation: ripple2 1s; }
这个应该很好理解吧。
那么,重点来了,现在把动画ripple1
和ripple2
里面的动画过程都改成一样,也是可以分别触发的,也就是说,只要动画名称不一样,css都会当成不同的动画来处理
这样就简单了,我们只需要默认一个状态,选中一个状态,然后分别触发名称不同的动画就行了~
.btn>input[type=checkbox]+span:after{ animation: ripple-in 1s; } .btn>input[type=checkbox]:checked+span:after{ animation: ripple-out 1s; } @keyframes ripple-in{ from { transform:translate(-50%,-50%) scale(0); background: rgba(0,0,0,.25); } to { transform:translate(-50%,-50%) scale(1); background: transparent; } } @keyframes ripple-out{/*仅仅名称不同*/ from { transform:translate(-50%,-50%) scale(0); background: rgba(0,0,0,.25); } to { transform:translate(-50%,-50%) scale(1); background: transparent; } }
效果就如文章一开始所示,完美
完整demo如下
https://codepen.io/xboxyan/pe...
一些不足
由于上述动画样式在默认情况下就会被触发,所以页面加载进来就会看到按钮上的水滴动画运动一次,不过也不是特别明显,还可以接受。
其次,实际效果肯定是希望鼠标点击哪里,就以该点为中心扩散,我们css肯定是做不到这点的,只能从中心扩散,这也只能妥协了。这里提供一个思路,可以使用css的变量,每次点击的时候吧相应的值存在style里面,这样css中也能用上。
希望能用css今后挖掘出更多有趣的效果^
The above is the detailed content of How to use pure css to achieve the water drop animation effect of buttons in Material Design. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
