js+svg实现一个按钮提交动画操作反馈效果代码

代码语言:html

所属分类:表单美化

代码描述:js+svg实现一个按钮提交动画操作反馈效果代码

代码标签: 按钮 提交 动画 操作 反馈 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        body {
            font-family: Avenir,'Helvetica Neue', 'Lato', 'Segoe UI', Helvetica, Arial, sans-serif;
            color: #eee;
            background-color: #323232;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }

        a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            border-bottom: 1px solid transparent;
            -webkit-transition: 0.3s;
            transition: 0.3s;
        }
        a:hover {
            color: #fff;
            border-bottom-color: #fff;
        }

        .container {
            text-align: center;
        }

        h1.demo-title {
            position: relative;
            display: inline-block;
            margin: 50px 0 0;
        }
        h1.demo-title:before, h1.demo-title:after {
            content: '';
            position: absolute;
            top: 50%;
            width: 30px;
            height: 1px;
            background-color: rgba(255, 255, 255, 0.8);
        }
        h1.demo-title:before {
            left: -50px;
        }
        h1.demo-title:after {
            right: -50px;
        }

        .demo-subtitle {
            margin: 5px 0 40px;
            color: rgba(255, 255, 255, 0.4);
        }

        .description {
            color: rgba(255, 255, 255, 0.7);
        }

        .showcase {
            position: relative;
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display: flex;
            -webkit-box-align: center;
            -webkit-align-items: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-pack: center;
            -webkit-justify-content: center;
            -ms-flex-pack: center;
            justify-content: center;
            margin-top: 50px;
        }
        .showcase button {
            position: relative;
            display: inline-block;
            min-width: 160px;
            border: none;
            background-color: #3A9AD9;
            color: #ffffff;
            letter-spacing: 1px;
            text-transform: uppercase;
            cursor: pointer;
            padding: 10px 20px;
            margin: 0 10px;
            border-radius: 2px;
            -webkit-transition: 0.3s;
            transition: 0.3s;
        }
        .showcase button:not([disabled]):hover {
            background-color: #4fa7dd;
        }
        .showcase button.bordered {
            background-color: transparent;
            border: 2px solid #3A9AD9;
            padding: 8px 0;
        }
        .showcase button.bordered:not([disabled]):hover {
            background-color: #3A9AD9;
            border-color: transparent;
        }
        .showcase button.control-button {
            width: 170px;
            font-size: 13px;
            visibility: hidden;
            opacity: 0;
            -webkit-transition: 0.5s 0s opacity, 0.1s visibility;
            transition: 0.5s 0s opacity, 0.1s visibility;
        }
        .showcase button.control-button.show-control {
            visibility: visible;
            opacity: 1;
            -webkit-transition: 0.5s 0.4s opacity, 0.3s background, 0.3s border;
            transition: 0.5s 0.4s opacity, 0.3s background, 0.3s border;
        }
        .showcase button.control-button.make-it-fail {
            border-color: #EB7260;
        }
        .showcase button.control-button.make-it-fail:hover {
            background-color: #EB7260;
        }
        .showcase button.control-button.make-it-succeed {
            border-color: #29ABA4;
        }
        .showcase button.control-button.make-it-succeed:hover {
            background-color: #29ABA4;
        }

        .loading-button.open-loading {
            color: rgba(255, 255, 255, 0.8);
        }
        .loading-button.open-loading.infinity {
            padding-top: 80px;
        }
        .loading-button.open-loading svg {
            display: inline-block;
            visibility: visible;
            opacity: 1;
            -webkit-transition: 1s opacity;
            transition: 1s opacity;
            -webkit-transform: translateX(-50%);
            -ms-transform: translateX(-50%);
            transform: translateX(-50%);
        }
        .loading-button.failed {
            background-color: #EB7260;
        }
        .loading-button.succeed {
            background-color: #29ABA4;
        }
        .loading-button.no-transition {
            -webkit-transition: 0s;
            transition: 0s;
        }
        .loading-button.no-transition * {
            -webkit-transition: 0s;
            transition: 0s;
        }
        .loading-button svg {
            visibility: hidden;
            position: absolute;
            left: 50%;
            -webkit-transform: translateX(-50%);
            -ms-transform: translateX(-50%);
            transform: translateX(-50%);
            opacity: 0;
            -webkit-transition: 0s;
            transition: 0s;
        }
        .loading-button svg path {
            stroke-linecap: round;
            stroke-linejoin: round;
            stroke-width: 4;
            fill: none;
        }
        .loading-button svg path.success-path, .loading-button svg path.error-path, .loading-button svg path.error-path2 {
            visibility: hidden;
        }

        .loading-button.top svg {
            top: 10px;
        }
        .loading-button.bottom svg {
            bottom: 10px;
        }
        .loading-button.left svg {
            top: 50%;
            -webkit-transform: scale(0.25) translateY(-50%);
            -ms-transform: scale(0.25) translateY(-50%);
            transform: scale(0.25) translateY(-50%);
            -webkit-transform-origin: 0 0 0;
            -ms-transform-origin: 0 0 0;
            transform-origin: 0 0 0;
            left: 20px;
        }
        .loading-button.right svg {
            top: 50%;
            -webkit-transform: scale(0.25) translateY(-50%);
            -ms-transform: scale(0.25) translateY(-50%);
            transform: scale(0.25) translateY(-50%);
            -webkit-transform-origin: 100% 0 0;
            -ms-transform-origin: 100% 0 0;
            transform-origin: 100% 0 0;
            left: auto;
            right: 20px;
        }
        .loading-button.open-loading.left {
            padding-left: 60px;
        }
        .loading-button.open-loading.right {
            padding-right: 60px;
        }
        .loading-button.open-loading.top svg, .loading-button.open-loading.bottom svg {
            -webkit-transition-delay: 0.2s;
            transition-delay: 0.2s;
        }
        .loading-button.open-loading.circular-loading.top, .loading-button.open-loading.circle-loading.top {
            padding-top: 140px;
        }
        .loading-button.open-loading.circular-loading.bottom, .loading-button.open-loading.circle-loading.bottom {
            padding-bottom: 140px;
        }
        .loading-button.open-loading.infinity-loading.top {
            padding-top: 80px;
        }
        .loading-button.open-loading.infinity-loading.bottom {
            padding-bottom: 80px;
        }

        .loading-options {
            text-align: center;
        }
        .loading-options > div {
            display: inline-block;
            width: 40%;
            cursor: default;
        }
        .loading-options label {
            display: inline-block;
            margin: 0 5px;
            cursor: pointer;
        }
        .loading-options label input {
            display: none;
        }
        .loading-options label input:checked + span {
            background-color: rgba(0, 0, 0, 0.5);
        }
        .loading-options label span {
            display: inline-block;
            padding: 5px 10px;
            font-size: 13px;
            letter-spacing: 1px;
            text-transform: uppercase;
            border-radius: 2px;
            background-color: rgba(0, 0, 0, 0.3);
        }

        .circle-loading circle {
            visibility: hidden;
            opacity: 0;
            -webkit-transition: 0.5s;
            transition: 0.5s;
        }
        .circle-loading.show-circles circle {
            visibility: visible;
            opacity: 1;
        }

    </style>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/classie.js"></script>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/segment.js"></script>
</head>
<body>
    <section class="container">
        <div class="container">
            <h1 class="demo-title">Loading Buttons</h1>

            <div class="description">
                Just press the Send button and then make it fail or succeed
            </div>
            <div class="description">
                Also play with all the possible loaders and positions
            </div>
            <div class="showcase main-showcase">
                <button class="control-button bordered make-it-fail">让它失败</button>
                <button class="loading-button circular-loading top">Send</button>
                <button class="control-button bordered make-it-succeed">让它成功</button>
            </div>
            <div class="loading-options">
                <div class="loaders">
                    <h2>Loaders</h2>
                    <label>
                        <input type="radio" name="loaders" value="circular-loading" checked="checked" />
                        <span>circular</span>
                    </label>
                    <label>
                        <input type="radio" name="loaders" value="circle-loading" />
                        <span>circle</span>
                    </label>
                    <label>
                        <input type="radio" name="loaders" value="infinity-loading" />
                        <span>infinity</span>
                    </label>
                </div>
                <div class="positions">
                    <h2>Positions</h2>
                    <label>
                        <input type="radio" name="positions" value="top" checked="checked" />
                        <span>top</span>
                    </label>
                    <label>
                        <input type="radio" name="positions" value="bottom" />
                        <span>bottom</span>
                    </label>
                    <label>
                        <input type="radio" name="positions" value="left" />
                        <span>left</span>
                    </label>
                    <label>
                        <input type="radio" name="positions" value="right" />
                        <span>right</span>
                    </label>
                </div>
            </div>
        </div>
    </section>
	<script type="text/template" id="circular-loading">
	    <svg width="120px" height="120px">
	        <path class="outer-path" stroke="#fff" d="M 60 60 m 0 -50 a 50 50 0 1 1 0 100 a 50 50 0 1 1 0 -100"></path>
	        <path class="inner-path" stroke="rgba(255, 255, 255, 0.5)" d="M 60 60 m 0 -30 a 30 30 0 1 1 0 60 a 30 30 0 1 1 0 -60"></path>
	        <path class="success-path" stroke="#fff" d="M 60 10 A 50 50 0 0 1 91 21 L 75 45 L 55 75 L 45 65"></path>
	        <path class="error-path" stroke="#fff" d="M 60 10 A 50 50 0 0 1 95 25 L 45 75"></path>
	        <path class="error-path2" stroke="#fff" d="M 60 30 A 30 30 0 0 1 81 81 L 45 45"></path>
	    </svg>
	</script>
	<script type="text/template" id="circle-loading">
	    <svg width="120px" height="120px">
	        <circle r="50" cx="60" cy="60" fill="none" stroke="rgba(255, 255, 255, 0.3)"></circle>
	        <circle r="30" cx="60" cy="60" fill="none" stroke="rgba(255, 255, 255, 0.3)"></circle>
	        <path class="outer-path" stroke="#fff" d="M 60 60 m 0 -50 a 50 50 0 1 1 0 100 a 50 50 0 1 1 0 -100"></path>
	        <path class="inner-path" stroke="#fff" d="M 60 60 m 0 -30 a 30 30 0 1 1 0 60 a 30 30 0 1 1 0 -60"></path>
	        <path class="success-path" stroke="#fff" d="M 60 10 A 50 50 0 0 0 16 36  L 45 65 L 55 75 L 75 45"></path>
	        <path class="error-path" stroke="#fff" d="M 60 10 A 50 50 0 0 0 25 95 L 75 45"></path>
	        <path class="error-path2" stroke="#fff" d="M 60 30 A 30 30 0 0 1 81 81 L 45 45"></path>
	    </svg>
	</script>
	<script type="text/template" id="infinity-loading">
	    <svg width="120px" height="60px">
	        <path class="infinity-path" stroke="#fff" d="M 30 10 a 20 20 0 1 0 0 40 c 20 0 40 -40 60 -40 a 20 20 0 0 1 0 40 c -20 0 -40 -40 -60 -40"></path>
	        <path class="success-path" stroke="#fff" d="M 30 10 C 15 10 35 25 45 35 L 55 45 L 75 15"></path>
	        <path class="error-path" stroke="#fff" d="M 30 10 a 20 20 0 1 0 0 40 Q 40 50 45 45 L 75 15"></path>
	        <path class="error-path2" stroke="#fff" d="M 30 10 Q 40 10 45 15 L 75 45"></path>
	    </svg>
	</script>

    <script>
        function LoadingButton(el, options) {
            this.el = el;
            this.options = options;
            this.init();
        }

        LoadingButton.prototype = {
            // Initialize everything
            init: function() {
                this.infinite = true;
                this.succeed = false;
                this.initDOM();
                this.initSegments();
                this.initEvents();
            },

            // Create an span element with inner text of the button and insert the corresponding SVG beside it
            initDOM: function() {
                this.el.innerHTML = '<span>' + this.el.innerHTML + '</span>';
                this.span = this.el.querySelector('span');
                var div = document.createElement('div');

                div.innerHTML = document.querySelector(this.options.s.........完整代码请登录后点击上方下载按钮下载查看

网友评论0