Skip to content

Instantly share code, notes, and snippets.

@tmthrgd
Last active September 2, 2016 09:59
Show Gist options
  • Save tmthrgd/89f82ce5d6be184c4cb2e9dfed5ac168 to your computer and use it in GitHub Desktop.
Save tmthrgd/89f82ce5d6be184c4cb2e9dfed5ac168 to your computer and use it in GitHub Desktop.
Animated input field borders.
<!doctype html>
<html lang=en>
<!--
(c) Tom Thorogood 2016
https://tomthorogood.co.uk
Copyright 2016 Tom Thorogood. All rights reserved.
Use of this source code is governed by a
Modified BSD License license that can be found in
the LICENSE file.
-->
<head>
<meta charset=utf-8>
<title>Input field borders</title>
<style>
.inputsvg {
display: block;
margin: 1em 0;
width: 202px;
height: 32px;
}
.inputsvg path {
fill: none;
stroke: blue;
stroke-width: 1px;
}
.inputsvg input {
width: 200px;
height: 30px;
box-sizing: border-box;
margin: 0;
padding: 4px;
border: none;
}
</style>
</head>
<body>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M0 0 L201 0 L201 31 L0 31 Z" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M0 31 L0 0 L201 0 L201 31 Z" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M100 0 L201 0 L201 31 L0 31 L0 0 Z" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M0 15 L0 0 L201 0 L201 31 L0 31 Z" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M0 0 L201 0 L201 31" />
<path d="M201 31 L0 31 L0 0" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<svg class=inputsvg viewBox="0 0 202 32">
<path d="M0 32 L0 0 L202 0" />
<path d="M202 0 L202 32 L0 32" />
<foreignObject x=1 y=1 width=200 height=30>
<input type=text>
</foreignObject>
</svg>
<script>
var paths = Array.prototype.slice.call(document.querySelectorAll('.inputsvg path'));
paths.forEach(function forEach(path) {
var length = path.getTotalLength();
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
});
// triggering a reflow so styles are calculated in their
// start position, so they animate from here
paths[0].getBoundingClientRect();
paths.forEach(function forEach(path) {
path.style.transition = 'stroke-dashoffset 2s linear';
path.style.strokeDashoffset = 0;
path.style.transition += ', stroke 2s linear';
path.style.stroke = 'red';
});
</script>
</body>
</html>
Copyright (c) 2016, Tom Thorogood.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Tom Thorogood nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment