Vue.js + waves-effect

Vue.js

Material Design 谷歌推出了全新的设计语言Material Design。谷歌表示,这种设计语言旨在为手机、平板电脑、台式机和“其他平台”提供更一致、更广泛的“外观和感觉”。(网上copy的)

Materialize 前端框架

<!-- 引用 materialize 的样式 -->
<link href="http://cdn.bootcss.com/materialize/0.97.6/css/materialize.min.css" rel="stylesheet">

Vue.js 自定义指令

// directive
Vue.directive('effect', {
    bind: function() {
        var el = this.el
        el.classList.add('waves-effect')
        this.expression && el.classList.add('waves-' + this.expression)
        function convertStyle(obj) {
            var style = '';
            for (var a in obj) {
                if (obj.hasOwnProperty(a)) {
                    style += (a + ':' + obj[a] + ';');
                }
            }
            return style;
        }
        this.handler = function(e) {
            var ripple = document.createElement('div');
            ripple.classList.add('waves-ripple');
            el.appendChild(ripple);
            var styles = {
                'left': e.layerX + 'px',
                'top': e.layerY + 'px',
                'opacity': 1,
                'transform': 'scale(' + ((el.clientWidth / 100) * 10) + ')',
                'transition-duration': '750ms',
                'transition-timing-function': 'cubic-bezier(0.250, 0.460, 0.450, 0.940)'
            };
            ripple.setAttribute('style', convertStyle(styles));
            setTimeout(function() {
                ripple.setAttribute('style', convertStyle({
                    'opacity': 0,
                    'transform': styles.transform,
                    'left': styles.left,
                    'top': styles.top
                }));
                setTimeout(function() {
                    ripple && el.removeChild(ripple);
                }, 750);
                // 
            }, 450);
        }
        this.el.addEventListener('mousedown', this.handler, false)
    },
    unbind: function() {
        this.el.removeEventListener('mousedown', this.handler)
    }
})

使用

<div id="app">
    <div class="container" style="padding:20px 0">
        <p>
            <button type="button" class="btn btn-large" v-effect="light"> Button effect - light</button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="red"> Button effect - red </button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="yellow"> Button effect - yellow</button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="orange"> Button effect - orange</button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="purple"> Button effect - purple</button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="green"> Button effect - green</button>
        </p>
        <p>
            <button type="button" class="btn btn-large" v-effect="teal"> Button effect - teal</button>
        </p>
    </div>
</div>

<script>
    new Vue({
        el: '#app'
    });
</script>

效果传送门 https://jsfiddle.net/chexian/m02j8gvh/

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