Material Design for Bootstrap

简朴几行代码,就能够给bs框架增加Material Design作风

结果图:
《Material Design for Bootstrap》

demo: http://jsoncode.github.io/mat…

这是罕见的btn加了Material Design结果
这里采纳的是bootstrap4

引入:bs.css

<link rel="stylesheet" href="css/bootstrap.min.css">
<button class="btn btn-secondary" type="button" materialDesign>Material Design for Bootstrap</button>

你能够看到上面多了一个materialDesign属性,对,等下我们就经由过程这个属性来完成material Design结果

css:

[materialDesign] {
    display: inline-block;
    letter-spacing: .8px;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    position: relative;
    overflow: hidden;
    z-index: 1;
}

.animate-hand{
    height: 134px;
    width: 134px;
    display: block;
    position: absolute;
    background: currentColor;
    opacity: 0.6;
    border-radius: 100%;
    -webkit-transform: scale(0);
    transform: scale(0);
    z-index: 0;
}
.animate-hand.animate {
    -webkit-animation: ripple .5s linear;
    animation: ripple .5s linear;
}

@-webkit-keyframes ripple {
    100% {
        opacity: 0;
        -webkit-transform: scale(4.5);
        transform: scale(4.5);
    }
}

@keyframes ripple {
    100% {
        opacity: 0;
        -webkit-transform: scale(4.5);
        transform: scale(4.5);
    }
}

js:

(function() {
    for (var i = 0, btn; btn = document.querySelectorAll('[materialDesign]')[i++];) {
        btn.addEventListener('click', function(e) {
            var tag = this;
            if (this.getAttribute('materialDesign') === undefined) {
                tag = this.parentNode;
            }
            var div = tag.querySelector(".animate-hand");
            if (!div) {
                div = document.createElement("div");
                tag.appendChild(div);
            }
            div.className = 'animate-hand';
            var x = e.pageX;
            var y = e.pageY;
            var left = tag.offsetLeft;
            var top = tag.offsetTop;
            var height = div.offsetHeight;
            var width = div.offsetWidth;
            div.className = "";

            div.style.left = x - left - width / 2 + "px";
            div.style.top = y - top - height / 2 + "px";
            div.className = "animate-hand animate";
        });
    }
})();

搞定,只需在恣意一个标签上增加materialDesign属性,即可完成该结果

更多殊效后续上传。

第二次修正:
源码已放到github:https://github.com/jsoncode/m…

因为没法处理touch事宜不能猎取中心标签的坐标位置,所以只能用click事宜,而不能用touch类事宜

防备click事宜的滥用,改用mousedown事宜

中心js:

(function() {
    'use strict';
    document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
    function DOMContentLoaded() {
        for (var i = 0, btn; btn = document.querySelectorAll('[materialDesign]')[i++];) {
            btn.addEventListener('mousedown', materialDesignBg);
        }
    }
    function materialDesignBg(e) {
        e.preventDefault();
        e.stopPropagation();
        var tag = this;
        var div = tag.querySelector(".animate-hand");
        if (!div) {
            div = document.createElement("div");
        }
        div.className = 'animate-hand';
        tag.insertBefore(div, tag.firstElementChild);

        var scale = Math.round(tag.offsetWidth / 100) || 1;
        var left = e.layerX;
        var top = e.layerY;

        div.style.left = left + "px";
        div.style.top = top + "px";
        scale = scale > 6 ? 6 : scale;
        div.className = "animate-hand animate ripple_" + (e.changedTouches ? scale + 1 : scale);
        if (tag.nodeName != 'A' && tag.getAttribute("href")) {
            location.href = tag.getAttribute("href");
        }
        return false;
    }
})();

款式延续更新,也能够依据本身喜好的款式从新定义bootstrap

    原文作者:jsoncode
    原文地址: https://segmentfault.com/a/1190000005708521
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞