美文网首页
Xx-vue实现点击水波纹涟漪效果之自定义指令

Xx-vue实现点击水波纹涟漪效果之自定义指令

作者: 风中凌乱的男子 | 来源:发表于2023-06-03 22:15 被阅读0次
    目录概览:
    |____
    |____directive
    | |____waves.css
    | |____waves.js
    |____main.js
    
    waves.css
    .waves-ripple {
        position: absolute;
        border-radius: 100%;
        background-image: radial-gradient(circle, rgba(255, 255, 255, .35) 100%, rgba(0, 0, 0, .15) 100%);
        background-clip: padding-box;
        pointer-events: none;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        -webkit-transform: scale(0);
        -ms-transform: scale(0);
        transform: scale(0);
        opacity: 1;
    }
    
    .waves-ripple.z-active {
        opacity: 0;
        -webkit-transform: scale(2);
        -ms-transform: scale(2);
        transform: scale(2);
        -webkit-transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out;
        transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out;
        transition: opacity 1.2s ease-out, transform 0.6s ease-out;
        transition: opacity 1.2s ease-out, transform 0.6s ease-out, -webkit-transform 0.6s ease-out;
    }
    
    waves.js
    import './waves.css';
    
    const vueWaves = {};
    vueWaves.install = (Vue, options = {}) => {
      Vue.directive('waves', {
        bind(el, binding) {
          el.addEventListener('click', e => {
            const customOpts = Object.assign(options, binding.value);
            const opts = Object.assign({
                ele: el, // 波纹作用元素
                type: 'hit', // hit点击位置扩散center中心点扩展
                color: 'rgba(0, 0, 0, 0.15)' // 波纹颜色
              }, customOpts),
              target = opts.ele;
            if (target) {
              target.style.position = 'relative';
              target.style.overflow = 'hidden';
              const rect = target.getBoundingClientRect();
              let ripple = target.querySelector('.waves-ripple');
              if (!ripple) {
                ripple = document.createElement('span');
                ripple.className = 'waves-ripple';
                ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px';
                target.appendChild(ripple);
              } else {
                ripple.className = 'waves-ripple';
              }
              switch (opts.type) {
                case 'center':
                  ripple.style.top = (rect.height / 2 - ripple.offsetHeight / 2) + 'px';
                  ripple.style.left = (rect.width / 2 - ripple.offsetWidth / 2) + 'px';
                  break;
                default:
                  ripple.style.top = (e.pageY - rect.top - ripple.offsetHeight / 2 - document.body.scrollTop) + 'px';
                  ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.body.scrollLeft) + 'px';
              }
              ripple.style.backgroundColor = opts.color;
              ripple.className = 'waves-ripple z-active';
              return false;
            }
          }, false);
        }
      })
    };
    
    export default vueWaves;
    
    main.js
    import Vue from 'vue'
    import App from './App'
    import router from './router'
    import store from './store'
    
    import vueWaves from './directive/waves'
    Vue.use(vueWaves)
    
    new Vue({
      el: '#app',
      router,
      store,
      components: { App },
      template: '<App/>',
    })
    
    使用示例:
    v-waves
    
    <a class='header__crumbs--btn' @click.stop='goback' v-waves>返回</a>
    
    

    相关文章

      网友评论

          本文标题:Xx-vue实现点击水波纹涟漪效果之自定义指令

          本文链接:https://www.haomeiwen.com/subject/lcgvedtx.html