Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
 <div class="svgContent">
       <svg xmlns="http://www.w3.org/2000/svg" id="svg" class="svg" viewBox="0 0 960 480" preserveAspectRatio="xMinYMin meet">
        <path fill="#B4BEC8" stroke="#B4BEC8" stroke-width="2px" stroke-miterlimit="10" d="M570.14 440.2l-29.165-28.99c-7.103-8.5-6.152-36.718-6.02-40.665H425.048c.133 3.947 1.082 32.164-6.018 40.666l-29.166 28.99c-1.237 1.404-1.712 2.505-1.623 3.37h-.054c.76 7.727 6.664 6.332 13.607 6.332H558.01c6.696 0 12.412 1.27 13.493-5.56.58-.953.274-2.282-1.364-4.14z"/>
        
        <path fill="#C8D2DC" stroke="#C8D2DC" stroke-width="2px" stroke-miterlimit="10" d="M727.488 355.125c0 8.514-6.597 15.42-14.738 15.42h-465.5c-8.14 0-14.74-6.906-14.74-15.42V45.42c0-8.517 6.6-15.42 14.74-15.42h465.5c8.142 0 14.738 6.903 14.738 15.42v309.705z"/>
        
        <path fill="#fff" stroke="#C8D2DC" stroke-width="2px" stroke-miterlimit="10" d="M489.01 343.713c-.042-4.223 3.447-6.254 3.605-6.352-1.963-2.866-5.018-3.263-6.102-3.31-2.602-.26-5.074 1.53-6.39 1.53s-3.356-1.49-5.506-1.448c-2.836.04-5.445 1.645-6.907 4.182-2.942 5.11-.75 12.672 2.116 16.814 1.4 2.02 3.072 4.305 5.268 4.22 2.114-.08 2.913-1.362 5.467-1.362 2.556 0 3.274 1.363 5.51 1.322 2.273-.04 3.716-2.064 5.105-4.098 1.61-2.35 2.273-4.63 2.313-4.748-.05-.02-4.434-1.7-4.48-6.75M484.807 331.31c1.168-1.41 1.953-3.37 1.738-5.327-1.68.068-3.713 1.12-4.916 2.53-1.08 1.247-2.027 3.245-1.77 5.16 1.87.143 3.784-.95 4.947-2.362"/>
        
        <path fill="#3C4650" stroke="#3C4650" stroke-width="2px" stroke-miterlimit="10" d="M727.488 315.527V45.982c0-8.828-6.597-15.982-14.738-15.982h-465.5c-8.14 0-14.74 7.155-14.74 15.982v269.545H727.49z"/>
        
        <path fill="#141E28" stroke="#141E28" stroke-width="2px" stroke-miterlimit="10" d="M251.2 48.887h457.205v245.52H251.2z"/>
      </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"
        });
      })();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
janilypro
0viewers