<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="svgContent">
<svg id="svg-device" xmlns="http://www.w3.org/2000/svg" viewBox="200 0 502.9 149.4" preserveAspectRatio="xMinYMin meet">
<!-- browser -->
<!-- Phone -->
<path stroke="#C8D2DC" stroke-width="0.2px" fill="#0581BA" d="M498.5 149.4H422c-1.3 0-2.4-.5-3.2-1.4-.8-.8-2.6-2.9-2.6-4.1l1.3-136.8c0-2.5 2-4.5 4.5-4.5h76.5c1.3 0 1-1.2 1.9-.3.7.8 2.6 2.6 2.6 4.8v137.8c-.1 2.5-2.1 4.5-4.5 4.5z"/>
<path fill="#D0EDF5" d="M497.3 147.6h-77c-2.3 0-4.2-1.9-4.2-4.2V5.1c0-2.3 1.9-4.2 4.2-4.2h77c2.3 0 4.2 1.9 4.2 4.2v138.2c0 2.4-1.9 4.3-4.2 4.3z"/>
<path stroke="#C8D2DC" stroke-width="0.2px" fill="#68B5D7" d="M492.9 119.7h-68.3c-1.2 0-2.2-1-2.2-2.2V9.5c0-1.2 1-2.2 2.2-2.2h68.3c1.2 0 2.2 1 2.2 2.2v108c.1 1.2-.9 2.2-2.2 2.2z"/>
<path fill="#0581BA" d="M468.5 136.5H449c-1.3 0-2.4-1.1-2.4-2.4v-2.3c0-1.3 1.1-2.4 2.4-2.4h19.5c1.3 0 2.4 1.1 2.4 2.4v2.3c0 1.3-1.1 2.4-2.4 2.4z"/>
<path opacity=".3" fill="#fff" d="M415.8 88L486.5.4h-70.7s1 87.6 0 87.6z"/>
</svg>
</div>
</body>
</html>
/**
* cobrasvg
*
*
* 一个用来使用SVG的路径(path)来制作动画效果的插件
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright 2015,janily
*
*/
(function(window) {
'use strict';
/**
* 针对不支持SVG的浏览器的检测,如果不支持svg的话,会在html元素上添加一个noSvg的class,反之则添加一个svg的class
* 可以使用这个class来做一些降级处理,如不支持svg的浏览器,则直接显示一张png或者是jpg图片
* 参考:http://stackoverflow.com/questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser
*/
cobrasvg.prototype._supportSvg = function() {
return !!document.createElementNS && !! document.createElementNS('http://www.w3.org/2000/svg', "svg").createSVGRect;
}
/* 探测浏览器种类
* 使用JavaScript来检测浏览器动画事件是否完成
* 参考http://davidwalsh.name/css-animation-callback
*/
function whichTransitionEvent() {
var t;
var el = document.createElement('fakeelement');
var transitions = {
'transition': 'transitionend',
'OTransition': 'oTransitionEnd',
'MozTransition': 'transitionend',
'WebkitTransition': 'webkitTransitionEnd'
}
for (t in transitions) {
if (el.style[t] !== undefined) {
return transitions[t];
}
}
}
var transitionEvent = whichTransitionEvent();
/**
* 扩展
*/
function extend(a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
/**
* 构造函数
*/
function cobrasvg(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}
/**
* 配置选项
* 指定要产生动画效果SVG元素的ID,默认为svg
* 是否开启填充动画效果
*/
cobrasvg.prototype.options = {
elementId: "svg", //指定要产生path动画效果的SVG元素的ID
fillPath: true //是否开启填充的动画效果
}
/**
* cobrasvg _init
* 初始化方法
*/
cobrasvg.prototype._init = function() {
if (!this._supportSvg()) {
document.documentElement.className = "noSvg";
} else {
document.documentElement.className = "svg";
}
this.svg = document.getElementById(this.options.elementId);
this.fillDraw = this.options.fillPath;
this.paths = this.svg.querySelectorAll("path");
this._initAnimation();
}
/**
* cobrasvg _initAnimation()
* 动画方法,主要是初始化一些属性的值,首先是获取path元素的长度;
* 然后设置path的透明度。
*/
cobrasvg.prototype._initAnimation = function() {
for (var i = 0; i < this.paths.length; i++) {
var path = this.paths[i];
var length = path.getTotalLength();
// 重置透明度
path.style.fillOpacity = 0;
path.style.strokeOpacity = 1;
// 重置transition
path.style.transition = path.style.transitionEvent = "none";
// 重置path的strokeDasharray和strokeDashoffset属性
path.style.strokeDasharray = length + " " + length;
path.style.strokeDashoffset = length;
path.getBoundingClientRect();
// 应用transition
path.style.transition = path.style.transitionEvent = "stroke-dashoffset 2s ease-in-out";
// 设置strokeDashoffset的值为0
path.style.strokeDashoffset = 0;
// 是否填充路径
if(this.fillDraw == true) {
this._fillPath(path);
}
}
}
/**
* cobrasvg _fillPath()
*
* 重置transition并设置路径的透明度
*
*/
cobrasvg.prototype._fillPath = function(path) {
path.addEventListener(transitionEvent, function() {
// 重置transition
path.style.transition = path.style.transitionEvent = "none";
path.style.transition = path.style.transitionEvent = "fill-opacity 1s ease-in-out, stroke-opacity 1s ease-in-out";
// 修改透明度
path.style.fillOpacity = 1;
path.style.strokeOpacity = 0;
});
}
/**
* 添加命名空间
*/
window.cobrasvg = cobrasvg;
})(window);
(function() {
var myAnimation = new cobrasvg({
elementId: "svg-device"
});
})();
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |