


How to use pure css to achieve the water drop animation effect of buttons in Material Design
Oct 19, 2018 pm 04:57 PMThe 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(){//監(jiān)聽css3動畫結(jié)束 ??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; }
這樣也能觸發(fā)動畫,但問題是,當再次點擊的時候就成了非選中狀態(tài)了,怎么觸發(fā)動畫呢?
其實可以用:not
來實現(xiàn)
.btn>input[type=checkbox]:not(:checked)+span:after{ ??animation:?ripple?1s; }
乍一看好像挺聰明的,仔細一想,正反兩個都寫了動畫,不就跟:checked
沒關(guān)系了?還不如直接
.btn>input[type=checkbox]+span:after{ ??animation:?ripple?1s; }
無限輪回中...
這個問題困擾了我好久,不過皇天不負有心人,后來試著在兩種狀態(tài)下觸發(fā)不同的動畫是可以分別觸發(fā)的,如下
.btn>input[type=checkbox]:checked+span:after{ ??animation:?ripple1?1s; } .btn>input[type=checkbox]:not(:checked)+span:after{ ??animation:?ripple2?1s; }
這個應(yīng)該很好理解吧。
那么,重點來了,現(xiàn)在把動畫ripple1
和ripple2
里面的動畫過程都改成一樣,也是可以分別觸發(fā)的,也就是說,只要動畫名稱不一樣,css都會當成不同的動畫來處理
這樣就簡單了,我們只需要默認一個狀態(tài),選中一個狀態(tài),然后分別觸發(fā)名稱不同的動畫就行了~
.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...
一些不足
由于上述動畫樣式在默認情況下就會被觸發(fā),所以頁面加載進來就會看到按鈕上的水滴動畫運動一次,不過也不是特別明顯,還可以接受。
其次,實際效果肯定是希望鼠標點擊哪里,就以該點為中心擴散,我們css肯定是做不到這點的,只能從中心擴散,這也只能妥協(xié)了。這里提供一個思路,可以使用css的變量,每次點擊的時候吧相應(yīng)的值存在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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The web page structure needs to be supported by core HTML elements. 1. The overall structure of the page is composed of , , which is the root element, which stores meta information and displays the content; 2. The content organization relies on title (-), paragraph () and block tags (such as ,) to improve organizational structure and SEO; 3. Navigation is implemented through and implemented, commonly used organizations are linked and supplemented with aria-current attribute to enhance accessibility; 4. Form interaction involves , , and , to ensure the complete user input and submission functions. Proper use of these elements can improve page clarity, maintenance and search engine optimization.

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

Client-sideformvalidationcanbedonewithoutJavaScriptbyusingHTMLattributes.1)Userequiredtoenforcemandatoryfields.2)ValidateemailsandURLswithtypeattributeslikeemailorurl,orusepatternwithregexforcustomformats.3)Limitvaluesusingmin,max,minlength,andmaxlen

Use tags in HTML to group options in the drop-down menu. The specific method is to wrap a group of elements and define the group name through the label attribute, such as: 1. Contains options such as apples, bananas, oranges, etc.; 2. Contains options such as carrots, broccoli, etc.; 3. Each is an independent group, and the options within the group are automatically indented. Notes include: ① No nesting is supported; ② The entire group can be disabled through the disabled attribute; ③ The style is restricted and needs to be beautified in combination with CSS or third-party libraries; plug-ins such as Select2 can be used to enhance functions.

HTML5, CSS and JavaScript should be efficiently combined with semantic tags, reasonable loading order and decoupling design. 1. Use HTML5 semantic tags, such as improving structural clarity and maintainability, which is conducive to SEO and barrier-free access; 2. CSS should be placed in, use external files and split by module to avoid inline styles and delayed loading problems; 3. JavaScript is recommended to be introduced in front, and use defer or async to load asynchronously to avoid blocking rendering; 4. Reduce strong dependence between the three, drive behavior through data-* attributes and class name control status, and improve collaboration efficiency through unified naming specifications. These methods can effectively optimize page performance and collaborate with teams.

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

Hello, JavaScript developers! Welcome to this week's JavaScript news! This week we will focus on: Oracle's trademark dispute with Deno, new JavaScript time objects are supported by browsers, Google Chrome updates, and some powerful developer tools. Let's get started! Oracle's trademark dispute with Deno Oracle's attempt to register a "JavaScript" trademark has caused controversy. Ryan Dahl, the creator of Node.js and Deno, has filed a petition to cancel the trademark, and he believes that JavaScript is an open standard and should not be used by Oracle

Metadata in HTMLhead is crucial for SEO, social sharing, and browser behavior. 1. Set the page title and description, use and keep it concise and unique; 2. Add OpenGraph and Twitter card information to optimize social sharing effects, pay attention to the image size and use debugging tools to test; 3. Define the character set and viewport settings to ensure multi-language support is adapted to the mobile terminal; 4. Optional tags such as author copyright, robots control and canonical prevent duplicate content should also be configured reasonably.
