CSS实现点击防抖

大家都知道,函数节流(throttle)是 JS 中一个非常常见的优化手段,可以有效的避免函数过于频繁的执行。但是,我今天确新学了一招CSS实现点击节流,分享给大家~

1. 实现背景

基于el-button封装的一个按钮组件,用法同Element

2. 实现原理

通过CSS,点击后设置N秒禁止点击的动画。

3. 优点

无需变量控制按钮状态,开箱即用。

4. 代码实现
<template>
  <el-button :class="{throttle: firstAddThrottle}" :circle="circle" :disabled="disabled" :icon="icon" :loading="loading" :plain="plain" :round="round" :size="size" :type="type"
             :style="cssVars" class="my-button" @click="handleClick"
  >
    <slot></slot>
  </el-button>
</template>

<script>
export default {
  name: 'Button',
  props: {
    // 尺寸 medium / small / mini
    size: {
      type: String,
      default: ''
    },
    // 类型		primary / success / warning / danger / info / text
    type: {
      type: String,
      default: ''
    },
    // 是否朴素按钮
    plain: {
      type: Boolean,
      default: false
    },
    // 是否圆角按钮
    round: {
      type: Boolean,
      default: false
    },
    // 是否圆形按钮
    circle: {
      type: Boolean,
      default: false
    },
    // 是否禁用状态
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否加载中状态
    loading: {
      type: Boolean,
      default: false
    },
    // 图标类名
    icon: {
      type: String,
      default: ''
    },
    //   节流秒
    second: {
      type: Number,
      default: 5
    }
  },
  computed: {
    cssVars() {
      return {
        '--throttleNum': this.second + 's'
      }
    }
  },
  data() {
    return {
      // 第一次点击的后添加节流,后面不需要了
      firstAddThrottle: false
    }
  },
  methods: {
    handleClick() {
      this.firstAddThrottle = true
      this.$emit('click')
    }
  }
}
</script>

<style lang="scss" scoped>
button {
  cursor: pointer;
}

.throttle {
  animation: throttle var(--throttleNum) step-end forwards;
}

button:active {
  animation: none;
}

@keyframes throttle {
  from {
    pointer-events: none;
  }
  to {
    pointer-events: all;
  }
}

</style>
5. 使用方式
  1. 引入组件(全局或局部引入)
  2. 使用
      <Button icon="el-icon-download" :second="3" size="mini" type="warning"  @click="exportClick">导出</Button > 

相关推荐

  1. CSS实现

    2024-03-20 10:16:05       22 阅读
  2. 接口(重复

    2024-03-20 10:16:05       9 阅读
  3. CSS节流】实现防止按钮重复

    2024-03-20 10:16:05       41 阅读
  4. 手写实现

    2024-03-20 10:16:05       9 阅读
  5. VUE 全局设置重复

    2024-03-20 10:16:05       25 阅读
  6. 精简版节流实现

    2024-03-20 10:16:05       19 阅读
  7. uniapp实现事件级

    2024-03-20 10:16:05       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-20 10:16:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-20 10:16:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-20 10:16:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-20 10:16:05       20 阅读

热门阅读

  1. 原型,模板,策略,适配器模式

    2024-03-20 10:16:05       17 阅读
  2. 设计模式(结构型设计模式——适配器模式)

    2024-03-20 10:16:05       17 阅读
  3. flink启动错误(使用YARN)

    2024-03-20 10:16:05       21 阅读
  4. HttpClient4基础

    2024-03-20 10:16:05       23 阅读
  5. Oracle中使用coe_load_sql_profile脚本固定执行计划

    2024-03-20 10:16:05       22 阅读
  6. Android Studio中快速修改包名

    2024-03-20 10:16:05       24 阅读
  7. 【Golang入门教程】Go语言变量的声明

    2024-03-20 10:16:05       22 阅读