Dibuat Oleh Devis Wisley
Sunday, September 01, 2024
Create an interactive Button with a Ripple Effect when clicked, which has a dynamic color gradient background. The ripple effect provides interesting visual feedback to users when they interact with the button.
In this blog post, I will walk you through creating an interactive button with an attractive ripple effect and gradient background using HTML, CSS, and JavaScript. We will create a button with a dynamic gradient background that will create a ripple effect when clicked.
With these features, the ripple button project with gradient background can give a professional touch to your user interface!
By creating a ripple effect project on a button with a gradient background using HTML, CSS, and JavaScript, you will gain the following skills:
This project will not only improve your technical skills in web development, but also give you a better understanding of how to combine HTML, CSS, and JavaScript to create professional and aesthetic user interface elements.
Here are the steps to create a ripple effect on a button with a gradient background using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<!-- Coding By CodingIndo - https://codingindo.vercel.app/ -->
<html lang="en">
<head>
<!-- Meta charset to define character encoding -->
<meta charset="UTF-8">
<!-- Meta viewport to set responsive display -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Title for page title -->
<title>Button Ripple Effect</title>
<!-- Stylesheet link to link CSS files -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Container for ripple effect button -->
<div class="button-container">
<!-- Button with ripple effect -->
<button class="ripple-button">Click Me</button>
</div>
<!-- Script to link JavaScript files -->
<script src="script.js"></script>
</body>
</html>
Number | Line of Code | Explanation |
---|---|---|
1 | <!DOCTYPE html> | Declares the document type and version of HTML being used, in this case, HTML5. |
2 | <!-- Coding By CodingIndo - https://codingindo.vercel.app/ --> | A comment specifying the author and their website. |
3 | <html lang="en"> | The opening tag for the HTML document with the lang attribute set to English. |
4 | <head> | The opening tag for the document head section. |
5 | <!-- Meta charset to define character encoding --> | A comment explaining the purpose of the following meta tag. |
6 | <meta charset="UTF-8"> | Sets the character encoding to UTF-8, ensuring that the page displays special characters correctly. |
7 | <!-- Meta viewport to set responsive display --> | A comment explaining the next meta tag. |
8 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | Ensures the page is responsive and scales properly on different devices. |
9 | <!-- Title for page title --> | A comment explaining the purpose of the title tag. |
10 | <title>Button Ripple Effect</title> | Sets the title of the web page as shown on the browser tab. |
11 | <!-- Stylesheet link to link CSS files --> | A comment explaining the purpose of the link tag. |
12 | <link rel="stylesheet" href="styles.css"> | Links an external CSS file called styles.css to the document. |
13 | </head> | Closes the head section. |
14 | <body> | The opening tag for the body section, which contains the content displayed on the page. |
15 | <!-- Container for ripple effect button --> | A comment describing the container for the button. |
16 | <div class="button-container"> | Creates a div element with a class named button-container, which can be used for styling or layout purposes. |
17 | <!-- Button with ripple effect --> | A comment explaining the button’s function. |
18 | <button class="ripple-button">Click Me</button> | Creates a button element with the text "Click Me" and the class ripple-button for adding custom styling or JavaScript functionality. |
19 | </div> | Closes the div element. |
20 | <!-- Script to link JavaScript files --> | A comment explaining the purpose of the script tag. |
21 | <script src="script.js"></script> | Links an external JavaScript file named script.js to add interactivity or logic. |
22 | </body> | Closes the body section. |
23 | </html> | Closes the HTML document. |
/* Sets the body display to be centered and have a background color */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
/* Sets the relative position for the button container */
.button-container {
position: relative;
}
/* Set the appearance of the ripple button */
.ripple-button {
position: relative;
overflow: hidden;
background: linear-gradient(45deg, #ff6b6b, #f94d6a, #ffca3a, #6a4df9, #3ab7ff);
background-size: 300% 300%;
border: none;
border-radius: 8px;
padding: 15px 30px;
color: white;
font-size: 18px;
cursor: pointer;
outline: none;
transition: background-position 1s;
}
/* Sets the hover effect on the ripple button */
.ripple-button:hover {
background-position: right center;
}
/* Sets the after effect on the ripple button */
.ripple-button::after {
content: '';
position: absolute;
border-radius: 50%;
width: 100px;
height: 100px;
background-color: rgba(255, 255, 255, 0.6);
opacity: 0;
transform: scale(1);
transition: transform 0.5s, opacity 1s;
}
/* Sets the ripple span display on the button */
.ripple-button span.ripple {
position: absolute;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.5);
transform: scale(0);
animation: ripple-animation 0.6s linear;
pointer-events: none;
}
/* Set the ripple animation */
@keyframes ripple-animation {
to {
transform: scale(4);
opacity: 0;
}
}
Number | Line of Code | Explanation |
---|---|---|
1 | /* Sets the body display to be centered and have a background color */ | A comment explaining the styles applied to the body. |
2 | body { | Starts the body styling block. |
3 | display: flex; | Sets the body display mode to flex, enabling flexbox properties. |
4 | justify-content: center; | Horizontally centers the content within the body. |
5 | align-items: center; | Vertically centers the content within the body. |
6 | height: 100vh; | Sets the body height to the full viewport height. |
7 | margin: 0; | Removes any default margin from the body. |
8 | background-color: #f0f0f0; | Sets the background color of the body to a light gray (#f0f0f0). |
9 | } | Ends the body styling block. |
10 | /* Sets the relative position for the button container */ | A comment describing the purpose of .button-container. |
11 | .button-container { | Starts the .button-container styling block. |
12 | position: relative; | Sets the position of the .button-container to relative for positioning child elements. |
13 | } | Ends the .button-container styling block. |
14 | /* Set the appearance of the ripple button */ | A comment explaining the styling for .ripple-button. |
15 | .ripple-button { | Starts the .ripple-button styling block. |
16 | position: relative; | Sets the position to relative for containing the pseudo-element (::after). |
17 | overflow: hidden; | Hides any content that overflows the button boundaries. |
18 | background: linear-gradient(45deg, #ff6b6b, #f94d6a, #ffca3a, #6a4df9, #3ab7ff); | Sets a multi-color gradient background at a 45-degree angle. |
19 | background-size: 300% 300%; | Enlarges the gradient background for smoother animation. |
20 | border: none; | Removes the default border from the button. |
21 | border-radius: 8px; | Rounds the corners of the button. |
22 | padding: 15px 30px; | Adds padding inside the button. |
23 | color: white; | Sets the button text color to white. |
24 | font-size: 18px; | Sets the font size of the button text. |
25 | cursor: pointer; | Changes the cursor to a pointer when hovered over the button. |
26 | outline: none; | Removes the outline when the button is focused. |
27 | transition: background-position 1s; | Adds a smooth transition effect for the background position. |
28 | } | Ends the .ripple-button styling block. |
29 | /* Sets the hover effect on the ripple button */ | A comment explaining the hover effect. |
30 | .ripple-button:hover { | Starts the hover effect styling block. |
31 | background-position: right center; | Moves the background position when hovered to create an animation. |
32 | } | Ends the hover effect styling block. |
33 | /* Sets the after effect on the ripple button */ | A comment explaining the pseudo-element styling. |
34 | .ripple-button::after { | Starts the ::after pseudo-element styling block for .ripple-button. |
35 | content: ''; | Inserts an empty content string. |
36 | position: absolute; | Positions the element absolutely within the button. |
37 | border-radius: 50%; | Gives the element a circular shape. |
38 | width: 100px; | Sets the width of the pseudo-element. |
39 | height: 100px; | Sets the height of the pseudo-element. |
40 | background-color: rgba(255, 255, 255, 0.6); | Sets the background color with transparency. |
41 | opacity: 0; | Makes the element initially transparent. |
42 | transform: scale(1); | Sets the initial scale to normal size. |
43 | transition: transform 0.5s, opacity 1s; | Adds transitions for scale and opacity. |
44 | } | Ends the pseudo-element styling block. |
45 | /* Sets the ripple span display on the button */ | A comment explaining the styling for the ripple span. |
46 | .ripple-button span.ripple { | Starts the .ripple span styling block. |
47 | position: absolute; | Positions the span absolutely within the button. |
48 | border-radius: 50%; | Gives the span a circular shape. |
49 | background-color: rgba(255, 255, 255, 0.5); | Sets a semi-transparent white background. |
50 | transform: scale(0); | Sets the initial scale to 0 for animation. |
51 | animation: ripple-animation 0.6s linear; | Applies the ripple-animation for 0.6s in a linear fashion. |
52 | pointer-events: none; | Prevents the span from capturing pointer events. |
53 | } | Ends the .ripple span styling block. |
54 | /* Set the ripple animation */ | A comment explaining the keyframe animation. |
55 | @keyframes ripple-animation { | Starts the keyframes block for the ripple-animation. |
56 | to { | Specifies the end state of the animation. |
57 | transform: scale(4); | Expands the element 4 times its original size. |
58 | opacity: 0; | Makes the element fully transparent by the end of the animation. |
59 | } | Ends the to keyframe block. |
60 | } | Ends the @keyframes block. |
// Add an event listener for clicks on elements with class 'ripple-button'
document.querySelector('.ripple-button').addEventListener('click', function (e) {
// Create a new 'span' element for the ripple effect
const ripple = document.createElement('span');
// Get the clicked button element
const button = e.currentTarget;
// Get the size and position of the button
const rect = button.getBoundingClientRect();
// Calculate ripple size based on button size
const size = Math.max(rect.width, rect.height);
// Calculate the x position of the ripple
const x = e.clientX - rect.left - size / 2;
// Calculate the y position of the ripple
const y = e.clientY - rect.top - size / 2;
// Set the ripple size
ripple.style.width = ripple.style.height = `${size}px`;
// Set the left position of the ripple
ripple.style.left = `${x}px`;
// Set the top position of the ripple
ripple.style.top = `${y}px`;
// Add 'ripple' class to span element
ripple.classList.add('ripple');
// Add ripple element to the button
button.appendChild(ripple);
// Remove the ripple element after the animation is complete
ripple.addEventListener('animationend', () => {
ripple.remove();
});
});
Number | Line of Code | Explanation |
---|---|---|
1 | // Add an event listener for clicks on elements with class 'ripple-button' | A comment explaining that an event listener will be added to handle click events on elements with the class ripple-button. |
2 | document.querySelector('.ripple-button').addEventListener('click', function (e) { | Selects the first element with the class ripple-button and adds a click event listener that triggers an anonymous function when the button is clicked. |
3 | // Create a new 'span' element for the ripple effect | A comment explaining the creation of a new span element for the ripple effect. |
4 | const ripple = document.createElement('span'); | Creates a new span element and assigns it to the variable ripple. |
5 | // Get the clicked button element | A comment explaining that the current button being clicked is retrieved. |
6 | const button = e.currentTarget; | Assigns the button that was clicked (e.currentTarget) to the variable button. |
7 | // Get the size and position of the button | A comment explaining that the button's size and position are being retrieved. |
8 | const rect = button.getBoundingClientRect(); | Retrieves the size and position of the button relative to the viewport and assigns it to rect. |
9 | // Calculate ripple size based on button size | A comment explaining how the size of the ripple is calculated. |
10 | const size = Math.max(rect.width, rect.height); | Calculates the ripple's size as the larger of the button's width or height. |
11 | // Calculate the x position of the ripple | A comment explaining how the x-coordinate for the ripple's position is calculated. |
12 | const x = e.clientX - rect.left - size / 2; | Calculates the x-coordinate for the ripple's position relative to the button. |
13 | // Calculate the y position of the ripple | A comment explaining how the y-coordinate for the ripple's position is calculated. |
14 | const y = e.clientY - rect.top - size / 2; | Calculates the y-coordinate for the ripple's position relative to the button. |
15 | // Set the ripple size | A comment explaining that the size of the ripple element is being set. |
16 | ripple.style.width = ripple.style.height = `${size}px`; | Sets both the width and height of the ripple element to the calculated size. |
17 | // Set the left position of the ripple | A comment explaining that the left position of the ripple is being set. |
18 | ripple.style.left = `${x}px`; | Sets the left CSS property of the ripple element. |
19 | // Set the top position of the ripple | A comment explaining that the top position of the ripple is being set. |
20 | ripple.style.top = `${y}px`; | Sets the top CSS property of the ripple element. |
21 | // Add 'ripple' class to span element | A comment explaining that the ripple class is added to the element for styling and animation. |
22 | ripple.classList.add('ripple'); | Adds the ripple class to the ripple element. |
23 | // Add ripple element to the button | A comment explaining that the ripple element is appended to the button. |
24 | button.appendChild(ripple); | Appends the ripple element to the button as a child. |
25 | // Remove the ripple element after the animation is complete | A comment explaining that the ripple element will be removed after the animation finishes. |
26 | ripple.addEventListener('animationend', () => { | Adds an event listener to the ripple element that listens for the animationend event. |
27 | ripple.remove(); | Removes the ripple element from the DOM once the animation ends. |
28 | }); | Closes the event listener's function block. |
29 | }); | Closes the click event listener function block. |
By keeping these additional notes in mind, you can ensure that the ripple effect on your buttons is not only visually appealing but also optimal for user experience.
In this tutorial, we have learned how to create a button with an attractive ripple effect and gradient background using HTML, CSS, and JavaScript. By following the steps outlined, you can enhance the user experience on your app or website by providing dynamic visual feedback.
Devis Wisley atau yang dikenal sebagai DevCode merupakan seorang Founder & Developer CodingIndo, sebuah komunitas dan platform edukasi teknologi yang berfokus pada pengembangan keterampilan pemrograman. Dengan passion-nya di dunia coding, DevCode berkomitmen untuk membantu para developer lokal meningkatkan kemampuan teknis melalui konten edukatif, tutorial, dan kolaborasi open-source.
Dibawah kepemimpinannya, CodingIndo terus berkembang menjadi wadah bagi programmer pemula maupun profesional untuk saling berbagi ilmu, mengikuti tren teknologi terbaru, dan membangun jaringan di industri IT. Selain aktif mengembangkan proyek-proyek inovatif, DevCode juga sering membagikan insight seputar pengembangan software, tips karir tech, serta best practices dalam dunia pemrograman.
Thursday, October 17, 2024
Sunday, September 01, 2024
Saturday, August 31, 2024
Sunday, September 08, 2024
LEAVE A REPLY