vue 自定义指令实现Material风格的动画按钮

 废话不多说,直接上代码

index.ts

/**
 * v-waves
 * 水波纹动画
 * 注意:当前动画颜色取用的是当前节点的字体颜色
 * 故:使用该指令需给 当前节点 设置 字体颜色,避免动画颜色错误
 */

import { App } from "vue";
import "./waves.css";

const context = "@@wavesContext";

//16进制颜色值转化为rgb
String.prototype.colorRgb = function (): string {
  // 16进制颜色值的正则
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 把颜色值变成小写
  var color = this.toLowerCase();
  if (reg.test(color)) {
    // 如果只有三位的值,需变成六位,如:#fff => #ffffff
    if (color.length === 4) {
      var colorNew = "#";
      for (var i = 1; i < 4; i += 1) {
        colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
      }
      color = colorNew;
    }
    // 处理六位的颜色值,转为RGB
    var colorChange = [];
    for (var i = 1; i < 7; i += 2) {
      colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
    }
    return "RGB(" + colorChange.join(",") + ")";
  } else {
    return color;
  }
};

//颜色转换
function colorChange(color: string, alpha: number): string {
  let rgbaVal = "";
  if (/^#/.test(color)) {
    colorChange(color.colorRgb(), alpha);
  } else if (/^(rgba|RGBA)/.test(color)) {
    rgbaVal = color.substring(5, color.lastIndexOf(","));
  } else if (/^(rgb|RGB)/.test(color)) {
    rgbaVal = color.substring(4, color.length - 1);
  }
  return `rgba(${rgbaVal},${alpha})`;
}

function handleClick(el: HTMLElement, binding: any) {
  function handle(e: any) {
    const customOpts = Object.assign({}, binding.value);
    // console.log(window.getComputedStyle(el)); //读取元素全部属性
    const opts = Object.assign(
      {
        ele: el, // 波纹作用元素
        type: "hit", // hit 点击位置扩散 center中心点扩展
        color: colorChange(window.getComputedStyle(el).color, 0.26), // 波纹颜色
      },
      customOpts
    );
    const 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.documentElement.scrollTop || document.body.scrollTop) +
            "px";
          ripple.style.left =
            (e.pageX -
              rect.left -
              ripple.offsetWidth / 2 -
              document.documentElement.scrollLeft || document.body.scrollLeft) +
            "px";
      }
      ripple.style.backgroundColor = opts.color;
      ripple.className = "waves-ripple z-active";

      if (!el[context]) {
        el[context] = {
          removeHandle: handle,
        };
      } else {
        el[context].removeHandle = handle;
      }
      return false;
    }
  }

  return handle;
}

export default (app: App<Element>) => {
  //自定义指令
  app.directive("waves", (el: HTMLElement, binding) => {
    el.addEventListener("click", handleClick(el, binding), false);
  });
};

waves.css

.waves-ripple {
  position: absolute;
  margin: 0 !important;
  border-radius: 100%;
  background-color: rgba(0, 0, 0, 0.15);
  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;
}

在main.ts中注册指令

import { createApp } from "vue";
import directives from "@/directive/waves";
const app = createApp(App);
app.use(directives)

使用

<button v-waves>点击我</button>

效果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Vue 自定义指令是可以在 DOM 元素上添加特殊功能的一种方式。下面是实现按钮禁用功能的代码示例: ```javascript Vue.directive('disable', { bind: function (el, binding) { if (binding.value) { el.setAttribute('disabled', true) } else { el.removeAttribute('disabled') } }, update: function (el, binding) { if (binding.value) { el.setAttribute('disabled', true) } else { el.removeAttribute('disabled') } } }) ``` 然后,在模板中使用这个自定义指令: ``` <template> <button v-disable="isDisabled">提交</button> </template> <script> export default { data () { return { isDisabled: true } } } </script> ``` 当 `isDisabled` 为 `true` 时,按钮将被禁用。 希望这个代码示例对你有所帮助。 ### 回答2: Vue自定义指令是一种可以用于扩展和改造现有DOM元素行为的方式。通过自定义指令,我们可以在Vue应用中自定义一些特殊的操作。下面是一个实现按钮禁用的Vue自定义指令的例子。 首先,在Vue应用中定义一个名为`v-disable`的自定义指令,用来实现按钮的禁用功能。在Vue实例的`directives`属性中注册该自定义指令。 ``` Vue.directive('disable', { // 在绑定元素的时候调用 bind: function(el, binding) { // 获取自定义指令的值 var disabled = binding.value; // 根据传入的disabled值设置按钮的禁用状态 if (disabled) { el.setAttribute('disabled', 'disabled'); } else { el.removeAttribute('disabled'); } }, // 在自定义指令所在的组件的VNode更新时调用 update: function(el, binding) { // 获取自定义指令的值 var disabled = binding.value; // 根据传入的disabled值设置按钮的禁用状态 if (disabled) { el.setAttribute('disabled', 'disabled'); } else { el.removeAttribute('disabled'); } } }); ``` 然后,在模板中使用该自定义指令来禁用按钮。在需要禁用按钮的地方加上`v-disable`指令,并绑定一个布尔值来控制按钮的禁用状态。 ``` <button v-disable="isDisabled">禁用按钮</button> ``` 其中,`isDisabled`是一个Vue实例中的data属性,它的值决定了按钮是否禁用。当`isDisabled`为`true`时,按钮被禁用;当`isDisabled`为`false`时,按钮可用。 通过以上步骤,我们就实现了一个Vue自定义指令实现按钮的禁用功能。在使用指令的地方,可以根据需要动态控制按钮的禁用状态,以达到更灵活的操作按钮的目的。 ### 回答3: Vue自定义指令是一种用于向Vue应用程序添加自定义功能的方法。要实现按钮的禁用功能,可以通过自定义指令实现。以下是一个实现按钮禁用的Vue自定义指令的示例: 首先,在Vue组件的JavaScript代码中,定义并注册一个名为'disable-button'的自定义指令: ``` Vue.directive('disable-button', { bind: function(el, binding, vnode) { // 获取指令的参数 var disable = binding.value; // 根据参数,禁用或启用按钮 if (disable) { el.setAttribute('disabled', 'disabled'); } else { el.removeAttribute('disabled'); } } }); ``` 接下来,在Vue组件的HTML模板中,使用该自定义指令来控制按钮的禁用状态: ``` <button v-disable-button="true">禁用按钮</button> <button v-disable-button="false">启用按钮</button> ``` 在上述示例中,使用v-disable-button指令来控制按钮的禁用状态。当指令的参数为true时,按钮将被禁用,当参数为false时,按钮将被启用。 通过以上的自定义指令,我们可以通过控制参数值来动态地禁用或启用按钮,从而实现按钮禁用的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值