


Steps to implement the water ripple effect on mouse click using pure CSS
The steps to implement the mouse click water ripple effect using pure CSS require specific code examples
The mouse click water ripple effect is one of the common interactive effects in web development. It can bring a more vivid experience to users. In this article, we’ll share how to achieve this effect using pure CSS and provide specific code examples.
The implementation steps are as follows:
Step 1: HTML structure
First, create an element with a mouse click effect in the HTML file. You can use a <div> element as a container and set a class name for styling in CSS. <pre class='brush:php;toolbar:false;'><div class="ripple-container">
<button class="ripple-button">Click me</button>
</div></pre><p>Step 2: CSS style settings</p><p>Next, set the styles of the <code>ripple-container
and ripple-button
classes in the CSS file. We set the ripple-container
class to relative positioning to accommodate the water ripple effect. At the same time, the ripple-button
class should be set to absolute positioning to ensure that the effect is displayed inside the button.
.ripple-container { position: relative; display: inline-block; } .ripple-button { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 12px 24px; background-color: #44a1ff; color: #ffffff; border: none; border-radius: 4px; cursor: pointer; }
Step 3: Define the water ripple effect
Use the ::after
pseudo-element and the animation
attribute to define the water ripple effect. We set the water ripple to be circular and animate it to gradually expand from the inside out. In addition, we also need to set the color and duration of the water ripples.
.ripple-button::after { content: ""; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 0; height: 0; border-radius: 50%; background-color: rgba(255, 255, 255, 0.4); animation: rippleEffect 1s ease-out; } @keyframes rippleEffect { 0% { width: 0; height: 0; opacity: 0.4; } 100% { width: 200px; height: 200px; opacity: 0; } }
Step 4: Bind the click event
Finally, bind the click event to the button element in the JavaScript file. When the mouse clicks on the button, a .ripple
class will be added to trigger the water ripple effect.
const button = document.querySelector('.ripple-button'); button.addEventListener('click', function(e) { // 只有当水波纹动画结束后,我们才会添加动画类 if (!button.classList.contains('ripple')) { button.classList.add('ripple'); setTimeout(function(){ button.classList.remove('ripple'); }, 1000); } });
So far, we have completed all the steps to achieve the water ripple effect on mouse clicks using pure CSS. By adding the above code and opening the HTML file in the browser, click the button to see the display of the water ripple effect when the mouse clicks.
I hope this article will help you achieve the water ripple effect when you click on the mouse. If you have any questions or doubts, you are always welcome to give feedback. Thank you for reading!
The above is the detailed content of Steps to implement the water ripple effect on mouse click using pure CSS. 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

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.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

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

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

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.

FeaturedetectioninCSSusing@supportschecksifabrowsersupportsaspecificfeaturebeforeapplyingrelatedstyles.1.ItusesconditionalCSSblocksbasedonproperty-valuepairs,suchas@supports(display:grid).2.Thismethodensuresfuturecompatibilityandavoidsrelianceonunrel

The top ten virtual currency trading platforms in Europe in 2025 include Binance, OKX, Coinbase, etc., and are selected based on compliance, security, expenses, asset types and user experience. 1. Binance: The world has the largest transaction volume, low fees, and has obtained a license in multiple countries; 2. OKX: Comprehensive products, strong technology, registered in France; 3. Coinbase: Compliance and safety, suitable for beginners, licensed in many countries; 4. Gate.io: Has a long history, high security, registered in many European countries; 5. Bitstamp: Founded early, has strong compliance, regulated by Luxembourg; 6. eToro: Supports social transactions, diversified investment, regulated by CySEC; 7. Bitpanda: World

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.
