当前位置:首页 > JavaScript

js实现一个按钮组件

2026-03-02 01:14:36JavaScript

实现一个基础按钮组件

使用原生JavaScript创建一个可复用的按钮组件,包含基础样式和点击事件。

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.style.padding = '8px 16px';
    button.style.borderRadius = '4px';
    button.style.backgroundColor = '#007bff';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.cursor = 'pointer';
    button.addEventListener('click', this.onClick);
    return button;
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

添加按钮状态管理

扩展按钮组件,增加禁用状态和样式变化功能。

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = false;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    this.applyBaseStyles(button);
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    return button;
  }

  applyBaseStyles(button) {
    button.style.padding = '8px 16px';
    button.style.borderRadius = '4px';
    button.style.border = 'none';
    button.style.cursor = 'pointer';
    button.style.transition = 'background-color 0.3s';
    this.updateButtonStyle();
  }

  updateButtonStyle() {
    if (this.disabled) {
      this.element.style.backgroundColor = '#cccccc';
      this.element.style.cursor = 'not-allowed';
    } else {
      this.element.style.backgroundColor = '#007bff';
      this.element.style.cursor = 'pointer';
    }
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

使用CSS类代替内联样式

改进组件,使用CSS类管理样式,提高可维护性。

class Button {
  constructor(text, onClick) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = false;
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.classList.add('btn');
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    this.updateButtonStyle();
    return button;
  }

  updateButtonStyle() {
    if (this.disabled) {
      this.element.classList.add('btn-disabled');
      this.element.classList.remove('btn-active');
    } else {
      this.element.classList.add('btn-active');
      this.element.classList.remove('btn-disabled');
    }
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

配套CSS样式:

.btn {
  padding: 8px 16px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-active {
  background-color: #007bff;
  color: white;
}

.btn-disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

添加按钮类型支持

扩展组件支持不同按钮类型(主按钮、次按钮、危险按钮等)。

class Button {
  constructor(text, onClick, options = {}) {
    this.text = text;
    this.onClick = onClick;
    this.disabled = options.disabled || false;
    this.type = options.type || 'primary';
    this.element = this.createButton();
  }

  createButton() {
    const button = document.createElement('button');
    button.textContent = this.text;
    button.classList.add('btn', `btn-${this.type}`);
    button.addEventListener('click', () => {
      if (!this.disabled) {
        this.onClick();
      }
    });
    this.updateButtonStyle();
    return button;
  }

  updateButtonStyle() {
    this.element.classList.toggle('btn-disabled', this.disabled);
  }

  setDisabled(disabled) {
    this.disabled = disabled;
    this.updateButtonStyle();
  }

  setType(type) {
    this.element.classList.remove(`btn-${this.type}`);
    this.type = type;
    this.element.classList.add(`btn-${this.type}`);
  }

  render(container) {
    if (container) {
      container.appendChild(this.element);
    }
    return this.element;
  }
}

配套CSS样式扩展:

js实现一个按钮组件

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
}

.btn-success {
  background-color: #28a745;
  color: white;
}

组件使用方法示例

// 创建按钮实例
const myButton = new Button('点击我', () => {
  console.log('按钮被点击');
}, {
  type: 'primary'
});

// 渲染到页面
myButton.render(document.body);

// 禁用按钮
myButton.setDisabled(true);

// 改变按钮类型
myButton.setType('danger');

这些实现展示了如何从简单到复杂逐步构建一个可复用的JavaScript按钮组件,包含样式管理、状态控制和类型支持等功能。

标签: 组件按钮
分享给朋友:

相关文章

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

css图片按钮制作

css图片按钮制作

使用纯CSS创建图片按钮 通过CSS的background-image属性可以轻松将图片设置为按钮背景。关键点在于设置合适的尺寸、去除默认边框并添加交互状态: .image-btn { widt…

vue实现按钮改变文本

vue实现按钮改变文本

Vue 实现按钮改变文本的方法 在 Vue 中实现按钮点击改变文本的功能,可以通过数据绑定和事件处理来实现。以下是几种常见的方法: 方法 1:使用 v-on 和 v-bind 指令 通过 v-on…

uniapp组件

uniapp组件

uniapp组件基础概念 uniapp的组件系统基于Vue.js,支持跨平台开发(H5、小程序、App等)。组件分为内置基础组件(如<view>、<button>)和自定义组件…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue实现折叠组件

vue实现折叠组件

实现折叠组件的基本思路 在Vue中实现折叠组件通常需要利用动态绑定和条件渲染。核心是通过控制一个布尔值状态来决定内容是否显示,并添加过渡动画提升用户体验。 基础实现方法 使用v-show或v-if控…