JavaScript Button Ripple Effect Onclick

JavaScript Button Ripple Effect Onclick
Code Snippet:[Pure JS] - Ripple effect
Author: Quentin Veron
Published: January 20, 2024
Last Updated: January 22, 2024
Downloads: 517
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code adds a button ripple effect onclick event with the attribute “data-animation” set to “ripple.” When you click a button, it creates a small animated ripple around the click location, providing a visual feedback effect. This code is helpful for enhancing the user interface and making button interactions more engaging.

How to Create JavaScript Button Ripple Effect Onclick

1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

2. Create buttons in your HTML with specific classes and the data-animation="ripple" attribute to enable the ripple effect:

<a class="btn btn-green" href="#" data-animation="ripple">Click me</a><a class="btn btn-blue" href="#" data-animation="ripple">Click me</a>

3. Define the button styles and the ripple effect in your CSS file. You can adjust colors, sizes, and other properties according to your preferences.

@import url(https://fonts.googleapis.com/css?family=Open+Sans);

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #aa5b56;
  font: 62.5% "open-sans", sans-serif;
}

/* ~~~~~~~~~~~~~~
     Button
~~~~~~~~~~~~~~ */
.btn {
  height: 42px;
  line-height: 42px;
  display: block;
  margin: 2em;
  padding: 0 2em;
  border-radius: 2px;
  box-shadow: 0 4px 6px 0 rgba(0, 0, 0, 0.2);
  font-size: 1.4em;
  text-transform: uppercase;
  text-decoration: none;
  color: #fff;
  transition: background 0.25s ease-in-out;
}

[data-animation] {
  position: relative;
  overflow: hidden;
}

/* ~~~~~~~~~~~~~~
     Colors
~~~~~~~~~~~~~~ */
.btn-blue {
  background: #00bcd4;
}
.btn-blue:hover {
  background: #008fa1;
}

.btn-green {
  background: #4caf50;
}
.btn-green:hover {
  background: #3d8b40;
}

/* ~~~~~~~~~~~~~~
  Ripple Effect
~~~~~~~~~~~~~~ */
.ripple {
  width: 2px;
  height: 2px;
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  -webkit-animation: rippleEffect 0.5s ease-in-out;
          animation: rippleEffect 0.5s ease-in-out;
}

@-webkit-keyframes rippleEffect {
  0% {
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(var(--scale));
  }
}

@keyframes rippleEffect {
  0% {
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(var(--scale));
  }
}

4. Finally, copy and paste the JavaScript code into your project. This code is responsible for creating the ripple effect when a button is clicked. It calculates the click position and creates a ripple animation around it.

const buttons = document.querySelectorAll('[data-animation="ripple"]');

[...buttons].forEach(button => {
  button.onmousedown = function (e) {

    const x = e.pageX - this.offsetLeft;
    const y = e.pageY - this.offsetTop;
    const w = this.offsetWidth;

    const ripple = document.createElement('span');

    ripple.className = 'ripple';
    ripple.style.left = x + 'px';
    ripple.style.top = y + 'px';
    ripple.style.setProperty('--scale', w);

    this.appendChild(ripple);

    setTimeout(() => {
      ripple.parentNode.removeChild(ripple);
    }, 500);
  };
});

You can add the data-animation="ripple" attribute to any button you want to apply the ripple effect to. Customize button text and styles to suit your design preferences.

That’s all! hopefully, you have successfully created a button ripple effect using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X