SVG Button Animation
How do I make an svg button animation?
Just playing with some SVG interaction. Inspired by this pen: http://codepen.io/fusco/pen/bvAwE (Admittedly, that effect is more awesome, but the JS in mine makes it more versatile). . What is a svg button animation? How do you make a svg button animation? This script and codes were developed by Jon Christensen on 03 August 2022, Wednesday.
SVG Button Animation - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>SVG Button Animation</title> <script src="http://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>SVG Animated Buttons</h1>
<h2>Animate with SVG baby!</h2>
<p>Doing the SVG line animation with variable size buttons could be hard, but with a little JS, you can use any size button and get the same effect without any math on your part. Because thinking is for losers.</p>
<div class="container"> <p>
<button type="button" class="btn btn-svg"> <svg class="btn_svg" version="1.1" viewBox="0 0 30 20" preserveAspectRatio="none"> <rect class="btn_svg_shape" width="30" height="20"/> </svg> <span>This is a button</span>
</button> </p> <p>
<button type="button" class="btn btn-svg btn-big"> <svg class="btn_svg" version="1.1" viewBox="0 0 30 20" preserveAspectRatio="none"> <rect class="btn_svg_shape" width="30" height="20"/> </svg> <span>This is a button</span>
</button> </p> <p>
<button type="button" class="btn btn-svg btn-small"> <svg class="btn_svg" version="1.1" viewBox="0 0 30 20" preserveAspectRatio="none"> <rect class="btn_svg_shape" width="30" height="20"/> </svg> <span>This is a button</span>
</button> </p>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
SVG Button Animation - Script Codes CSS Codes
.btn { position: relative; overflow: hidden; background: none; padding: 1em; background-color: transparent; -webkit-transition: background-color 250ms ease-in; transition: background-color 250ms ease-in; font-size: 1.5rem; color: orange; -webkit-transition-property: background-color, color; transition-property: background-color, color; -webkit-transition-duration: 250ms; transition-duration: 250ms; -webkit-transition-timing-function: ease-in-out; transition-timing-function: ease-in-out; -webkit-transition-delay: 0; transition-delay: 0; border: none;
}
.btn.btn-small { font-size: 1rem;
}
.btn.btn-big { font-size: 2rem;
}
.btn:before { content: ""; display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 1px solid orange; opacity: 1; -webkit-transition-property: opacity; transition-property: opacity; -webkit-transition-duration: 250ms; transition-duration: 250ms; -webkit-transition-timing-function: ease-in-out; transition-timing-function: ease-in-out; -webkit-transition-delay: 0; transition-delay: 0;
}
.btn > span { position: relative;
}
.btn .btn_svg { position: absolute; top: 0; left: 0; width: 100%; height: 100%;
}
.btn .btn_svg_shape { stroke: orange; stroke-width: 2px; stroke-linecap: round; fill: transparent;
}
.btn:hover { -webkit-transition-delay: 250ms; transition-delay: 250ms; background-color: rgba(0, 0, 0, 0.4); color: white;
}
.btn:hover:before { opacity: 0;
}
.btn:hover .btn_svg_shape { -webkit-transition-property: stroke-dasharray, stroke-dashoffset; transition-property: stroke-dasharray, stroke-dashoffset; -webkit-transition-duration: 500ms; transition-duration: 500ms; -webkit-transition-timing-function: ease-in-out; transition-timing-function: ease-in-out; -webkit-transition-delay: 0; transition-delay: 0; stroke-dashoffset: 0 !important; -webkit-animation: 500ms pencil ease-in forwards; animation: 500ms pencil ease-in forwards;
}
/* Stroke animation. Use ems to scale :) */
@-webkit-keyframes pencil { 0% { stroke-width: 0.5em; } 75% { stroke-width: 0.25em; } 100% { stroke-width: 0.1em; }
}
@keyframes pencil { 0% { stroke-width: 0.5em; } 75% { stroke-width: 0.25em; } 100% { stroke-width: 0.1em; }
}
/* Transition mixin, cause lazy */
/* W00t border-box */
* { box-sizing: border-box;
}
*:after,
*:before { box-sizing: border-box;
}
/* Make things perty */
html { height: 100%;
}
body { font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; background: url(http://www.jmchristensendesign.com/wp-content/themes/jmcdsn/images/intro_default-background.jpg); color: #fff; height: 100%; padding-top: 2em; text-align: center;
}
h1,
h2 { margin: 0; text-transform: uppercase; text-shadow: 0 0 0.5em black;
}
h2 { font-weight: 300;
}
input { border: 1px solid #666; background: #333; color: #fff; padding: 0.5em; box-shadow: none; outline: none !important; margin: 1em auto; text-align: center;
}
a { color: orange; text-decoration: none; -webkit-transition: color 250ms ease-in-out; transition: color 250ms ease-in-out;
}
a:hover { color: yellow;
}
.container { display: block; margin: 2em 0;
}
p { max-width: 40em; margin: 1em auto;
}
SVG Button Animation - Script Codes JS Codes
jQuery(document).ready(function(){ $('.btn-svg').each(function(){ var $this = $(this), width = $this.outerWidth(), height = $this.outerHeight(), $svg = $this.find('svg'), $rect = $svg.find('rect'), totalPerimeter = width*2+height*2; $svg[0].setAttribute('viewBox', '0 0 '+width+' '+height); $rect.attr('width', width); $rect.attr('height', height); $rect.css({ strokeDashoffset: totalPerimeter, strokeDasharray: totalPerimeter }); });
});
Developer | Jon Christensen |
Username | JMChristensen |
Uploaded | August 03, 2022 |
Rating | 4.5 |
Size | 4,333 Kb |
Views | 58,696 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
CSS Banners | 2,548 Kb |
SVG Circle Progress | 3,368 Kb |
Panel Navigation | 4,288 Kb |
Graphs and Snap.svg | 2,870 Kb |
Custom Stagger Start Point | 4,001 Kb |
Responsive SVG Overlay | 2,674 Kb |
Section Diagonal Areas | 2,810 Kb |
Animated gradient text | 3,791 Kb |
Image Cover | 3,914 Kb |
Vue JS Filtering | 9,746 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Border-radius animation | Yukulele | 2,480 Kb |
The Fly | GianlucaGuarini | 3,405 Kb |
CSS3 Fullscreen Background Slideshow | Leetech | 3,435 Kb |
Print element on a page | Mrs_snow | 2,081 Kb |
The Walking Raymarcher | Xorxor_hu | 7,934 Kb |
Donald Trump - The New Yorker | Agbales | 2,502 Kb |
Spiralator 9000 | AdmiralPotato | 4,671 Kb |
Yuliya v krylova | Rafszul | 37,351 Kb |
Scifi-style Interative Form | Aaronchuo | 4,566 Kb |
Pattern lab logo | TimPietrusky | 3,666 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!