当前位置:首页 > JavaScript

js实现徽章

2026-03-14 23:27:30JavaScript

实现徽章的基本方法

使用HTML和CSS创建徽章的基础结构,通过JavaScript动态添加或修改徽章内容。以下是一个简单示例:

<div class="badge-container">
  <span class="badge">1</span>
</div>
.badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: red;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}
// 更新徽章内容
function updateBadge(count) {
  const badge = document.querySelector('.badge');
  badge.textContent = count;
  badge.style.display = count > 0 ? 'flex' : 'none';
}

动态创建徽章

通过JavaScript动态创建徽章元素并附加到目标元素上:

js实现徽章

function createBadge(parentElement, count) {
  const badge = document.createElement('span');
  badge.className = 'badge';
  badge.textContent = count;

  // 设置父元素为相对定位
  parentElement.style.position = 'relative';

  // 添加徽章到父元素
  parentElement.appendChild(badge);

  return badge;
}

使用SVG创建更复杂的徽章

对于需要更复杂样式的徽章,可以使用SVG:

js实现徽章

function createSVGBadge(parentElement, text) {
  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  svg.setAttribute('width', '24');
  svg.setAttribute('height', '24');

  const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  circle.setAttribute('cx', '12');
  circle.setAttribute('cy', '12');
  circle.setAttribute('r', '10');
  circle.setAttribute('fill', 'red');

  const textElement = document.createElementNS("http://www.w3.org/2000/svg", "text");
  textElement.setAttribute('x', '12');
  textElement.setAttribute('y', '16');
  textElement.setAttribute('text-anchor', 'middle');
  textElement.setAttribute('fill', 'white');
  textElement.textContent = text;

  svg.appendChild(circle);
  svg.appendChild(textElement);

  parentElement.appendChild(svg);
  return svg;
}

响应式徽章组件

创建一个可重用的徽章组件类:

class Badge {
  constructor(options) {
    this.element = document.createElement('span');
    this.element.className = 'badge';

    this.update(options);
  }

  update({ count, color, position }) {
    this.element.textContent = count;
    this.element.style.backgroundColor = color || 'red';

    if (position) {
      this.element.style.top = position.top || '0';
      this.element.style.right = position.right || '0';
    }

    this.element.style.display = count > 0 ? 'block' : 'none';
  }

  attachTo(parentElement) {
    parentElement.style.position = 'relative';
    parentElement.appendChild(this.element);
  }
}

使用示例

// 创建并附加徽章
const notificationIcon = document.getElementById('notification-icon');
const badge = new Badge({ count: 5, color: 'blue' });
badge.attachTo(notificationIcon);

// 更新徽章
badge.update({ count: 3 });

动画效果

为徽章添加简单的动画效果:

.badge-pulse {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
// 添加脉冲动画
function addPulseAnimation(badgeElement) {
  badgeElement.classList.add('badge-pulse');
}

标签: 徽章js
分享给朋友:

相关文章

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与响应式…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js画图实现

js画图实现

使用Canvas API绘制图形 Canvas是HTML5提供的绘图API,通过JavaScript操作Canvas元素可以绘制各种图形。以下是一个简单的示例: <canvas id="myC…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…