点击我 #myB…">
当前位置:首页 > JavaScript

js实现按钮效果

2026-02-03 07:43:00JavaScript

实现基础按钮效果

使用HTML和CSS创建基础按钮样式,通过JavaScript添加交互效果:

<button id="myButton">点击我</button>
#myButton {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

#myButton:hover {
  background-color: #45a049;
}
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
  this.textContent = '已点击!';
  this.style.backgroundColor = '#ff9800';
});

添加点击动画效果

使用CSS动画和JavaScript触发:

js实现按钮效果

@keyframes bounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(0.9); }
}

.animate {
  animation: bounce 0.3s;
}
button.addEventListener('click', function() {
  this.classList.add('animate');
  setTimeout(() => this.classList.remove('animate'), 300);
});

实现按钮禁用状态

通过JavaScript控制按钮状态:

js实现按钮效果

button.addEventListener('click', function() {
  this.disabled = true;
  this.textContent = '处理中...';

  // 模拟异步操作
  setTimeout(() => {
    this.disabled = false;
    this.textContent = '完成!';
  }, 2000);
});

创建波纹点击效果

添加视觉反馈的波纹动画:

.ripple {
  position: relative;
  overflow: hidden;
}

.ripple-effect {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.7);
  transform: scale(0);
  animation: ripple 0.6s linear;
}

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}
button.addEventListener('click', function(e) {
  const ripple = document.createElement('span');
  ripple.classList.add('ripple-effect');

  const rect = this.getBoundingClientRect();
  const size = Math.max(rect.width, rect.height);
  ripple.style.width = ripple.style.height = `${size}px`;
  ripple.style.left = `${e.clientX - rect.left - size/2}px`;
  ripple.style.top = `${e.clientY - rect.top - size/2}px`;

  this.appendChild(ripple);
  setTimeout(() => ripple.remove(), 600);
});

实现按钮加载状态

创建加载动画指示器:

.loading::after {
  content: "";
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-left: 8px;
  border: 2px solid #fff;
  border-radius: 50%;
  border-color: #fff transparent #fff transparent;
  animation: loading 1.2s linear infinite;
}

@keyframes loading {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
button.addEventListener('click', function() {
  this.classList.add('loading');
  this.disabled = true;

  setTimeout(() => {
    this.classList.remove('loading');
    this.disabled = false;
  }, 3000);
});

标签: 按钮效果
分享给朋友:

相关文章

vue实现单选按钮

vue实现单选按钮

实现单选按钮的基本方法 在Vue中实现单选按钮通常使用v-model指令绑定数据,结合input元素的type="radio"属性。以下是一个基础示例: <template> <…

vue实现预览效果

vue实现预览效果

实现预览效果的常见方法 在Vue中实现预览效果可以通过多种方式完成,以下列举几种典型场景和对应的实现方案: 图片/文件预览 使用<input type="file">结合FileRead…

css3按钮制作

css3按钮制作

CSS3 按钮制作方法 基础按钮样式 使用 border-radius 和 padding 创建圆角按钮: .btn { display: inline-block; padding: 1…

vue实现动态按钮

vue实现动态按钮

实现动态按钮的基本思路 在Vue中实现动态按钮通常涉及根据数据状态动态改变按钮的样式、文本或行为。可以通过绑定动态类名、样式或事件来实现。 动态绑定按钮样式 使用v-bind:class或简写:cl…

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…