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>
 <svg id="svg-device" xmlns="http://www.w3.org/2000/svg" viewBox="-150 0 502.9 149.4" enable-background="new 0 0 502.9 149.4">
    <!-- Browser --> 
    <g>
      <!-- Shadow -->
      <path fill="#0581BA" d="M201.2 146H5.3c-1.8 0-5.2-3.3-5.2-4.9L2.5 5.3c0-1.5 1.3-2.8 2.8-2.8l194-2.4c1.5 0 4.7 2.2 4.7 5.2v137.9c0 1.5-1.3 2.8-2.8 2.8z"/>
      <!-- Browser frame -->
      <path stroke="#C8D2DC" stroke-width="0.2px" fill="#68B5D7" d="M199.3 143.5H2.2c-1.2 0-2.2-1-2.2-2.2V2.2C0 1 1 0 2.2 0h197c1.2 0 2.2 1 2.2 2.2v139c.1 1.3-.9 2.3-2.1 2.3z"/>
      <!-- Window -->
      <path stroke="#C8D2DC" stroke-width="0.2px" fill="#D0EDF5" d="M192.7 136.8H8.9c-1.2 0-2.3-1-2.3-2.2V28c0-1.2 1-2.2 2.3-2.2h183.8c1.2 0 2.2 1 2.2 2.2v106.5c.1 1.2-.9 2.3-2.2 2.3z"/>
      <path stroke="#C8D2DC" stroke-width="0.2px" fill="#D0EDF5" d="M10 8.9h136.3c1.3 0 2.4 1.1 2.4 2.4v2.3c0 1.3-1.1 2.4-2.4 2.4H10c-1.3 0-2.4-1.1-2.4-2.4v-2.3c0-1.3 1.1-2.4 2.4-2.4z"/>
      <!-- Optimizely logo -->
      <path stroke="#C8D2DC" stroke-width="0.2px" fill="#68B5D7" d="M141.8 47.7L129 52.3c-6.6-7.5-16.2-12.1-27.4-12.1-22.3 0-42.2 18.4-44.5 41.1-2.3 22.7 14 41 36.3 41s42.2-18.4 44.5-41c.8-7.7-.6-14.9-3.6-21.1l7.5-12.5zm-9.5 29.5c-1.7 17.3-17.5 31.3-35.2 31.3-17.7 0-30.7-14-29-31.3 1.7-17.3 17.5-31.3 35.2-31.3 10.1 0 18.6 4.5 23.8 11.6l8.5-5.1-7.1 7.3-5.1 5.3-28.3 29.6c2.2.9 4.7 1.4 7.4 1.4 11.6 0 21.9-9.1 23-20.4.2-2.4 0-4.7-.5-6.8l4.8-6.9c2.1 4.5 3.1 9.7 2.5 15.3zM106.5 55.1c-11.6 0-21.9 9.1-23 20.4-.3 2.8 0 5.5.9 7.9L121 61.3c-3.5-3.8-8.6-6.2-14.5-6.2z"/>
    </g>
  </svg>
</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

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

Dismiss x
public
Bin info
janilypro
0viewers