Elevate your website design by adding interactive Ripple Buttons using a combination of Javascript and

Recently, I came across a script that allows for an animation to occur when a button is clicked. However, I have encountered an issue where the script does not redirect to a link.

(function() { var cleanUp, debounce, i, len, ripple, rippleContainer, ripples, showRipple;

debounce = function(func, delay) {
  var inDebounce;
  inDebounce = undefined;
  return function() {
    var args, context;
    context = this;
    args = arguments;
    clearTimeout(inDebounce);
    return inDebounce = setTimeout(function() {
      return func.apply(context, args);
    }, delay);
  };
};

showRipple = function(e) {
  var pos, ripple, rippler, size, style, x, y;
  ripple = this;
  rippler = document.createElement('span');
  size = ripple.offsetWidth;
  pos = ripple.getBoundingClientRect();
  x = e.pageX - pos.left - (size / 2);
  y = e.pageY - pos.top - (size / 2);
  style = 'top:' + y + 'px; left: ' + x + 'px; height: ' + size + 'px; width: ' + size + 'px;';
  ripple.rippleContainer.appendChild(rippler);
  return rippler.setAttribute('style', style);
};

cleanUp = function() {
  while (this.rippleContainer.firstChild) {
    this.rippleContainer.removeChild(this.rippleContainer.firstChild);
  }
};

ripples = document.querySelectorAll('[ripple]');

for (i = 0, len = ripples.length; i < len; i++) {
  ripple = ripples[i];
  rippleContainer = document.createElement('div');
  rippleContainer.className = 'ripple--container';
  ripple.addEventListener('mousedown', showRipple);
  ripple.addEventListener('mouseup', debounce(cleanUp, 2000));
  ripple.rippleContainer = rippleContainer;
  ripple.appendChild(rippleContainer);
}
 }());
.bg--red {
  background-color: #e74c3c;
}
... (CSS code continues)

</div>

I am seeking a way to make the buttons clickable so that upon clicking, it redirects to a specific link.

While one solution could be to add a JavaScript redirect function, I prefer to achieve this functionality using <a href="">

If anyone has a solution, I would greatly appreciate it :)

Answer №1

(function() { var cleanUp, debounce, i, len, ripple, rippleContainer, ripples, showRipple;

debounce = function(func, delay) {
  var inDebounce;
  inDebounce = undefined;
  return function() {
    var args, context;
    context = this;
    args = arguments;
    clearTimeout(inDebounce);
    return inDebounce = setTimeout(function() {
      return func.apply(context, args);
    }, delay);
  };
};

showRipple = function(e) {
  var pos, ripple, rippler, size, style, x, y;
  ripple = this;
  rippler = document.createElement('span');
  size = ripple.offsetWidth;
  pos = ripple.getBoundingClientRect();
  x = e.pageX - pos.left - (size / 2);
  y = e.pageY - pos.top - (size / 2);
  style = 'top:' + y + 'px; left: ' + x + 'px; height: ' + size + 'px; width: ' + size + 'px;';
  ripple.rippleContainer.appendChild(rippler);
  return rippler.setAttribute('style', style);
};

cleanUp = function() {
  while (this.rippleContainer.firstChild) {
    this.rippleContainer.removeChild(this.rippleContainer.firstChild);
  }
};

ripples = document.querySelectorAll('[ripple]');

for (i = 0, len = ripples.length; i < len; i++) {
  ripple = ripples[i];
  rippleContainer = document.createElement('div');
  rippleContainer.className = 'ripple--container';
  ripple.addEventListener('mousedown', showRipple);
  ripple.addEventListener('mouseup', debounce(cleanUp, 2000));
  ripple.rippleContainer = rippleContainer;
  ripple.appendChild(rippleContainer);
}
 }());
.bg--red {
  background-color: #e74c3c;
}
.tx--red {
  color: #e74c3c;
}
.bg--blue {
  background-color: #00f;
}
.tx--blue {
  color: #00f;
}
.bg--green {
  background-color: #2ecc71;
}
.tx--green {
  color: #2ecc71;
}
.bg--white {
  background-color: #fff;
}
.tx--white {
  color: #fff;
}
body {
  color: #777;
  text-align: center;
  padding: 0 0 0 0;
  margin: 0 0 0 0;
}
button,a { /* style the anchor as a button */
  border: none;
  padding: 20px;
  margin: 10px;
  font-size: 14px;
  outline: 0;
  box-shadow: 0px 2px 4px 0px #000;
  border-radius: 4px;
}
button:active {
  box-shadow: 0px 2px 6px 0px #000;
}
[ripple] {
  position: relative;
  overflow: hidden;
}
[ripple] .ripple--container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
[ripple] .ripple--container span {
  transform: scale(0);
  border-radius: 100%;
  position: absolute;
  opacity: 0.75;
  background-color: #fff;
  animation: ripple 1000ms;
}
@-moz-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-webkit-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-o-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
<div class="content">
  <a  class="bg--red tx--white" ripple="ripple" href="http://www.google.de">Fail</a>
  <button class="bg--blue tx--white" ripple="ripple">Info</button>
  
  <!-- change this to anchor -->
  <a href="http://www.google.com" class="bg--green tx--white" ripple="ripple">Save</a>
  
  
</div>

Explore a new look for your anchor element by styling it as a button. You can customize the HTML and CSS like this.

Answer №2

You have the option to enclose <a> within <button>

<!-- begin snippet: js hide: false console: true babel: false -->
   

 (function() { var cleanUp, debounce, i, len, ripple, rippleContainer, ripples, showRipple;

debounce = function(func, delay) {
  var inDebounce;
  inDebounce = undefined;
  return function() {
    var args, context;
    context = this;
    args = arguments;
    clearTimeout(inDebounce);
    return inDebounce = setTimeout(function() {
      return func.apply(context, args);
    }, delay);
  };
};

showRipple = function(e) {
  var pos, ripple, rippler, size, style, x, y;
  ripple = this;
  rippler = document.createElement('span');
  size = ripple.offsetWidth;
  pos = ripple.getBoundingClientRect();
  x = e.pageX - pos.left - (size / 2);
  y = e.pageY - pos.top - (size / 2);
  style = 'top:' + y + 'px; left: ' + x + 'px; height: ' + size + 'px; width: ' + size + 'px;';
  ripple.rippleContainer.appendChild(rippler);
  return rippler.setAttribute('style', style);
};

cleanUp = function() {
  while (this.rippleContainer.firstChild) {
    this.rippleContainer.removeChild(this.rippleContainer.firstChild);
  }
};

ripples = document.querySelectorAll('[ripple]');

for (i = 0, len = ripples.length; i < len; i++) {
  ripple = ripples[i];
  rippleContainer = document.createElement('div');
  rippleContainer.className = 'ripple--container';
  ripple.addEventListener('mousedown', showRipple);
  ripple.addEventListener('mouseup', debounce(cleanUp, 2000));
  ripple.rippleContainer = rippleContainer;
  ripple.appendChild(rippleContainer);
}
 }());
    .bg--red {
      background-color: #e74c3c;
    }
    .tx--red {
      color: #e74c3c;
    }
    .bg--blue {
      background-color: #00f;
    }
    .tx--blue {
      color: #00f;
    }
    .bg--green {
      background-color: #2ecc71;
    }
    .tx--green {
      color: #2ecc71;
    }
    .bg--white {
      background-color: #fff;
    }
    .tx--white {
      color: #fff;
    }
    body {
      color: #777;
      text-align: center;
      padding: 0 0 0 0;
      margin: 0 0 0 0;
    }
    button {
      border: none;
      padding: 20px;
      margin: 10px;
      font-size: 14px;
      outline: 0;
      box-shadow: 0px 2px 4px 0px #000;
      border-radius: 4px;
    }
    button:active {
      box-shadow: 0px 2px 6px 0px #000;
    }
    [ripple] {
      position: relative;
      overflow: hidden;
    }
    [ripple] .ripple--container {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    [ripple] .ripple--container span {
      transform: scale(0);
      border-radius: 100%;
      position: absolute;
      opacity: 0.75;
      background-color: #fff;
      animation: ripple 1000ms;
    }
    @-moz-keyframes ripple {
      to {
        opacity: 0;
        transform: scale(2);
      }
    }
    @-webkit-keyframes ripple {
      to {
        opacity: 0;
        transform: scale(2);
      }
    }
    @-o-keyframes ripple {
      to {
        opacity: 0;
        transform: scale(2);
      }
    }
    @keyframes ripple {
      to {
        opacity: 0;
        transform: scale(2);
      }
    }
<div class="content">
      <a  class="bg--red tx--white" ripple="ripple" href="http://www.google.de">Fail</a>
      <a href="https://www.google.com/" target="_blank"><button class="bg--blue tx--white" ripple="ripple">Info</button></a>
      <a href="https://www.google.com/" target="_blank"><button class="bg--green tx--white" ripple="ripple">Save</button></a>
    </div>

Answer №3

Don't attempt this solution here because stackoverflow has disabled redirection.

(function() { var cleanUp, debounce, i, len, ripple, rippleContainer, ripples, showRipple;

$('.bg--blue').on('click',function(){
window.open("https://stactoverflow.com")
})
$('.bg--green').on('click',function(){
window.open("https://stactoverflow.com")
})

debounce = function(func, delay) {
  var inDebounce;
  inDebounce = undefined;
  return function() {
    var args, context;
    context = this;
    args = arguments;
    clearTimeout(inDebounce);
    return inDebounce = setTimeout(function() {
      return func.apply(context, args);
    }, delay);
  };
};

showRipple = function(e) {
  var pos, ripple, rippler, size, style, x, y;
  ripple = this;
  rippler = document.createElement('span');
  size = ripple.offsetWidth;
  pos = ripple.getBoundingClientRect();
  x = e.pageX - pos.left - (size / 2);
  y = e.pageY - pos.top - (size / 2);
  style = 'top:' + y + 'px; left: ' + x + 'px; height: ' + size + 'px; width: ' + size + 'px;';
  ripple.rippleContainer.appendChild(rippler);
  return rippler.setAttribute('style', style);
};

cleanUp = function() {
  while (this.rippleContainer.firstChild) {
    this.rippleContainer.removeChild(this.rippleContainer.firstChild);
  }
};

ripples = document.querySelectorAll('[ripple]');

for (i = 0, len = ripples.length; i < len; i++) {
  ripple = ripples[i];
  rippleContainer = document.createElement('div');
  rippleContainer.className = 'ripple--container';
  ripple.addEventListener('mousedown', showRipple);
  ripple.addEventListener('mouseup', debounce(cleanUp, 2000));
  ripple.rippleContainer = rippleContainer;
  ripple.appendChild(rippleContainer);
}
 }());
.bg--red {
  background-color: #e74c3c;
}
.tx--red {
  color: #e74c3c;
}
.bg--blue {
  background-color: #00f;
}
.tx--blue {
  color: #00f;
}
.bg--green {
  background-color: #2ecc71;
}
.tx--green {
  color: #2ecc71;
}
.bg--white {
  background-color: #fff;
}
.tx--white {
  color: #fff;
}
body {
  color: #777;
  text-align: center;
  padding: 0 0 0 0;
  margin: 0 0 0 0;
}
button {
  border: none;
  padding: 20px;
  margin: 10px;
  font-size: 14px;
  outline: 0;
  box-shadow: 0px 2px 4px 0px #000;
  border-radius: 4px;
}
button:active {
  box-shadow: 0px 2px 6px 0px #000;
}
[ripple] {
  position: relative;
  overflow: hidden;
}
[ripple] .ripple--container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
[ripple] .ripple--container span {
  transform: scale(0);
  border-radius: 100%;
  position: absolute;
  opacity: 0.75;
  background-color: #fff;
  animation: ripple 1000ms;
}
@-moz-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-webkit-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-o-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
  <a  class="bg--red tx--white" ripple="ripple" href="http://www.google.de">Fail</a>
  <button class="bg--blue tx--white" ripple="ripple">Info</button>
  <button class="bg--green tx--white" ripple="ripple">Save</button>
</div>

Answer №4

Revamp your CSS with the following code

(function() { var cleanUp, debounce, i, len, ripple, rippleContainer, ripples, showRipple;

debounce = function(func, delay) {
  var inDebounce;
  inDebounce = undefined;
  return function() {
    var args, context;
    context = this;
    args = arguments;
    clearTimeout(inDebounce);
    return inDebounce = setTimeout(function() {
      return func.apply(context, args);
    }, delay);
  };
};

showRipple = function(e) {
  var pos, ripple, rippler, size, style, x, y;
  ripple = this;
  rippler = document.createElement('span');
  size = ripple.offsetWidth;
  pos = ripple.getBoundingClientRect();
  x = e.pageX - pos.left - (size / 2);
  y = e.pageY - pos.top - (size / 2);
  style = 'top:' + y + 'px; left: ' + x + 'px; height: ' + size + 'px; width: ' + size + 'px;';
  ripple.rippleContainer.appendChild(rippler);
  return rippler.setAttribute('style', style);
};

cleanUp = function() {
  while (this.rippleContainer.firstChild) {
    this.rippleContainer.removeChild(this.rippleContainer.firstChild);
  }
};

ripples = document.querySelectorAll('[ripple]');

for (i = 0, len = ripples.length; i < len; i++) {
  ripple = ripples[i];
  rippleContainer = document.createElement('div');
  rippleContainer.className = 'ripple--container';
  ripple.addEventListener('mousedown', showRipple);
  ripple.addEventListener('mouseup', debounce(cleanUp, 2000));
  ripple.rippleContainer = rippleContainer;
  ripple.appendChild(rippleContainer);
}
 }());
.bg--red {
  background-color: #e74c3c;
}
.tx--red {
  color: #e74c3c;
}
.bg--blue {
  background-color: #00f;
}
.tx--blue {
  color: #00f;
}
.bg--green {
  background-color: #2ecc71;
}
.tx--green {
  color: #2ecc71;
}
.bg--white {
  background-color: #fff;
}
.tx--white {
  color: #fff;
}
body {
  color: #777;
  text-align: center;
  padding: 0 0 0 0;
  margin: 40px 0 0 0;
}
a {/**change the style on the link not the button**/
  border: none;
  padding: 20px;
  margin: 10px;
  font-size: 14px;
  outline: 0;
  box-shadow: 0px 2px 4px 0px #000;
  border-radius: 4px;
  text-decoration:none;/**remove the link underline**/
}
a:active {/**probabily don't need this**/
  box-shadow: 0px 2px 6px 0px #000;
}
[ripple] {
  position: relative;
  overflow: hidden;
}
[ripple] .ripple--container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
[ripple] .ripple--container span {
  transform: scale(0);
  border-radius: 100%;
  position: absolute;
  opacity: 0.75;
  background-color: #fff;
  animation: ripple 1000ms;
}
@-moz-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-webkit-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@-o-keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
}
@keyframes ripple {
  to {
    opacity: 0;
    transform: scale(2);
  }
} 
<div class="content">
  <a  class="bg--red tx--white" ripple="ripple" href="http://www.google.de">Fail</a>
  <a class="bg--blue tx--white" ripple="ripple"  href="http://www.google.de">Info</a>
  <a class="bg--green tx--white" ripple="ripple"  href="http://www.google.de">Save</a>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason for incorporating the footer within the container?

To see the issue in question, please visit: The footer needs to span the full width. Here is the code snippet for the page: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="container"> <div c ...

When the mouse leaves, the gauge chart component's size will expand

I have encountered a problem while using the react-gauge-chart library in my react project. Within the project, I have integrated a popover component using material-ui and incorporated the gauge chart component from the library within the popover modal. T ...

Having trouble sending values via POST request in Node.js using Express

Currently, I am in the process of learning how to use Express (v4) with Node.js. My main goal right now is to create a basic REST API. This API specifically focuses on one endpoint: /orders. The main functionality I am trying to achieve is the ability to r ...

Implementing AJAX functionality to dynamically show a message on a webpage following an HTTP POST request

After submitting the form, I want to remain on the current page and show a message indicating whether the submission was successful or not. The HTTP POST request is processed like this: app.post('/insertdb', function(request, response) { // in ...

Issue with hidden sourcemap not loading in Chrome or Firefox during Vite build

Transitioning my react application from create-react-app to Vite has resulted in some unexpected behavior with source maps. To learn more about Vite's documentation on source maps, click here. Initially, I was thinking of using sourcemap: true, but th ...

Nodemailer is experiencing difficulties when used within the routes directory

Recently, I encountered an issue with my subscribe form. It functions perfectly when called from app.js where the express server is defined. However, when I move subscribe.js to the routes folder and connect both files, it fails to send emails. Upon pressi ...

Identify the specific element that activated the MutationObserver across multiple elements

After exploring a helpful Stack Overflow post, I discovered that it is feasible to monitor multiple elements using a single MutationObserver object. In order to track the text of two specific elements on a website, I crafted the following script: var secon ...

What is the hierarchy of fields in package.json?

After submitting a pull request to a repository to include a typings field in the package.json file, the maintainer suggested the following modification: - "typings": "./src/index.d.ts", - "main": "./src/index.js" ...

Working with regular expressions on fieldsets in JavaScript jQuery

I'm facing an issue with a JavaScript regexp and I could really use some assistance. Currently, I have an ajax query result saved in a variable (in this case, a regexp) and I am trying to only match the content within the fieldset tag. Here is a sni ...

Issues arise while utilizing the execute method in PHPUnit-Selenium2

When creating Acceptance tests with PhpUnit and its Selenium2 extension, I am attempting to utilize the execute method within the PHPUnit_Extensions_Selenium2TestCase class to run Javascript code that checks if the document is fully loaded. Here is an exa ...

Display the output based on checkbox selection using JavaScript

I am working on a feature where I need to capture checkbox inputs using JavaScript and then store them in a PHP database. Each checkbox corresponds to a specific column in the database. If a checkbox is checked, I want to insert its value into the databa ...

Tips for pinpointing parent elements within a group of various sub child elements

CSS - <div class="windows" id="window'+divCount+'"> <p id="windowName'+divCount+'"></p> <p id="para'+divCount+'">Source</p> </div> <div cla ...

When the jQuery Div is moved to the right, it gradually shrinks in size, yet remains unchanged when

I have been making updates to this page, which you can view here. When you select "Let's Get Started" and then proceed with the right arrows, the divs smoothly move to the left without shrinking. However, when clicking on the back or left arrows, the ...

Unexpected Issue Encountered in JQuery Integration

I recently added jQuery to my HTML file: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> After that, I included a link to my JavaScript file: <script src="public/javascripts/new_javascript.js" type ...

Reacting to each change event, Angular dynamically loads new autocomplete options

I am facing an issue with my form where users need to select a company using mat-select-search. Upon selection, an API call is made with the selected company ID to fetch users from that company for the autocomplete feature in recipient fields. The process ...

Error message indicating that the jQuery script has not been properly defined in a Wordpress environment

Seeking assistance with a JavaScript issue on my WordPress site. I have added an external JS file but it's not functioning properly and throwing errors in the code below. Any help to resolve this would be greatly appreciated. var clock; jquery(docu ...

Activate an event on a separate webpage using jQuery or JavaScript

Currently, I am in the process of working on a project with 2 distinct pages: Index Settings On the Settings page, I have implemented a button to close an element and hide it. However, I am encountering an issue where I want the elements on the Index pa ...

Validation of Single Fields in Angular Reactive Forms

When I validate a reactive form in Angular, I expect the error message to show up beneath the invalid field whenever incorrect data is entered. <form (ngSubmit)=sendQuery() [formGroup]="form"> <div *ngFor='let key of modelKeys&ap ...

Storing HTML form information into a CSV file using Python and Flask

I am struggling with a small .py program that renders 2 HTML pages. One of the pages contains a basic form asking for a name and comment. I am having difficulty figuring out how to take the data entered in the form and store it into a csv file. While I h ...

How can I intercept/manage the back button of the browser in React-router?

Utilizing Material-ui's Tabs, which are controlled, I am implementing them for (React-router) Links in the following manner: <Tab value={0} label="dashboard" containerElement={<Link to="/dashboard/home"/>}/> <Tab value={1} label="users ...