国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Idea
This is the page structure and style we have written
Try passing the mouse?
In daily work,
If you need to focus any element, you can specify a
Home Web Front-end CSS Tutorial How to use pure css to achieve the water drop animation effect of buttons in Material Design

How to use pure css to achieve the water drop animation effect of buttons in Material Design

Oct 19, 2018 pm 04:57 PM
css css3 css3 animation html Pure css implementation

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

How to use pure css to achieve the water drop animation effect of buttons in Material Design

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

  • hovermouse After

  • ##:active

    Mouse press

  • :focus

    Mouse focus

  • :checked

    Mouse selected

  • In many cases, the effects on our pages are achieved through
hover

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

How to use pure css to achieve the water drop animation effect of buttons in Material Design 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

How to use pure css to achieve the water drop animation effect of buttons in Material DesignThen, 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;
}

How to use pure css to achieve the water drop animation effect of buttons in Material DesignThe 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>

How to use pure css to achieve the water drop animation effect of buttons in Material DesignThe effect is also unsatisfactory, a bit similar to

mouse hold

, 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">&lt;a&gt;button&lt;/a&gt;</pre> <pre class="brush:php;toolbar:false">.btn:focus:after{ ??animation:?ripple?1s; }</pre>

How to use pure css to achieve the water drop animation effect of buttons in Material Design

foucs

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">&lt;label&gt; ??&lt;input&gt;&lt;span&gt;button&lt;/span&gt; &lt;/label&gt;</pre> We changed it to

lable

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;
}

這個應該很好理解吧。

那么,重點來了,現(xiàn)在把動畫ripple1ripple2里面的動畫過程都改成一樣,也是可以分別觸發(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的變量,每次點擊的時候吧相應的值存在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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Autoprefixer and how does it work? What is Autoprefixer and how does it work? Jul 02, 2025 am 01:15 AM

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

What is the loading='lazy' one of the html attributes and how does it improve page performance? What is the loading='lazy' one of the html attributes and how does it improve page performance? Jul 01, 2025 am 01:33 AM

loading="lazy" is an HTML attribute for and which enables the browser's native lazy loading function to improve page performance. 1. It delays loading non-first-screen resources, reduces initial loading time, saves bandwidth and server requests; 2. It is suitable for large amounts of pictures or embedded content in long pages; 3. It is not suitable for first-screen images, small icons, or lazy loading using JavaScript; 4. It is necessary to cooperate with optimization measures such as setting sizes and compressing files to avoid layout offsets and ensure compatibility. When using it, you should test the scrolling experience and weigh the user experience.

What is the conic-gradient() function? What is the conic-gradient() function? Jul 01, 2025 am 01:16 AM

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

What are best practices for writing valid and well-formed HTML code? What are best practices for writing valid and well-formed HTML code? Jul 01, 2025 am 01:32 AM

When writing legal and neat HTML, you need to pay attention to clear structure, correct semantics and standardized format. 1. Use the correct document type declaration to ensure that the browser parses according to the HTML5 standard; 2. Keep the tag closed and reasonably nested to avoid forgetting closed or wrong nesting elements; 3. Use semantic tags such as, etc. to improve accessibility and SEO; 4. The attribute value is always wrapped in quotes, and single or double quotes are used uniformly. Boolean attributes only need to exist, and the class name should be meaningful and avoid redundant attributes.

CSS tutorial focusing on mobile-first design CSS tutorial focusing on mobile-first design Jul 02, 2025 am 12:52 AM

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

What are the essential HTML elements for structuring a webpage? What are the essential HTML elements for structuring a webpage? Jul 03, 2025 am 02:34 AM

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.

CSS tutorial for creating loading spinners and animations CSS tutorial for creating loading spinners and animations Jul 07, 2025 am 12:07 AM

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.

Implementing client-side form validation using HTML attributes. Implementing client-side form validation using HTML attributes. Jul 03, 2025 am 02:31 AM

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

See all articles